doufan2541 2013-12-18 11:15
浏览 57
已采纳

使用PHP显示MySQL数据

I have recently started programing for fun so sorry if the resolution to this problem is something simple.

I have a mysql table that has customer sales tickets stored in it. What I am trying to do is get it so that you can refine the tickets shown by a drop down list. I am able to populate the drop down list from the mysql table but when I click submit to filter out and display by the customer name selected. I get the following error:-

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocsesult.php on line 16

Below are the two PHP pages that i use. The first page is where the drop down is populated.

<?php
//db connection 
mysql_connect("********","********","********");
mysql_select_db("salesys");

//query
$sql=mysql_query("SELECT DISTINCT CustomerName FROM ticket ORDER BY CustomerName ASC");
if(mysql_num_rows($sql)){

$select= '<select name="select">';  
while($rs=mysql_fetch_array($sql)){
      $select.='<option value="'.$rs['CustomerName'].'">'.$rs['CustomerName'].'</option>';
  }
}

$select.='</select>';

?>  

<form name="input" action="result.php" method="POST">
<?php echo $select; ?>
<input type="submit" value="Submit">
</form>

And below is the second page.

<?php
$customerName = $_POST['select'];
echo "Showing Results For: ", $customerName;
echo "<BR>";
echo "<BR>";

$con=mysqli_connect("localhost","root","********","********");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM ticket WHERE CustomerName = $customerName");

while($row = mysqli_fetch_array($result))
  {
  echo $row['TicketID'] . " | " . $row['CustomerName'] . " | " . $row['BriefDesc'];
  echo "<br>";
    echo "<br>";
  }

mysqli_close($con);
?>

Any help would much be appreciated.

  • 写回答

2条回答 默认 最新

  • douzhuiqiu4923 2013-12-18 11:37
    关注

    You have to check the query is correct before while($row = mysqli_fetch_array($result)) to help you to debug your code.

    You can check if $result is valid or not. See below that simple code (to use only for debug purpose, because it's not safe to display mysql errors in production )

    In you case, the error is probably than $customerName is a string, and have to be surrounded by quotes

    $customerName = '"'.mysqli_escape_string($con, $customerName).'"';
    $result = mysqli_query($con, "SELECT * FROM ticket WHERE CustomerName = $customerName");
    if (!$result)
    {
      echo mysqli_error(); 
    }
    

    EDIT: in production, you should not display errors unless some required conditions validated. If your site has a login system, you can display errors only if you (and not anyone else) is logged in.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么