Jump to content

can connect to DB via PHP, but MySQL data won't display


pi865

Recommended Posts

Hi.  I couldn't find my exact problem in a Search.  I hope someone can help.

 

I'm doing a school project where I need to build a rudimentary 'blog' from scratch using PHP and MySQL.  The first step is to get some data from a DB to display using PHP.  I'm choosing to do this on my own server with my own DB, which I set up and entered some data into.  Here's my issue:

 

when I write:

 

$dbh = mysql_connect("localhost" , "username" , "password, "database")

if ($dbh) {

echo "Connected.\n";

}

 

and navigate to that page, I get "Connected".  However, when I try to add any code that queries the DB, I get a blank page -- not even the connected shows up.  I have tried several methods from tutorials, so I'm pretty sure it's not a typo or capitalization error.  Anyway, here's the ideal code I'd like to use and have data display...

 

<html>

<head>

<title>Blog</title>

</head>

<body>

 

<h1>Test Blog</h1>

<br />

<br />

 

<?php

$dbh = mysql_connect("localhost" , "fake" , "fake" , "fake");

 

if ($dbh) {

$sql = "SELECT * FROM Posts ORDER BY Number";  /*Problems begin here*/

$query = mysql_query($dbh, $sql);

 

if($query) {

echo "<table border=1>\n";

echo "<tr><th>Number</th><th>Title</th></tr>\n";

 

$rows = mysql_num_rows($query);

 

for ($i = 0; $i < $rows; $i++) {

$feed = mysql_fetch_assoc($query);

$id = $feed['Number']

$title = $feed['Title'];

 

echo "<tr><td>$Number</td>";

echo "<td>$Title</td>";

echo "<td>$Content</td></tr>";

echo "</table>\n";

}

?>

</body>

</html>

 

There are more efficient ways to do this I'm sure, but it's the code our prof is recommending.

If someone could please tell me what I'm doing wrong and why I can connect to the db yet queried data will not echo or print on a page for me, I'd really appreciate it.  P.S. when I add code beyond the simple mysql_connect, the source code also outputs nothing.

 

Thanks,

Paris

 

P.P.S.

 

When viewing the page in Firefox with Firebug plugin turned on, I get this weird error:

 

            this._pwmgr.log("onStateChange accepted: req = " +

                            (aRequest ?  aRequest.name : "(null)") +

                            ", flags = 0x" + aStateFlags.toString(16));

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.