MySQL Database call help
07-07-2010, 03:52 PM,
#1
MySQL Database call help
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! :)
Hey.
Reply
07-07-2010, 11:17 PM, (This post was last modified: 07-07-2010, 11:33 PM by RichardGv.)
#2
RE: MySQL Database call help
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.
Gentoo Linux User (w/ fvwm) / Loyal Firefox User / Owner of a Stupid Old Computer - My PGP Public Key

No man is an island, entire of itself; every man is a piece of the continent, a part of the main; if a clod be washed away by the sea, Europe is the less, as well as if a promontory were, as well as if a manor of thy friends or of thine own were; any man's death diminishes me, because I am involved in mankind; and therefore never send to know for whom the bell tolls; it tolls for thee.
-- Devotions Upon Emergent Occasions (1624), John Donn
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  MySql backup? Mone 1 1,302 06-01-2011, 02:29 AM
Last Post: capp

Forum Jump: