Forum Moderators: open

Message Too Old, No Replies

SELECT * FROM db.table WHERE username = $string

SELECT * FROM db.table WHERE username = $string

         

troubled

8:33 pm on Aug 8, 2006 (gmt 0)

10+ Year Member



Hi peeps,

I've seen a similar question to this in my extensive searches of your forum, unfortunatly the answer given doesn't work for me.

I'm trying to send a simple SQL query to a database and display a table in HTML format with the details. Everything works except the query part.

The code is as follows;

$req_user_info = $database->getEquipmentInfo($req_user);

$name = $req_user_info['username'];

echo $name;

function Equipment(){
global $database;
$q = "SELECT type,modem,osl FROM ".TBL_EQUIPMENT." WHERE `username` = '$name'";
$result = $database->query($q);
$num_rows = mysql_numrows($result);
if(!$result ¦¦ ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "Database table empty";
return;
}

* I know that $name contains the info I want due to the echo of it before the function
* If I run the code as is, I get my 'Database table empty' message
# if I remove the single quotes from '$name' then I get an error;

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /public_html/info.php on line 137
Error displaying info

Please help! I'm desperate.

loneregister

9:27 pm on Aug 8, 2006 (gmt 0)

10+ Year Member



you may wish to verify that the value you are passing does not contain extraneous spaces.

You can test this by using a trim() function.

OR - if the username field has extra spaces in it, you may wish to use LIKE just to check it.

Finally - in testing it, I'd hardcode the variable in there to something that you know returns results, and see what happens. If it does, then somehow someway they are not matching.

Hope this helps you in the right direction.

Kevin

LifeinAsia

10:17 pm on Aug 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Additionally, if extraneous spaces were entered into the database field, trimming the $name variable will guarantee no matches. So make sure you trim extraneous spaces when entering data as well.

physics

11:20 pm on Aug 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks to me like you will have to pass $name into Equipment() as a parameter or make it global (or use $_SESSION, probably better than global).

physics

11:22 pm on Aug 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By the way, welcome to WebmasterWorld.com troubled!

troubled

7:10 am on Aug 9, 2006 (gmt 0)

10+ Year Member



Hi,

Thanks for all your help so far, I'll look up the Trim() and $_Session and see if i can't use them.

I'm now also echoing my $q = query in the error message I get like so;

if($num_rows == 0){
echo "Database table empty";
echo $q;
return;

and it actually returns a blank in the username match. I guess this means my string isn't working after all, right?

Thanks again. I'll keep trying.

troubled

7:22 am on Aug 9, 2006 (gmt 0)

10+ Year Member



Horay!

Thanks peeps. You've all helped loads and I've finally got it working.
Code is as follows;

function Equipment(){
global $database;
$name = $_SESSION['username'];
$q = "SELECT type,modem,osl FROM ".TBL_EQUIPMENT." WHERE username = '$name'";
$result = $database->query($q);
$num_rows = mysql_numrows($result);
if(!$result ¦¦ ($num_rows < 0)){
echo "Error displaying info";
echo $q;
return;
}
if($num_rows == 0){
echo "Database table empty";
echo $q;
return;
}

So, I've learned, if you're going to use a string in a function.
Create the string IN the function.

And I think $_SESSION is better, although I don't really understand why.

Thanks again.

physics

10:09 pm on Aug 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to hear it's working. Personally I would do:
function Equipment($database,$name){
...

Then when you call Equipment you call it like
Equipment($this_database,$this_name);

That way you can even move the function off to a 'functions' file and call it from any .php file that includes the functions file.

But whatever works for now ;)