Reputation: 4253
I have a table with columns (user_id, user_name, user_email, user_pw) and 2 rows
1 admin [email protected] admin
2 user_1 [email protected] user_1
I am trying to query the db from PHP following this example http://php.net/manual/en/mysqli-result.fetch-row.php
but when I print the output, I only get the column names instead of the values (and unfortunately, also line breaks get ignored)
Connection successfulSelect returned 2 rows.user_id, user_name, user_email user_id, user_name, user_email
I basically copied the example in the link. What am I doing wrong here?
<?php
$conn_error = "Could not connect to db";
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '';
$mysql_db = 'test';
$mysqli = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_db);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "Connection successful";
}
$query = "SELECT 'user_id', 'user_name', 'user_email' FROM users";
if ($result = $mysqli->query($query)) {
printf("Select returned %d rows.", mysqli_num_rows($result));
/* fetch object array */
while ($row = $result->fetch_row()) {
printf ("%s, %s, %s\n", $row[0], $row[1], $row[2]);
}
$result->close();
} else {
echo "Nothing returned on query " .$query;
}
$mysqli->close();
?>
Upvotes: 0
Views: 929
Reputation: 350192
Remove the quotes around the field names. Change:
SELECT 'user_id', 'user_name', 'user_email' FROM users
To:
SELECT user_id, user_name, user_email FROM users
You probably intended to use back ticks, like this:
SELECT `user_id`, `user_name`, `user_email` FROM users
That would work as well, but they are only needed when you use reserved words or special characters (like spaces) in field names.
Upvotes: 3