user990097
user990097

Reputation: 13

Trying to make a VERY simple CMS with PHP and MySQL, can't get the data to output

I was trying to make a VERY simple CMS for a website for a personal project. It's my first time using MySQL, and one of my first using PHP, so it's kind of a learning experience. According to everybody I've asked, though, this should work. However, all it outputs is the <h1></h1>. I've checked everything on my database and table, the column names are right. I'm not trying to make anything to add to the database just yet. I just want to be able to read, and even a simple proof-of-concept isn't working.

If it matters, I'm using 000webhost for my web hosting.

<?php
$mysql_host = "****";
$mysql_database = "****";
$mysql_user = "sql_blog";
$mysql_password = "*****";

$link = mysql_connect($mysql_host, $mysql_user, $mysql_password);

if (!mysql_select_db($mysql_database, $link)) {
echo 'Database error';
exit;
}

$sql_statement = ('SELECT * FROM Blog_Entries');
$result = mysql_query($sql_statement);
while ($curr_row = mysql_fetch_assoc($result)) {
  echo '<h1>' . $result['Title'] . '</h1>';
}
mysql_free_result($result);
?>

Most of that code is copied from http://guy-lecky-thompson.suite101.com/build-a-blog-or-cms-with-php-a55246 but like I said, it looks like it should work...

Upvotes: 1

Views: 259

Answers (4)

Felipe
Felipe

Reputation: 76

while ($curr_row = mysql_fetch_assoc($result)) {
echo '<h1>' . $result['Title'] . '</h1>';
}

Change to:

while ($curr_row = mysql_fetch_assoc($result)) {
  echo '<h1>' . $curr_row['Title'] . '</h1>';
}

Upvotes: 2

craigmc
craigmc

Reputation: 468

Change

echo '<h1>' . $result['Title'] . '</h1>';

to

echo '<h1>' . $curr_row['Title'] . '</h1>';

Upvotes: 1

wmelon
wmelon

Reputation: 103

You need to change

echo '<h1>' . $result['Title'] . '</h1>';

to

echo '<h1>' . $curr_row['Title'] . '</h1>';

$result is the handle to the entire result set that your query returned. $curr_row is the row you've pulled out of the result set.

Upvotes: 1

QuantumRob
QuantumRob

Reputation: 2924

You have an error in your while loop.

 echo '<h1>' . $result['Title'] . '</h1>';

Should be

 echo '<h1>' . $curr_row['Title'] . '</h1>';

When you iterate through the rows you need to use the row, not the pointer to the resultset.

Upvotes: 3

Related Questions