Page 1 of 1
Using mysql_fetch_row to retrieve data from database
Posted: Wed Mar 12, 2003 4:35 pm
by marnieg
Here is the code I have for selecting all fields from a table based on an email address entered. When I call the function and try to echo the results I get NO data. I have tried other functions such as fetch_assoc, but get the same results.
I know that I am getting a valid result because it does echo the literals, but no variables.
Please advise
mysql_select_db("orders");
$query = "select * from rxfirst where email = '.$email.')";
$result = mysql_query($query);
if ($result)
$row = mysql_fetch_row($result);
echo "Name of Patient: <u>" .$row[3]. "</u> Phone number: <u>".$row[9]."</u><br>";
echo "Mailing Address: <u>" .$row[4]. "</u> Apt/Space:_________ <br>";
I have verified that name is the 4th column in my table, since starting with 0 and phone is 9, and address is 4.
Posted: Wed Mar 12, 2003 4:38 pm
by volka
try
Code: Select all
$row = mysql_fetch_row($result) or die('no data');
print_r($row);
Posted: Wed Mar 12, 2003 5:05 pm
by pootergeist
just remove the closing parentesis from your select statement
where email = '.$email.')"; == bad
where email = '.$email.'"; == better
Posted: Thu Mar 13, 2003 10:05 am
by marnieg
Thanks for pointing out the ) in my select statement and the other post for using the print_r($row) so I could see the array.
The actual solution was to remove the periods (.) around the variable $email in the select statement.
In all the examples I have seen online and in my php reference manuals, it shows the period(.) syntax. I'm not sure why but I am just glad I got it working.
Thanks for your direction.
Posted: Thu Mar 13, 2003 10:19 am
by daven
What you should do is this:
$query = "select * from rxfirst where email = '".$email."'";
The dot notation is for string concatenation. If you have imbedded strings ($query = "select * from rxfirst where email = '$email'";) there are several problems which can arise.
Posted: Thu Mar 13, 2003 10:21 am
by twigletmac
The period is used for concenation, that is joining two strings and/or variables together. So you can do:
Code: Select all
$concentated_vars = $var1.$var2;
$concentated_strings = 'something'.$var1;
PHP doesn't parse code within single quotes so
Code: Select all
$var = 'world';
echo 'Hello $var';
would produce
whereas
Code: Select all
$var = 'world';
echo 'Hello '.$var;
would produce
PHP does however parse code in double quotes so you would get 'hello world' from this too:
Your SQL statement is in double quotes so:
Code: Select all
$query = "select * from rxfirst where email = '$email'";
is fine.
If you are having problems with an SQL statement echo out the statement to check that variables are being replaced correctly, and use mysql_error() to trap errors:
Code: Select all
echo 'The SQL statement is: '.$query;
$result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
For more information:
http://www.php.net/manual/en/language.types.string.php
http://www.php.net/manual/en/function.mysql-error.php
http://www.php.net/manual/en/function.die.php
Mac
Posted: Thu Mar 13, 2003 10:24 am
by twigletmac
daven wrote:What you should do is this:
$query = "select * from rxfirst where email = '".$email."'";
The concenation is not necessary within double quoted strings.
daven wrote:If you have imbedded strings ($query = "select * from rxfirst where email = '$email'"; ) there are several problems which can arise.
Not within double quoted strings, unless you're using array elements in which case you could do:
Code: Select all
$query = "select * from rxfirst where email = '$email[info]'";
// or
$query = "select * from rxfirst where email = '{$email['info']}'";
It's down to personal choice.
Mac