Link to home
Start Free TrialLog in
Avatar of edo pratama
edo pratama

asked on

A PHP Error was encountered Severity: Warning Message: mysqli_query() expects at least 2 parameters, 1 given Filename: models/m_peminjaman.php Line Number: 7 and 8

i have A PHP Error was encountered
Severity: Warning

Message: mysqli_query() expects at least 2 parameters, 1 given

Filename: models/m_peminjaman.php

Line Number: 7 and 8

This Scipt:
class M_Peminjaman extends CI_Model{
    private $table="transaksi";
   
    function nootomatis(){
        $today=date('Ymd');
        $query=mysqli_query("select max(id_transaksi) from transaksi where id_transaksi like '$today%'");
        $data=mysqli_fetch_array($query);
        $lastNoFaktur=$data['last'];
       
        $lastNoUrut=substr($lastNoFaktur,8,3);
       
        $nextNoUrut=$lastNoUrut+1;
       
        $nextNoTransaksi=$today.sprintf('%03s',$nextNoUrut);
       
        return $nextNoTransaksi;
    }
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Looks like someone converted from mysql to mysqli module by search&replace.

There's more to do than adding an "i", for example mysqli_query, well, as the warning message tells you, needs two parameters. What was an optional 2nd parameter of mysql_query is now a non optional parameter of the procedural style mysqli_query.

You'll do a mysqli_connect() somewhere else in your code, that's where you'll get the necessary mysqli $link parameter.

Bye, Olaf.
See Example #2 Procedural style on this page http://php.net/manual/en/mysqli-result.fetch-array.php for details.
check in this line here are mistake

$query=mysqli_query("select max(id_transaksi) from transaksi where id_transaksi like '$today%'");
$data=mysqli_fetch_array($query);

Open in new window


change it like this

$query=mysqli_query($conn"select max(id_transaksi) from transaksi where id_transaksi like '$today%'");
        $data=mysqli_fetch_array($query);
you are missing connection 

Open in new window

The error is telling you everything you need to know - you are sending 1 parameter to a function that requires 2
When you use the procedural mysqli calls you have to include your connection resource
mysqli_query($link, $query);

Open in new window

http://php.net/manual/en/mysqli.query.php

When you are using the object oriented version you can call it with just one
$mysqli = new mysqli('host','user','password','db');
$mysqli->query($query);

Open in new window


In your code however, you are not showing us where you create the connection to the database.

You will need to pass this as a parameter to the nootomatis() function in order to use it - something like this
 function nootomatis($conn){
        $today=date('Ymd');

        // THIS IS THE RETURN VALUE. DEFAULT TO FALSE.
        $nextNoTransaksi = false;

        // USE MEANINGFUL NAMES mysqli_query() RETURNS A mysqli_result() OBJECT OR false
        $result=mysqli_query($conn, "select max(id_transaksi) from transaksi where id_transaksi like '$today%'");

        // CHECK THE $result
        if ($result) {

          // PROCEDURAL VERSION - MUST INCLUDE $result FOR IT TO WORK
          $data=mysqli_fetch_array($result, $query);
          $lastNoFaktur=$data['last'];
       
          $lastNoUrut=substr($lastNoFaktur,8,3);
       
          $nextNoUrut=$lastNoUrut+1;
       
          $nextNoTransaksi=$today.sprintf('%03s',$nextNoUrut);
        }
 
        return $nextNoTransaksi;
    } 

Open in new window


To call it you would do something like
// ASSUMES $conn IS IN SCOPE AND CONNECTED TO DB
$peminjaman = new M_Peminjaman(); 
$result = $peminjaman->nootomatis($conn);

Open in new window


Having said all that it appears your class is extending an existing Model class
class M_Peminjaman extends CI_Model{
    private $table="transaksi";

Open in new window

This suggests that the parent might already support DB connection code. The DB code you have in your class does not look like it belongs there, no $conn object in scope or used in the mysqli_query() - so I am guessing there is potentially more to this than we are seeing.

What has been recommended here might not be the best approach. Is it possible to post your CI_Model class source?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.