MySQLi_Query() Expects At Least 2 Parameters, 1 Given

Warning: mysqli_query() expects at least 2 parameters, 1 given. Why?

Change it to $result = mysqli_query($conn, $query); add the connection variable

Warning: mysqli_query() expects at least 2 parameters,1 given in [...]

The errors are self-explanatory, mysqli_query requires 2 parameters - the documentation is available here: https://php.net/mysqli_query

As per the docs, the first parameter is your database connection created through mysqli_connect.

Your could should read similar to:

$db = mysqli_connect(..);

// ..

$email_check = mysqli_query($db, "SELECT email FROM users WHERE email='$email'");

Also, your code is vulnerable to SQL injection, you should consider using parameters in your query with code such as:

if ($stmt = mysqli_prepare($db, "SELECT email FROM users WHERE email = ?"))
{
mysqli_stmt_bind_param($stmt, "s", $email);

mysqli_stmt_execute($stmt);

$email_check = mysqli_stmt_num_rows($stmt);
}

mysqli_query() expects at least 2 parameters, 1 given in?

The error message is quite clear. mysqli_query() requires two parameters. You only provide one. When you see an error message like this the first thing you need to do is go to the manual. If you did you would see you must provide your MySQLi link as the first parameter:

$search_query=mysqli_query($con, $search_sql);

PHP - mysqli_query() expects at least 2 parameters, 1 given in

You need to pass $conn variable to mysqli_query($query);
Following is the example that you need to follow.

mysqli_query($conn,"SELECT * FROM table");

Following that method your new code should be(for two function only, rest of the code will remain same)

function runQuery($query) {
$conn = connectDB(); // if you are using within class then $this->connectDB();
$result = mysqli_query($conn,$query);
if($result)
{
if(mysqli_num_rows($result) > 0)
{
while($row=mysqli_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
{
return $resultset;
}
}
}
}

function numRows($query) {
$conn = connectDB(); // if you are using within class then $this->connectDB();
$result = mysqli_query($query);
$rowcount = mysqli_num_rows($result);
return $rowcount;
}

Logout getting PHP Warning: mysqli_query() expects at least 2 parameters, 1 given

You should send two parameters to mysqli_query:

  1. db connection and
  2. query.

for reference

At this moment you only send db connections to this function.

mysqli code, getting error as Warning: mysqli_query() expects at least 2 parameters, 1 given & null is given

The function mysqli_query() requires two parameters. The first being an open mysql connection resource (the result of mysql_connect()). In your case $conn needs to be the first parameter.

Eg:

mysqli_query($conn, "SELECT COUNT(*) as `num` FROM {$query}");

You'll need to make $conn one of the parameters of pagination() in order for it to be available in the function.

Also, you are connected using MySQLi the OOP version, not procedural. This means you should you're better off using $conn->query() not mysqli_query($conn)

Edit:

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}

...

function pagination(mysqli $conn, $query, $per_page = 10, $page = 1, $url = '?'){ 
$result = $conn->query("SELECT COUNT(*) as `num` FROM {$query}");
while ($row = $result->fetch_assoc()) {
// Do stuff
}
...
}

...

<?php echo pagination($conn, $sql,$limit,$page,"?subcat2_id=".$subcat2_id."&"); ?> 


Related Topics



Leave a reply



Submit