skip to Main Content

I am trying to execute the following.

public function result_multi($sql) {

    try {
        mysqli_multi_query($this->db_connection,$sql);
        $result=mysqli_store_result($this->db_connection);
        $row=mysqli_fetch_row($result);
    }

$b=new DB;
$sql="INSERT INTO payout (mid, type, datetime) VALUES ('0','BC', 
NOW());SELECT LAST_INSERT_ID();";
$b->result_multi($sql);

$row returns NULL and $result return FALSE, however when I execute the SQL query in phpmyadmin it works.

2

Answers


  1. When it comes to fetching the results of a multi query, the mysqli_multi_query documentation states :

    To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().

    So you would need to call mysqli_next_result once to access the results of the second query, and then mysqli_store_result/mysqli_fetch_row to fetch the id.

    But in your use case, it seems like a simpler way to proceed is to use mysqli_insert_id method, like :

    my $conn = $this->db_connection;
    $sql = "INSERT INTO payout (mid, type, datetime) VALUES ('0','BC', NOW())";
    if (mysqli_query($conn, $sql)) {
        $last_id = mysqli_insert_id($conn);
        echo "Record inserted with ID " . $last_id;
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    

    PS : you should also check for errors after opening the connection to the database.

    Login or Signup to reply.
  2. I need more code for this, because you used the multi sql so must have more query.

    Try this code if only one query sql :

    public function result($sql) {
    
        try {
            mysqli_query($this->db_connection,$sql);
            $result=mysqli_store_result($this->db_connection);
            $row=mysqli_fetch_row($result);
        }
    
    $b=new DB;
    $sql="INSERT INTO payout (mid, type, datetime) VALUES ('0','BC', 
    NOW());SELECT LAST_INSERT_ID();";
    $b->result($sql);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search