PHP and SQL queries...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
richcoleuk
Forum Newbie
Posts: 24
Joined: Fri Nov 05, 2004 1:33 pm

PHP and SQL queries...

Post 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'
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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'];
}
?>
richcoleuk
Forum Newbie
Posts: 24
Joined: Fri Nov 05, 2004 1:33 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
richcoleuk
Forum Newbie
Posts: 24
Joined: Fri Nov 05, 2004 1:33 pm

Post by richcoleuk »

thanks phenom
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

no prob :lol:
Post Reply