user3369387
user3369387

Reputation: 13

Retrieving Data from mysql

I setup my database and want to display first name last name email and age. It is only displaying the first name. here is my code:

<?php
$host_name = 'localhost';
$db_user ='root';
$db_pass = '';
$db_name = 'mydb';
/* Connect to MySQL */
 $con = mysql_connect("$host_name","$db_user","$db_pass") or die ("Couldn't connect!");
 $db = mysql_select_db("$db_name") or die ("Couldn't connect to database!");

 $query = mysql_query("SELECT * FROM users");


/*Fetch the results / convert into an array */
while($rows = mysql_fetch_array($query))
            {
        $rows = $rows['first_name'];
        $last_name = $rows['last_name'];


                echo
             $rows.'</br>'.$last_name;
            }
?>

This is error i get

Warning: Illegal string offset 'last_name' in C:\xampp\htdocs\global.php on line 17 eric e Warning: Illegal string offset 'last_name' in C:\xampp\htdocs\global.php on line 17 stacey s

I checked my tables and all fields exist what is wrong with my coding so far. I didnt add the email or age because i will still get offstring error.

Upvotes: 0

Views: 69

Answers (2)

echo_Me
echo_Me

Reputation: 37243

you are giving same name $rows to first name and to the fetch

try change the name

   $rows = $rows['first_name'];

to

   $first_name = $rows['first_name'];

then

 echo $first_name.'</br>'.$last_name;

Upvotes: 0

Paul McLoughlin
Paul McLoughlin

Reputation: 2289

while($rows = mysql_fetch_array($query))
            {
        $first_name = $rows['first_name']; 
        $last_name = $rows['last_name'];



             echo $first_name .'</br>'.$last_name;
            }

Try this.

Upvotes: 1

Related Questions