How to fix "mysqli_query() expects parameter 2 to be string" error

Asked 11 months ago 279 views 1 answer Modified 1 days ago
24

I'm getting this error when trying to execute a database query:

"mysqli_query() expects parameter 2 to be string"

Here's my code:

$query = "SELECT * FROM table_name WHERE column = '" . $query . "'";
// Preparation
$query = $conn->prepare($query);
// Get result
$result = mysqli_query($conn, $query);
if ($result->num_rows > 0) {
    $slugResult = $result->fetch_all();
    // My PHP code here
}

What's wrong with this approach?

shama-naaz
50 reputation

1 Answer

0

mysqli_query() expects parameter 2 to be string

mysqli_query(Parameter 1, Parameter 2);

In mysqli_query() parameter 2 shuold be string.

 

Problem here:

Your var $query does not contain a string.

 

Solution:

Store in your var $query a string like:

$query = "SELECT * FROM table_name WHERE column = '" . $query . "'";

then use var $query to fetch result,

 

Your code should be like the following:

$query = "SELECT * FROM table_name WHERE column = '" . $query . "'";
 
// Get query;
$stmt = mysqli_query($conn, $query);
// Check query;
if ($stmt->num_rows > 0) {
// Get result;
  $result = $stmt->fetch_all();
// Your PHP code here
}
imran-nadwi
answered 11 months ago
You can use Markdown to format your answer.
Last edited: 2 months ago
Preview: