使用PHP从数据库中获取数据

时间:2013-07-11 06:02:17

标签: php fetch dta

我试图从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
?>

我没有任何想法。请帮帮我。

4 个答案:

答案 0 :(得分:0)

您对mysql_query的参数订单不正确。首先查询然后连接。

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

答案 1 :(得分:0)

你给错了连接。应该是这样的

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

答案 2 :(得分:0)

您刚刚连接时不需要连接变量。你应该能够输入

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

让它工作得很好

答案 3 :(得分:0)

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步时将修复


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语法