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

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历