未定义的变量:run_posts和警告:mysql_fetch_array() 期望参数 1 是资源,给定空值


Undefined variable: run_posts and Warning: mysql_fetch_array() expects parameter 1 to be resource, null given

 <div id="main_content"> 
<?php
 //error_reporting(0);
  include("includes/connect.php");
 $select_posts = " select * from posts ";
 $run = mysql_query($select_posts);
while($row = mysql_fetch_array($run_posts)) {
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$post_date = $row['post_date'];
$post_author = $row['post_author'];
$post_image = $row['post_image'];
$post_keywords = $row['post_keywords'];
$post_content = $row['post_content'];

?>
<h2> <?php echo $post_title; ?> </h2>
<?php } ?>
</div>

创建CMS时,我在主页上显示内容,但收到常见的错误和警告

注意:未定义的变量:在第 10 行的 C:''wamp''www''CMS''include''main_content.php 中run_posts

警告:mysql_fetch_array(( 期望参数 1 是资源,在第 10 行的 C:''wamp''www''CMS''include''main_content.php 中给出空

$run = mysql_query($select_posts);
while($row = mysql_fetch_array($run_posts)) {

您正在尝试获取$run_posts而不是$run变量

将循环更改为while($row = mysql_fetch_array($run)) {

您正在尝试从$run_posts中获取结果,但您的MySQL查询实际上是$run变量。看看错误消息,这正是它所说的。

另外,请不要再使用mysql_*,切换到mysqli_PDO

只需替换

while($row = mysql_fetch_array($run_posts)) {

while($row = mysql_fetch_array($run)) {
相关文章: