我的問題是我想要一個頁面顯示10個記錄 ,我發現這個偉大的代碼在phpjabbers:錯在PHP尋呼機代碼retrive SQL查詢
Host = "localhost"
user="root"
password=""(Nothing)
<?php
$con = mysql_connect("localhost","root","") or die("Unable to connect");
$db = mysql_select_db("test",$con) or die("Unable to select DB");
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}
$sf = ($page-1) * 10;
$sql = "SELECT * FROM students LIMIT ".$sf.",10";
$rs = mysql_query($sql,$con);
//echo $rs;
?>
<html>
<head>
<title>Pages</title>
</head>
<body>
<?php
$sql1 = "SELECT COUNT(name) FROM students";
$rs1 = mysql_query($sql1,$con);
$row1 = mysql_fetch_row($rs1);
$total = $row1[0];
$tp = ceil($total/10);
for($i = 1; $i <= $tp; $i++)
{
echo "<a href='test.php?page=".$i."'>".$i."</a> ";
}
?>
<table>
<tr>
<th>Name</th>
<th>Phone Number</th>
</tr>
<?php
while($row = mysql_fetch_assoc($rs))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['ph_no']; ?></td>
</tr>
<?php
}
?>
</table>
<?php
$sql1 = "SELECT COUNT(name) FROM students";
$rs1 = mysql_query($sql1,$con);
$row1 = mysql_fetch_row($rs1);
$total = $row1[0];
$tp = ceil($total/10);
for($i = 1; $i <= $tp; $i++)
{
echo "<a href='test.php?page=".$i."'>".$i."</a> ";
}
?>
</body>
</html>
當我取回無條件表數據像代碼:
"SELECT * FROM students LIMIT ".$sf.",10";
它的工作原理,但是當我添加一個條件,如:
"SELECT * FROM `students` where `Instructor-Email`=". $_SESSION['ID'] ." OR `Instructor-Email` IS NULL LIMIT".$sf.",10"
它不起作用。
我懷疑我的查詢語法,所以我下載了查詢生成器程序(flyspeed SQL查詢)。它內置的查詢爲:
"Select * From students Where (`Instructor-Email` = '. $_SESSION['ID'] .') Or (`Instructor-Email` Is Null) LIMIT 0,10"
,但仍然沒有工作!
你能告訴我是什麼問題嗎?衷心感謝您的努力。
感謝但如果你的意思是'',我之前做過但沒有工作:( – user2790300
你有任何錯誤消息嗎?你確定這個字段在這個表嗎?而不是在例如教師表? –
是的我確定我在其他頁面中使用了相同的查詢並且它工作正常,我認爲錯誤可能與「LIMIT在查詢中的位置,由於查詢無法正常工作而出現消息錯誤這是:消息錯誤「警告:mysql_fetch_assoc()期望參數1是資源,在第86行 – user2790300