question

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
jackson_1
Forum Newbie
Posts: 1
Joined: Tue Nov 22, 2011 8:41 pm

question

Post by jackson_1 »

Hi everybody,

I couldn't print a column of database in the code below. Please help me.

Code: Select all

$query = "SELECT UserId, FirstName, LastName, UserName, Password FROM User WHERE User.UserName =
'".$_POST["username"]."' AND User.Password = '".$_POST["password"]."'";

$result = mysql_query($query) or die('Query failed');

if (mysql_fetch_row($result)){
$cnt = mysql_num_rows($result);
$row =  mysql_fetch_array($result);

while($row = mysql_fetch_array( $result )) {
                
echo $row['FirstName'];  [b]<------------ This doesn't work[/b]
        } 
}
Last edited by Benjamin on Wed Nov 23, 2011 6:17 am, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: question

Post by Gopesh »

Hi,Any error messages got? Pls post the full code including the connection satements.
JeFFb68CAM
Forum Newbie
Posts: 15
Joined: Tue Apr 03, 2007 11:17 pm

Re: question

Post by JeFFb68CAM »

Either change

Code: Select all

while($row = mysql_fetch_array( $result )) {[c
to

Code: Select all

while($row = mysql_fetch_array( $result, MYSQL_ASSOC )) {
or to

Code: Select all

while($row = mysql_fetch_assoc( $result )) {
JeFFb68CAM
Forum Newbie
Posts: 15
Joined: Tue Apr 03, 2007 11:17 pm

Re: question

Post by JeFFb68CAM »

I just noticed you have a lot of extras there. Get rid of it, try this:

Code: Select all

$query = "SELECT UserId, FirstName, LastName, UserName, Password FROM User WHERE UserName =
'".$_POST["username"]."' AND Password = '".$_POST["password"]."'";

$result = mysql_query($query) or die('Query failed');

if (mysql_num_rows($result) == 1){
    $row = mysql_fetch_assoc($result);
    echo $row['FirstName'];
}
Post Reply