I want to do a data lookup using the id, and then display the other data. After getting the data, save it to a json array and print it.
Then this is what I have:
<?php
include_once "conexion1.php";
if(!empty($_POST)){
if (empty($_POST['id'])) {
}else{
$id = $_POST['id'];
//Hago consulta a la base de datos
$datos = mysqli_query($conexion,"SELECT * FROM clientes WHERE id = '$id' ");
//Guardo el resultado
//Linea del error
$resultado = mysqli_query($conexion,$datos) or die(mysqli_error($conexion));
//Guardo los datos en un array
$array = mysqli_fetch_array($query);
//Guardo los datos en un json
$json = $array;
echo json_encode($json, JSON_UNESCAPED_UNICODE);
}
}
?>
Can someone spot the error? I don't understand why it says that the parameters must be string, if I have the connection variable and the query variable.
The message
expects parameter 2 to be string
literally means parameter 2 was expected to be a string .The problem is that in this line you are passing the variable as the second parameter
$datos
:The second parameter of
mysqli_query()
must be the string with the SQL statement to execute, which you did above by saving that reference$datos
precisely:Now what would correspond would be to read the resource, which you already have in
$datos
, not to invoke againmysqli_query
.Apart from that there are several things that can be improved in your code:
if
more without needdie
, by controlling the flow of errors and saving a keyerror
in the response. This is essential in client/server communications for example. If the client expects a JSON and you respond with adie
you could be breaking (or complicating) the logic in the client when handling the response.The initial error and code optimization could look like this: