Forum Moderators: open
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.
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
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.
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.
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 ;)