vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Query problem with username (https://vborg.vbsupport.ru/showthread.php?t=297981)

smirkley 05-09-2013 06:29 PM

Query problem with username
 
Seems simple enough, but I have a way with making it difficult it seems. :(


I have a variable that is loaded, $username

And I want to fill the variable $useremail from that username

This will go in a php file. But I cant figure out how to do a query that will accomplish this.

$username
$useremail = sqlquery from the $username


Can someone smak me with the obvious please?

nhawk 05-09-2013 06:42 PM

Here you go...

Code:

$tmpname = $vbulletin->db->query_first("SELECT email
FROM " . TABLE_PREFIX . "user
WHERE username = '" . $username . "'");

$useremail = $tmpname['email'];


smirkley 05-09-2013 07:26 PM

Thanks, giving it a try now.

Quick question?... can I do this to put it into an html template?

Code:

<?php
$tmpname = $vbulletin->db->query_first("SELECT email
FROM " . TABLE_PREFIX . "user
WHERE username = '" . $username . "'");

$useremail = $tmpname['email'];
?>

--------------- Added [DATE]1368133742[/DATE] at [TIME]1368133742[/TIME] ---------------

Reason asking about the html template is, I found where I need to inject this query is in an embedded html template within the php file.

Code:

<?php
\$tmpname = \$vbulletin->db->query_first("SELECT email
FROM " . TABLE_PREFIX . "user
WHERE username = '" . \$username . "'");

\$useremail = \$tmpname['email'];
?>

Currently produces
"Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING"

in

WHERE username = '" . \$username . "'");

I have to add the backslashes infront of $ to eliminate another error.

--------------- Added [DATE]1368135094[/DATE] at [TIME]1368135094[/TIME] ---------------

$HTML_header_t is the only place I can execute this sql query, with of course $useremail being printed in the body of that page..

Code:

<?php
$HTML_header_t = <<< TEOL
<meta http-equiv="Content-Type" content="text/html; charset={\$stylevar[charset]}" />
<html>
<head>
\$css
</head>
<body>

....etc


Lynne 05-09-2013 10:42 PM

You cannot put php into a template. You need to put the php into a plugin and then you preregister the resulting variable for the variable you are going to use it in. It's not clear at all which template you are wanting to use this in so there is no way I can get more specific.

I can point you to this article about preregistering variables (near the end) - [vB4] Rendering templates and registering variables - a short guide

smirkley 05-09-2013 10:52 PM

Quote:

Originally Posted by Lynne (Post 2420972)
You cannot put php into a template. You need to put the php into a plugin and then you preregister the resulting variable for the variable you are going to use it in. It's not clear at all which template you are wanting to use this in so there is no way I can get more specific.

I can point you to this article about preregistering variables (near the end) - [vB4] Rendering templates and registering variables - a short guide

Thanks Lynne.

I am reading that and hopefully may find a solution.

This isnt a template per se', at least not a stock vb template. It is a custom file and called by another custom file.

It is a php file as you can see at the bottom of my last post for part of it. Unless that is a template by definition.

--------------- Added [DATE]1368150006[/DATE] at [TIME]1368150006[/TIME] ---------------

Lynne, I read that completely and I do understand what I have read, but that doesnt, nor can it, apply here.

I know that my problem is the sql syntax here, and I cant seem to find the syntax issue.

Thanks anyway, but still looking for a tad bit-o-help.

nhawk 05-10-2013 09:08 AM

When you use HTML and PHP mixed on a page, you use the .php extension for the file but you separate the PHP and HTML like this...

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php echo HTML_PARAMS; ?>>
<head>
<title><?php echo META_TAG_TITLE; ?></title>

<body>
etc..

Also, to use the query I gave you would need to require vBulletin's global.php file to setup the vB database connection. I'm don't recall if init.php is needed or not. You can try it without it to find out.

So something like this would be needed...
Code:

<?php
define('CSRF_PROTECTION', true);
require_once('./global.php');
require_once (CWD . '/includes/init.php');

$username = "Joe";

$tmpname = $vbulletin->db->query_first("SELECT email
FROM " . TABLE_PREFIX . "user
WHERE username = '" . $username . "'");

$useremail = $tmpname['email'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php echo HTML_PARAMS; ?>>
<head>
<title><?php echo META_TAG_TITLE; ?></title>

<body>
My Email Address Is: <?php echo $useremail ?>
etc..

I hope that helps.

nerbert 05-10-2013 11:28 AM

If you include or requite global.php you should be able to get any user information without an extra query, something like this:

$vbulletin->userinfo['email']

Lynne 05-10-2013 03:46 PM

If you include the global.php file, you don't need to also include the init.php file.

kh99 05-10-2013 07:30 PM

You should probably use $vbulletin->db->escape_string($username) in the query, otherwise if a username has a special character it may cause an error.

smirkley 05-11-2013 12:58 AM

Quote:

Originally Posted by kh99 (Post 2421160)
You should probably use $vbulletin->db->escape_string($username) in the query, otherwise if a username has a special character it may cause an error.

How would I reference that using $username to get $useremail?


All times are GMT. The time now is 05:38 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01145 seconds
  • Memory Usage 1,748KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (6)bbcode_code_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete