I do not understand why I think I already use this type of query and this gives me an error

0

Query generates this error:

  

Warning: mysqli_query () expects parameter 1 to be mysqli, object given   in C: \ xampp \ htdocs \ system \ my system \ my system \ consultas.php on line   90

     

Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result,   null given in C: \ xampp \ htdocs \ system \ my system \ my   sistema \ consultas.php on line 91

My code

$query=$db->query("SELECT * FROM accidente");
$result = mysqli_query($query, $db);
    while ($rows = mysqli_fetch_array($result))
    
asked by Carlos Alberto Soler Velasquez 18.06.2017 в 03:28
source

2 answers

3

The Line $query=$db->query("SELECT * FROM accidente") basically what you are doing is to execute the query passed by parameter and the return value will be a mysqli_result (if executed correctly) then assign it to your variable $query (in case there is an error it will return false)

In line $result = mysqli_query($query, $db) you execute the query in theory again, but there is an error and that is that the order of the parameters are mysqli_query($db,$query) but not your variable because if it was executed successfully the first will have a mysqli_result , in case of errors a value FALSE and not a query itself.

To solve this, you must first choose an option to perform query if you choose the first one, your code would be

$query=$db->query("SELECT * FROM accidente");
while ($rows = $query->fetch_array(MYSQLI_ASSOC)){
   print_r($rows);
}

If you choose the second way it would be.

$result = mysqli_query($db,"select * from accidente");
while ($rows = mysqli_fetch_array($result)){
   print_r($rows);
}
  

As an additional note, I suggest using sentences prepared with   extension PDO and it would not hurt to read How to avoid   SQL injection?

    
answered by 18.06.2017 / 06:42
source
0

Hello friend, I think your problem is that it is being revealed in this line

$result = mysqli_query($query, $db); 

If you look at the PHP documentation you will see the following:

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

link

I hope the answer to the solution will be helpful:

$ result = mysqli_query ($ db, $ query);

    
answered by 18.06.2017 в 03:38