【问题标题】:Fetching data from database using PHP使用PHP从数据库中获取数据
【发布时间】:2013-07-11 06:02:16
【问题描述】:

我尝试从elective_mgmt 数据库的优先级表中获取数据。源代码如下:

<?php
    $connect = mysql_connect("localhost","root","");
    mysql_select_db("elective_mgmt",$connect);
    $result = mysql_query($con,"SELECT * FROM priority");
        echo "<table border='1'>
`<tr>
<th>Name</th>
<th>Roll</th>
<th>Email</th>
<th>Priorityone</th>
<th>Prioritytwo</th>
<th>Prioritythree</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['Roll'] . "</td>";
  echo "<td>" . $row['Email']. "</td>";
  echo "<td>" . $row['Priorityone']."</td>";
  echo "<td" . $row['Prioritytwo']."</td>";
  echo "<td" . $row['Prioritythree']."</td>"; 
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

 ?>

当我运行它时,它显示如下:

Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\Elective_management\admin_view.php on line 5

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Elective_management\admin_view.php on line 15
Name    Roll    Email   Priorityone Prioritytwo Prioritythree

Warning: mysql_close() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Elective_management\admin_view.php on line 28
?>

我不知道。请帮帮我。

【问题讨论】:

标签: php fetch dta


【解决方案1】:

您对mysql_query 的参数顺序不正确。首先是查询,然后是连接。

mysql_query("SELECT * FROM priority", $connect);

【讨论】:

    【解决方案2】:

    您提供了错误的连接。应该是这样的

     $result = mysql_query("SELECT * FROM priority",$connect );
    

    【讨论】:

      【解决方案3】:

      你不应该需要连接变量,因为你刚刚连接。你应该可以输入

      $result = mysql_query("SELECT * FROM priority");
      

      让它正常工作

      【讨论】:

        【解决方案4】:

        1 -

        mysql_close($con);
        

        这里你没有 $con 变量,所以它是空的,为什么会出现这个错误

        Warning: mysql_close() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Elective_management\admin_view.php on line 28 
        

        修复: 改变

        mysql_close($con);
        

        到

        mysql_close($connect);
        

        2 -

        $row = mysql_fetch_array($result)`
        

        在这里你指向$result,在$result你有$con = Null所以这个错误出现

        Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Elective_management\admin_view.php on line 15
        

        修复:

        将在您修复第三步时修复


        3 -

        $result = mysql_query($con,"SELECT * FROM priority");
        

        这里你有第二个参数作为一个字符串,它不应该是一个字符串,所以这个错误会出现

        Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\Elective_management\admin_view.php on line 5
        

        修复:

        修复: 改变

        mysql_query($con,"SELECT * FROM priority");
        

        到

        mysql_query("SELECT * FROM priority",$connect)
        

        PS:如果您刚刚开始在此项目中编码
        请考虑将您的语法从 MySql_* 更改为 PDO 语法

        【讨论】:

          猜你喜欢
          • 2019-11-26
          • 1970-01-01
          • 1970-01-01
          • 2016-10-29
          • 1970-01-01
          • 2015-09-18
          • 2013-07-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多