pi865 Posted December 9, 2008 Share Posted December 9, 2008 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)); Link to comment https://forums.phpfreaks.com/topic/136265-can-connect-to-db-via-php-but-mysql-data-wont-display/ Share on other sites More sharing options...
mkebkr04 Posted December 9, 2008 Share Posted December 9, 2008 Maybe you can try using the row variable such as $row['...._id'] after your echos. The word in the brackets has to be the exact word from the database. Link to comment https://forums.phpfreaks.com/topic/136265-can-connect-to-db-via-php-but-mysql-data-wont-display/#findComment-710917 Share on other sites More sharing options...
teng84 Posted December 9, 2008 Share Posted December 9, 2008 look here http://www.php.net/manual/en/function.mysql-connect.php select your DB this way http://www.php.net/manual/en/function.mysql-select-db.php Link to comment https://forums.phpfreaks.com/topic/136265-can-connect-to-db-via-php-but-mysql-data-wont-display/#findComment-710924 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.