尝试使用 PHP 和 MySQL 制作一个非常简单的 CMS,无法获取数据输出

发布于 2024-12-09 10:44:38 字数 965 浏览 0 评论 0原文

我试图为个人项目的网站制作一个非常简单的 CMS。这是我第一次使用 MySQL,也是我第一次使用 PHP,所以这是一次学习经历。不过,根据我问过的每个人的说法,这应该有效。然而,它输出的只是

。我已经检查了数据库和表上的所有内容,列名是正确的。我还没有尝试将任何内容添加到数据库中。我只想能够阅读,但即使是简单的概念验证也行不通。

如果重要的话,我使用 000webhost 作为我的网络托管。

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

大部分代码是从 http: //guy-lecky-thompson.suite101.com/build-a-blog-or-cms-with-php-a55246 但就像我说的,看起来应该可以工作......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜空下最亮的亮点 2024-12-16 10:44:38

您的 while 循环中有错误。

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

应该是

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

当您迭代行时,您需要使用该行,而不是指向结果集的指针。

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.

影子是时光的心 2024-12-16 10:44:38
while ($curr_row = mysql_fetch_assoc($result)) {
echo '<h1>' . $result['Title'] . '</h1>';
}

更改为:

while ($curr_row = mysql_fetch_assoc($result)) {
  echo '<h1>' . $curr_row['Title'] . '</h1>';
}
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>';
}
许仙没带伞 2024-12-16 10:44:38

您需要更改

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

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

$result 是查询返回的整个结果集的句柄。 $curr_row 是您从结果集中提取的行。

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.

赤濁 2024-12-16 10:44:38

更改

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

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

Change

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

to

echo '<h1>' . $curr_row['Title'] . '</h1>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文