du970294 2015-02-09 14:32
浏览 82
已采纳

Mysqli_insert_id返回0或$ con未定义

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.

  • 写回答

1条回答 默认 最新

  • dtyyrt4545 2015-02-11 20:14
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成