mysqli_query() expects parameter 1 and mysqli_fetc

2019-09-22 03:56发布

问题:

This question already has an answer here:

  • mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result 32 answers

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; 

?>

回答1:

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);


标签: php mysqli