Sun_saurya
Sun_saurya

Reputation: 35

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

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/fewatrai/public_html/includes/function.php on line 6

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/fewatrai/public_html/includes/function.php on line 7

my code is: function.php

<?php include 'connect.php'; 
function pagination($query, $per_page = 10,$page = 1, $url = '?'){ 
$query = mysqli_query("SELECT COUNT(*) as `num` FROM {$query}");
$row = mysqli_fetch_array($conn, $query);
.....
....
?>

and connect.php

<?php 
$host = "localhost";
$user= "root";
$pass= "";
$db= "fewa";
$err1 = "Couldn't connect db, try again ";
$conn = mysqli_connect($host, $user, $pass) or die($err1);
$select_db = mysqli_select_db($conn, $db);
?>

i have pagination page, where items are displayed: categoy.php

<?php 
 include_once ('includes/function.php');
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 3;
$startpoint = ($page * $limit) - $limit;
//to make pagination
 $sql = "items where subcat2_id='$subcat2_id'";
 //show records
 $query = mysqli_query($conn,"SELECT * FROM {$sql} LIMIT {$startpoint} , {$limit}");
while ($row = mysqli_fetch_assoc($query)) {
......
 <?php }?>
 <?php  echo pagination($sql,$limit,$page,"?subcat2_id=".$subcat2_id."&"); ?> 

Upvotes: 0

Views: 634

Answers (1)

Ian
Ian

Reputation: 25336

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."&"); ?> 

Upvotes: 1

Related Questions