Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-09-2013, 06:29 PM
smirkley smirkley is offline
 
Join Date: Apr 2008
Posts: 627
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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?
Reply With Quote
  #2  
Old 05-09-2013, 06:42 PM
nhawk nhawk is offline
 
Join Date: Jan 2011
Posts: 1,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Here you go...

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

$useremail = $tmpname['email'];
Reply With Quote
Благодарность от:
smirkley
  #3  
Old 05-09-2013, 07:26 PM
smirkley smirkley is offline
 
Join Date: Apr 2008
Posts: 627
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #4  
Old 05-09-2013, 10:42 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #5  
Old 05-09-2013, 10:52 PM
smirkley smirkley is offline
 
Join Date: Apr 2008
Posts: 627
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lynne View Post
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.
Reply With Quote
  #6  
Old 05-10-2013, 09:08 AM
nhawk nhawk is offline
 
Join Date: Jan 2011
Posts: 1,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 05-10-2013, 11:28 AM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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']
Reply With Quote
Благодарность от:
nhawk
  #8  
Old 05-10-2013, 03:46 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If you include the global.php file, you don't need to also include the init.php file.
Reply With Quote
Благодарность от:
nhawk
  #9  
Old 05-10-2013, 07:30 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
Благодарность от:
nhawk
  #10  
Old 05-11-2013, 12:58 AM
smirkley smirkley is offline
 
Join Date: Apr 2008
Posts: 627
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
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?
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:50 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.06817 seconds
  • Memory Usage 2,272KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (6)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (4)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (4)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete