mysql_insert_id只返回0

时间:2014-04-14 06:48:44

标签: php mysql mysqli mysql-insert-id

我被困在获取mysql最后一次增加id :( 我只是尝试了mysql_insert_id,但获得0而不是最后一个增量id :( 这是数组和插入fucntion调用

$input_data = array(
                        'id' => '',
                        'booking_date' => $_POST['booking_date'],
                        'event_name' =>  $_POST['ev_name'],
                        'exb_comp_name' => $_POST['exb_comp_name'],
                        'address' =>  $_POST['address'],
                        'city' => $_POST['city'],
                        'country' => $_POST['country'],
                        'tel1' => $_POST['tel1'],
                        'tel2' => $_POST['tel2'],
                        'email' =>  $_POST['email'],
                        'package' =>  $_POST['package'],
                        'stand_no' => $_POST['stand_no'],
                        'hall_no' =>  $_POST['hall_no'],
                        'area' =>  $_POST['area'],
                        'contact_per' =>  $_POST['contact_per'],
                        'desg' => $_POST['desg'],
                        'cell_no' =>  $_POST['cell_no'],
                        'url' =>  $_POST['url'],
                      );
                      echo get_insert_id('exb_reg',$input_data);

这是功能代码

function get_insert_id($table_name, $data){
global $connection;
$value_a = '';
$value_b = '';

foreach($data as $field => $values){
    $value_a .= "`".$field."`,";
    $value_b .= "'".$values."',";
}
$value_a = substr($value_a, 0, strlen($value_a)-1);
$value_b = substr($value_b, 0, strlen($value_b)-1);
$query = "INSERT INTO `$table_name` ($value_a) VALUES ($value_b)";

$result = mysqli_query($connection,$query);

 $id = mysql_insert_id();  
return $id;

}

我在函数中尝试了echo,但结果是0 :(

1 个答案:

答案 0 :(得分:4)

您已切换数据库库。始终使用mysqli_,它与已弃用的mysql_库不兼容。