Page 1 of 1

PHP and SQL queries...

Posted: Sat Dec 04, 2004 9:48 am
by richcoleuk
I have used the following query on a MySQL table;

$sql = "SELECT FirstName, LastName FROM user;";
mysql_query($sql);

This will return some values from the FirstName and LastName columns of the table. How do i now use this returned information.

E.g

Hello 'FirstName' your last name is 'LastName'

Posted: Sat Dec 04, 2004 9:54 am
by John Cartwright

Code: Select all

<?php
$sql = "SELECT FirstName, LastName FROM user;"; 
$result = mysql_query($sql); 

//loop through each row
while ($row = mysql_fetch_assoc($result))
{
echo 'your firstname is '. $row['FirstName'] .' and your lastname is '. $row['LastName'];
}
?>

Posted: Sat Dec 04, 2004 9:57 am
by richcoleuk
okay thanks for that. I have a variable called $uname that holds the username of the user. I want to use this to add to the previous query so that it will select just the user WHERE username = '$uname'

This doesn't work:

$sql = "SELECT FirstName FROM user WHERE UserName = '" + $uname + "';

i get the error:

Parse error: parse error in /home/cr202/public_html/success.php on line 33

Posted: Sat Dec 04, 2004 10:14 am
by John Cartwright

Code: Select all

$sql = "SELECT FirstName FROM user WHERE UserName = '".$uname."'";
when escaping a string with a variable you must use a period

Posted: Sat Dec 04, 2004 10:19 am
by richcoleuk
thanks phenom

Posted: Sat Dec 04, 2004 12:48 pm
by John Cartwright
no prob :lol: