开发者

Help displaying data from a MySQL database

开发者 https://www.devze.com 2023-04-04 15:02 出处:网络
I want to displa开发者_运维技巧y 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 s

I want to displa开发者_运维技巧y 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'"

?>


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'] . "'";
}
?>


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.


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


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

?>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消