我在「SELECT * FROM table LIMIT'中插入$ q時出現問題」$ q。「'」;在ajax和mysql中的限制
HTML代碼: 保存在「ajax.php」文件中的html代碼。
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
<meta charset="utf-8">
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
和PHP代碼: 與HTML代碼相同的目錄保存在 「getuser.php」 這個PHP代碼。
<?php
$q = intval($_GET['q']);
$m = 2;
$con = mysqli_connect('localhost','root','','net');
mysqli_select_db($con,"members");
$sql="SELECT * FROM members LIMIT '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['family'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
輸出顯示
這messege:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\php2\admin\test\getuser.php on line 30
,但是當我插入 「SELECT * FROM成員限制2」 這工作順利
讓我們擺脫明顯的第一個。試試這一行,而不是你所擁有的:'$ sql =「SELECT * FROM members LIMIT」。$ q;'。 – Sebas