PDA

View Full Version : Problem with mySQL


Balkee867
07-22-06, 10:13 AM
I have the following code:


<?


if (!mysql_connect('myhost','mydatabase','mypassword' ))

echo mysql_error();

else echo "connection established.";

mysql_select_db('mydatabase');

$get=mysql_query(SELECT * FROM users WHERE firstname=$_POST['firstname']);

while($row=mysql_fetch_array($get))

echo "<p><br><font face='verdana'>", $row ['firstname'] , " " , $row ['lastname'];

die (mysql_error());

?>


My problem is in this string:


$get=mysql_query(SELECT * FROM users WHERE firstname=$_POST['firstname']);


when I try and select $_POST['firstname'] , it wont let me do it. Since the person I want to select is the person who just registered, how could I do this without using $_POST['firstname'] or how can I use it correctly?

Nico
07-22-06, 10:46 AM
This may helps: mysql_insert_id() (http://us2.php.net/manual/en/function.mysql-insert-id.php)

I don't see why this wouldn't work though. Only one thing, you have to put quotes around the SQL string.


$get=mysql_query("SELECT * FROM users WHERE firstname = '". mysql_real_escape_string($_POST['firstname']) ."'");


EDIT:

Moving to database...

Balkee867
07-22-06, 01:22 PM
hmm still not working for some reason. Now I have an error telling me there's an enexpected "else" statement in the last line:


$get=mysql_query("SELECT * FROM users WHERE firstname= '". mysql_real_escape_string($_POST['firstname'])."'");

if ($row=mysql_fetch_array($get));

echo $row;

else echo mysql_error();


and I can't figure out why? I figure i'll at least know what's wrong if I can get the mysql_error() , but I can't for some reason?

Nico
07-22-06, 02:08 PM
Not exactly sure what you want to do, but try this

$get=mysql_query("SELECT * FROM users WHERE firstname= '". mysql_real_escape_string($_POST['firstname'])."'") OR die( mysql_error() );

$row=mysql_fetch_array($get);

echo $row['firstname'];



After applying mysql_fetch_array() to $row, $row will be an array. So you have to specify the array key as well when you try to echo its content. To see all array keys use this if you're unsure.



print_r($row);