简体   繁体   中英

Mysqli_insert_id returning 0 or $con not defined

I have been trying to convert my mysql to mysqli which was working before this. I am using prepared statements for the most part. I am not sure how mysqli_insert_id is to be written as the various things I have tried has resulted in 0 or $con not defined depending on the code.

Prepared statements

<?php


function db_connect() {

    // Define connection as a static variable, to avoid connecting more than once 
    static $con;

    // Try and connect to the database, if a connection has not been established yet
    if(!isset($con)) {
         // Load configuration as an array. Use the actual location of your configuration file
        $config = parse_ini_file('config.ini'); 
        $con = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
    }

    // If connection was not successful, handle the error
    if( $con === false) {
        // Handle error - notify administrator, log to a file, show an error screen, etc.
        return mysqli_connect_error(); 
    }
    return  $con;
}

function db_query($query) {
    // Connect to the database
     $con = db_connect();


    // Query the database
    $result = mysqli_query( $con,$query);

    return $result;
}

function db_error() {
     $con = db_connect();

        return mysqli_error($con);
}

function db_select($query) {
    $rows = array();
    $result = db_query($query);

    // If query failed, return `false`
    if($result === false) {
        return false;
    }

    // If query was successful, retrieve all the rows into an array
    while ($row = mysqli_fetch_assoc($result)) {
        $rows[] = $row;
    }
    return $rows;
}
function db_quote($value) {
     $con = db_connect();

        return "'" . mysqli_real_escape_string($con,$value) . "'";
}
?>

Code to INSERT and define $last_id. All fields and values are variables and a row is inserted without issues.

 $result = db_query("INSERT INTO $table($col1,$col3,$col4,$col5) VALUES ('$field','$field3','$field4','$field5')");
    if($result === false) {
     $error = db_error();
     echo $error;}
     else {
        // We successfully inserted a row into the database

    $last_id = mysqli_insert_id($con);
    echo $last_id;

I use the $last_id to populate another table to include a column for last_id.

As it stands this code displays an error that Notice: Undefined variable: con in D:\\xampp\\htdocs\\Websites\\mindia\\project.php on line 186

If I declare $con outside of any of the functions as below and change to $last_id = mysqli_insert_id($con); , I get 0 when I echo $last_id

static $con;
      if(!isset($con)) {
         // Load configuration as an array. Use the actual location of your configuration file
        $config = parse_ini_file('config.ini'); 
        $con = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
    }

I need to somehow use $last_id so I can populate another table with rows that will show $last_id - this is a separate script, if required, I will post that as well.

Any help will be greatly appreciated. Please let me know if any additional information is required.

Again I have found the answer to my own question after no response from anyone else.

Apparently its very easy to solve the problem. I looked at the code of all the prepared statements and found there to be a $con variable defined as the db_connect prepared statement.

so in order for this to work I did this

$con= db_connect();
$last_id = mysqli_insert_id($con);

Basically this defines the variable $con and allows mysqli_insert_id to use it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM