"mysqli_query() expects parameter 2 to be string" - error

#49
149
1 Answer
Question Image

I get an error "mysqli_query() expects parameter 2 to be string" when I trying to fetch data from database using mysquli.

my code is:

$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
}

Please give me a solution.


Related to:
phpmysqlimysqli_query()stringerror
0
Answer
Answer 1 of 1
# 36

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
}
1
0
0
Related Articles

If you still have a question about this, submit it in our Q&A community - Ask Question