NiftyHost Forums (Archive)
MySQL Database call help - Printable Version

+- NiftyHost Forums (Archive) (https://niftyhost.chary.us/support)
+-- Forum: Technology (https://niftyhost.chary.us/support/forum-7.html)
+--- Forum: Coding (https://niftyhost.chary.us/support/forum-12.html)
+--- Thread: MySQL Database call help (/thread-376.html)



MySQL Database call help - Cameron - 07-07-2010

Looking for some more help xD.

This time, I want to display the gender of the user on the 'profile'. I have added the correct fields in the register form and the database via PHPMyAdmin.

I use the below code for the 'profile':
PHP Code:
<?php

include ('functions.php');

if (!
loggedin())
{
   
header("location:login.php");
   exit();
}
?>

<html>

<?php

    
//connect to database
    
mysql_connect("localhost","cameron_cameron","MYPASSGOESHERE") or die();
    
mysql_select_db("cameron_login") or die();
  
$welcome $_SESSION['username'];
  
$query mysql_query("SELECT * FROM users WHERE gender='$gender'");
  
$gender mysql_fetch_array($query);
  
var_dump($gender);
  echo 
"Welcome, ".$welcome."! - <a href='logout.php'>Logout</a>";
  echo 
"You are ".$gender."!";
  
?>

The dump returns this:

bool(false)

But the $gender displays nothing.

Can anyone tell me how to fix this?

Thanks in advance! :)


RE: MySQL Database call help - RichardGv - 07-07-2010

The SQL query you used shouldn't work correctly since "SELECT * FROM users WHERE gender='$gender'" finds users whose gender is the value of $gender. But $gender is undefined at the time. You are supposed to use "SELECT gender FROM users WHERE username='$username'" or something like that, I think. (Do not tell me this SQL query is bust again! The SQL query very possibly won't work without modifications because I have no idea about the columns and structure of your SQL table. $username needs to be replaced with the correct username variable, as well. $_SESSION['username']? That does not seem nice enough.)
The reason that var_dump() returns false is the SQL query is incorrect and there's no rows returned from the query. mysql-fetch-array() returns FALSE if there are no rows.

Even if the SQL query is corrected, your code still won't work correctly. It will print out "Array" instead of the correct gender. mysql-fetch-array() returns an array, so $gender should be an array instead of a string variable. You cannot display an array with echo correctly.

Also, I think relearning SQL would be a nice idea for you.