Embedding a variable in an SQL Query

This is a discussion on Embedding a variable in an SQL Query within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I am getting the following error when running : $result = mysql_query("SELECT * FROM users WHERE Username = $ThisUsername"); $num_rows = mysql_num_rows($...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-29-2003
Andy Levy
 
Posts: n/a
Default Embedding a variable in an SQL Query

I am getting the following error when running :

$result = mysql_query("SELECT * FROM users WHERE Username = $ThisUsername");
$num_rows = mysql_num_rows($result);

Error =
Warning: Warning: mysql_num_rows(): supplied argument is not a valid MySQL
result resource in I:\Inetpub\wwwroot\protex\confirmation.php on line 37 in
I:\Inetpub\wwwroot\protex\confirmation.php on line 37


Reply With Quote
  #2 (permalink)  
Old 10-29-2003
Andy Levy
 
Posts: n/a
Default Re: Embedding a variable in an SQL Query

If i change $ThisUsername to an actual value in the database then it works
fine ie. change it to

WHERE Username = 'Value1' ");

but not as an embedded value.


"Andy Levy" <andy_levy@hotmail.com> wrote in message
news:bNNnb.4596$0E5.125@news-binary.blueyonder.co.uk...
> I am getting the following error when running :
>
> $result = mysql_query("SELECT * FROM users WHERE Username =

$ThisUsername");
> $num_rows = mysql_num_rows($result);
>
> Error =
> Warning: Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource in I:\Inetpub\wwwroot\protex\confirmation.php on line 37

in
> I:\Inetpub\wwwroot\protex\confirmation.php on line 37
>
>



Reply With Quote
  #3 (permalink)  
Old 10-29-2003
furry
 
Posts: n/a
Default Re: Embedding a variable in an SQL Query

Try the following, which will output the cause of the problem:

$result = mysql_query("SELECT * FROM users WHERE Username = $ThisUsername")
or die("mysql_query failed - Error: " . mysql_error());
$num_rows = mysql_num_rows($result);





"Andy Levy" <andy_levy@hotmail.com> wrote in message
news:bNNnb.4596$0E5.125@news-binary.blueyonder.co.uk...
I am getting the following error when running :

$result = mysql_query("SELECT * FROM users WHERE Username = $ThisUsername");
$num_rows = mysql_num_rows($result);

Error =
Warning: Warning: mysql_num_rows(): supplied argument is not a valid MySQL
result resource in I:\Inetpub\wwwroot\protex\confirmation.php on line 37 in
I:\Inetpub\wwwroot\protex\confirmation.php on line 37


Reply With Quote
  #4 (permalink)  
Old 10-29-2003
Alvaro G Vicario
 
Posts: n/a
Default Re: Embedding a variable in an SQL Query

*** Andy Levy wrote/escribió (Wed, 29 Oct 2003 11:54:57 -0000):
> If i change $ThisUsername to an actual value in the database then it works
> fine ie. change it to
>
> WHERE Username = 'Value1' ");
>
> but not as an embedded value.


How do you set your variable?

$ThisUsername="Value1";

or

$ThisUsername="'Value'";

First way will fail if it isn't a numeric field.


>> $result = mysql_query("SELECT * FROM users WHERE Username =

> $ThisUsername");



--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Reply With Quote
  #5 (permalink)  
Old 10-29-2003
furry
 
Posts: n/a
Default Re: Embedding a variable in an SQL Query


you're just missing the quotes. try this:

$result = mysql_query('select * from users where Username
="'.$ThisUsername.'");



"Andy Levy" <andy_levy@hotmail.com> wrote in message
news:LUNnb.4672$0E5.1308@news-binary.blueyonder.co.uk...
If i change $ThisUsername to an actual value in the database then it works
fine ie. change it to

WHERE Username = 'Value1' ");

but not as an embedded value.


"Andy Levy" <andy_levy@hotmail.com> wrote in message
news:bNNnb.4596$0E5.125@news-binary.blueyonder.co.uk...
> I am getting the following error when running :
>
> $result = mysql_query("SELECT * FROM users WHERE Username =

$ThisUsername");
> $num_rows = mysql_num_rows($result);
>
> Error =
> Warning: Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource in I:\Inetpub\wwwroot\protex\confirmation.php on line 37

in
> I:\Inetpub\wwwroot\protex\confirmation.php on line 37
>
>




Reply With Quote
  #6 (permalink)  
Old 10-29-2003
Tom Thackrey
 
Posts: n/a
Default Re: Embedding a variable in an SQL Query


On 29-Oct-2003, "Andy Levy" <andy_levy@hotmail.com> wrote:

> I am getting the following error when running :
>
> $result = mysql_query("SELECT * FROM users WHERE Username =
> $ThisUsername");
> $num_rows = mysql_num_rows($result);
>
> Error =
> Warning: Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource in I:\Inetpub\wwwroot\protex\confirmation.php on line 37
> in
> I:\Inetpub\wwwroot\protex\confirmation.php on line 37


Unless Username is numeric you need quotes around the value, try:

$result = mysql_query("SELECT * FROM users WHERE Username = '$ThisUsername'
");


--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to jamesbutler@willglen.net (it's reserved for spammers)
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:19 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0