mysqli_query() expects parameter 1 and mysqli_fetc

2019-09-22 04:05发布

I got this error when I run my php file

  1. Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\ims\graphdata.php on line 7

  2. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\ims\graphdata.php on line 15

This is my php code:

<?php

$sql = "SELECT YEAR(borrowDate) as year_val, MONTH(borrowDate) as month_val ,COUNT(*) as total FROM itemrecord GROUP BY YEAR(borrowDate), MONTH(borrowDate);";

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

//start the json data in the format Google Chart js/API expects to receive it
$data = array('cols' => array(array('label' => 'Year', 'type' => 'number'),
                              array('label' => 'Month', 'type' => 'number'),
                              array('label' => 'Total', 'type' => 'number')),
              'rows' => array());

while($row = mysqli_fetch_array($result)){
  array_push($data['rows'], array('c' => array(
  array('v' => $row['Year']),
  array('v' => $row['Month']),
  array('v' => $row['Total']))));
  }  

$jsonData = json_encode($data);  
echo $jsonData; 

?>

标签: php mysqli
1条回答
乱世女痞
2楼-- · 2019-09-22 04:34

You are using the mysqli_query function wrong. See http://php.net/manual/en/mysqli.query.php

In your case you are using procedural style, but don' provide the link variable in the correct position. Either add it as the first parameter oder use OO style.

Edit: Example for procedural style:

$result=mysqli_query($conn,$sql);
查看更多
登录 后发表回答