0

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'"

?>
1
  • Please do not use the root user to connect to the database through PHP, this can cause a world of hurt down the line. Commented Sep 12, 2011 at 22:48

5 Answers 5

2

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'] . "'";
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

See more examples in the documentation at uk3.php.net/manual/en/function.mysql-query.php
The formatting of the output could be a bit better, but this will get him the information he needs!
0

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.

Comments

0

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);

Comments

0

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

?>

1 Comment

And need to fix the quotes in $query ;)
0

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. :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.