mysqli_fetch_assoc()期望参数1为mysqli_result

时间:2013-11-01 06:59:52

标签: php html

我正在尝试创建一个表,该表只是通过在表列状态中选择包含QLD的行来生成的。我不确定我的fetch数组是如何工作的,但我知道它会返回适合给定条件的行。

但是html会抛出错误。谁能知道我哪里出错了。

我的PHP代码

<?php
$q = intval($_GET['q']);

$con = mysqli_connect("localhost","root","","test");
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,"fabrik");
if($q="1")
{
  $sql="SELECT * FROM user WHERE state = 'QLD'";

$result = mysqli_query($con,$sql);
}


echo "<table border='1'>
<tr>
<th>Status</th>
<th>Consultant</th>
<th>Client</th>
<th>Role</th>
<th>Likelyhood</th>
<th>StartDate</th>
<th>EndDate</th>
<th>Comments</th>
</tr>";

while($row = mysqli_fetch_assoc($result))
  {
  echo "<tr>";
  echo "<td>" . $row['statustable_name_'] . "</td>";
  echo "<td>" . $row['consultant'] . "</td>";
  echo "<td>" . $row['client'] . "</td>";
  echo "<td>" . $row['role'] . "</td>";
  echo "<td>" . $row['likelyhood'] . "</td>";
  echo "<td>" . $row['startdate'] . "</td>";
  echo "<td>" . $row['enddate'] . "</td>";
  echo "<td>" . $row['comments_'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

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) // 200 to check the status is ok 4=onreadystatechange event is triggered five times (0-4), one time for each change in readyState
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","view.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select the State </option>
<option value="1">QLD</option>
<option value="2">NSW</option>
<option value="3">VIC</option>
</select>
</form>
<br>
<div id="txtHint"><b>List with selected State will be populated below</b></div>

</body>
</html>

如果有人对我的问题有所了解会很有帮助

由于

2 个答案:

答案 0 :(得分:0)

请更改此

 if($q="1") to if($q=="1")

您的查询仅在$ q值== 1时执行,并且还要确保您获得$ q值= 1始终

答案 1 :(得分:0)

这是许多人最常犯的错误,您没有检查所调用函数的返回值。当你这样做时:

$result = mysqli_query($con,$sql);

你应该检查$result是否为假。如果是,则很可能您的查询不正确

通过执行此操作来尝试检查它是否正确

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

并且别忘了改变

if($q="1")

 if($q=="1")

如果您仍然遇到相同的错误,请检查您是否有user表,其中state列拼写正确。

这应解决问题