mysql_fetch_array()上的警告

时间:2013-09-23 04:18:46

标签: php mysql syntax-error

很抱歉浪费你的时间,但我正在尝试将数据库表中的数据存储到数组中并显示在表中。我一直得到同样的错误。我已经更改了"'并删除了变量。我还是得到了

  

警告:mysql_fetch_array():提供的参数不是第41行/home/a3656574/public_html/Home.php中的有效MySQL结果资源

<?php
if ($_POST['search_projects']){

    $con= mysql_connect("host","username","password","a3656574_opacmin") or die ('Error: ' . mysql_error()); 
    $sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
    $result= mysql_query($sql); 

    while($row= mysql_fetch_array($result))
    { 
        $Date =$row['AccessDate']; 
        $Key=$row['keyWord']; 
        $Count=$row['count']; 

        echo "<tr>"; 
        echo "<td>" .$Date ."</td> ". " <td>" . $Key.  " </td>" . " <td>" . $Count.  " </td>"; 
        echo "</tr>";  

    }
}
?>

我不知道如何解决这个问题。有人可以帮忙吗?

4 个答案:

答案 0 :(得分:3)

Mysql连接函数接收3个参数(mysql_server,mysql_user,mysql_password)

你应该使用mysql_select_db(database,connection_resource_id)选择数据库;

同时确保您的凭证

尝试:

$con= mysql_connect("host","username","password");
mysql_select_db("a3656574_opacmin",$con);

答案 1 :(得分:0)

请检查您的数据库凭据,然后尝试:

<?php
  if ($_POST['search_projects'] && !empty($_POST['search']))
  {
    $con= mysql_connect("host.com","opacmin","password","opacmin") or die ('Error: ' . mysql_error()); 

    $sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
    $result= mysql_query($sql); 

    while($row= mysql_fetch_array($result))
    { 
      $Date =$row['AccessDate']; 
      $Key=$row['keyWord']; 
      $Count=$row['count']; 

      echo "<tr>"; 
      echo "<td>" .$Date ."</td> ". " <td>" . $Key.  " </td>" . " <td>" . $Count.  " </td>"; 
      echo "</tr>";  
    }
  }
?>

答案 2 :(得分:0)

0)首先,我建议您开始使用PDO而不是mysql_connect(和朋友),因为后者已被弃用。有一些教程可以帮助您开始此迁移here

1)我不确定你期望mysql_connect()为你做的第四个参数是什么。根据PHP文档,这应该是一个布尔值:

mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false...

2)在进入mysql_fetch_array()之前检查错误情况:

$sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
$result= mysql_query($sql); 

if (! $result) {
    // use something like mysql_error() to find out why it failed, log it, etc.
} else {
    while( ... ) { ... }
}

答案 3 :(得分:0)

首先,我建议您开始使用“mysqli”。您可以查找此here

然后您应首先检查您的凭据是否正确。如果这是正确的到phpmyadmin并尝试你的查询,看看它是否正常工作。也许你错过了什么。祝你好运。