Muhammad Hamza Younas
Muhammad Hamza Younas

Reputation: 508

mysql_insert_id returning only 0

i am stuck to get mysql last increment id :( i just tried mysql_insert_id but getting 0 instead of last increment id :( Here is the array and insert fucntion call

$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);

And here is the function code

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;

}

i tried echo in function but at the end result is 0 :(

Upvotes: 0

Views: 91

Answers (1)

Quentin
Quentin

Reputation: 943579

You've switched database libraries. Use mysqli_ throughout, it isn't compatible with the deprecated mysql_ library.

Upvotes: 4

Related Questions