M92
M92

Reputation: 1

Help displaying data from a MySQL database

I want to display some basic data from a MySQL database. Here's the current code I have, but it doesn't seem to work. Could someone please explain why this doesn't work and offer a solution? Thanks!

<?php


mysql_connect("localhost", "root", "");
mysql_select_db("cede") or die("Couldn't find database");

$result = 'SELECT * FROM 'users' ORDER BY 'DATE' DESC LIMIT 8';

echo = "'$result'"

?>

Upvotes: 0

Views: 83

Answers (5)

GolezTrol
GolezTrol

Reputation: 116100

You should escape the ' in your string, because you use them to open and close your string too. The syntax highlighter actually tells you that you are wrong ('users' and 'DATE' are black instead of maroon). :)

Please see the PHP.net documentation about strings:

After that, you'll need to further process $result. It is just a resource pointer and cannot be echoed that way. But that's a second step. :)

Upvotes: 0

Tamer Shlash
Tamer Shlash

Reputation: 9523

You should query your string and then echo the result, like this for example:

<?php


mysql_connect("localhost", "root", "");
mysql_select_db("cede") or die("Couldn't find database");

$query = 'SELECT * FROM `users` ORDER BY `DATE` DESC LIMIT 8';

$result = mysql_query($query);

echo = "'$result'";  // This may need a foreach loop

?>

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59987

After the selecting the database need

   $stmt = mysql_query("SELECT * FROM users ORDER BY DATE DESC LIMIT 8");
   while ($result = mysql_fetch_array($stmt, MYSQL_NUM))
   {
      var_dump($result);
   }
   mysql_free_result($stmt);

Upvotes: 0

Bojangles
Bojangles

Reputation: 101473

You forgot to query the database!

You need to use mysql_query() to retrieve data from your DB server, then loop through it with a while() loop.

Also, you can't use quotes inside quoted strings - it breaks the string, meaning you'll get a syntax error with the SELECT ... line. You don't actually need to quote database fields in queries, so the following should work fine:

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("cede") or die("Couldn't find database");

$query = 'SELECT * FROM users ORDER BY DATE DESC LIMIT 8';

$result = mysql_query($query);      // Query the database.

// Loop through each returned row
while($row = mysql_fetch_assoc($result))
{
    print_r($row);      // Prints the current row
}
?>

To show any errors that PHP reports, put these two lines at the top of your script.

error_reporting(E_ALL);
ini_set('display_errors', '1');

They will output any errors you get, making problems much easier to solve.

Upvotes: 0

user142162
user142162

Reputation:

Providing your connection and structure information is correct, the following should work for you:

<?php


mysql_connect("localhost", "root", "");
mysql_select_db("cede") or die("Couldn't find database");

$result = 'SELECT * FROM `users` ORDER BY `DATE` DESC LIMIT 8';
$query = mysql_query($result) or die("Query Error");
while($row = mysql_fetch_assoc($query))
{
   echo = "'" . $row['user'] . "'";
}
?>

Upvotes: 2

Related Questions