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);
}
Warning: mysqli_query() expects at least 2 parameters, 1 given. Why?
Change it to $result = mysqli_query($conn, $query);
add the connection variable
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);
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."&"); ?>
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;
}
Related Topics
Syntax Error While Defining an Array as a Property of a Class
Insert Current Date in Datetime Format MySQL
PHP Flush That Works... Even in Nginx
Wamp/MySQL Errors Not in Correct Language
Set Session in Database in PHP
Load Data Local Infile Forbidden In... PHP
How to Properly Use While Loop in Pdo Fetchall
Undefined Class Constant 'Mysql_Attr_Init_Command' with Pdo
Strip Tags and Everything in Between
How to Fix the "Base Table or View Not Found: 1146" Error When Running 'PHP Artisan Migrate' Command
PHP Share Variable Among Different Users/Sessions
Manipulate a Url String by Adding Get Parameters
Publishing to User's Wall Without Being Online/Logged-In - Facebook Sharing Using Graph API
PHP Namespace with Dynamic Class Name
Setting Up Ssl on a Local Xampp/Apache Server
Php: Check If Variable Exist But Also If Has a Value Equal to Something