php - Use mysql_insert_id in single query -


ok, don't know if simple in practice in theory want know.

i have single insert query in query, want extract auto_increment value reuse in same query.

for example

//values inserted in database table $a_name = $mysqli->real_escape_string($_post['a_name']); $details = $mysqli->real_escape_string($_post['details']); $display_type = $mysqli->real_escape_string($_post['display_type']); $getid = mysqli_insert_id();  //mysqli insert query $insert_row = $mysqli->query("insert articles (a_name,details,display_type,date_posted) values('$a_name','$details','$display_type$getid',current_timestamp)"); 

apparently, getting blank value(i know because mysqli_insert_id() before query, i've tried nothing has come out want. can please me on how achive this

it seems value required $display_type :$display_type + (max(id) + 1).

in order max_id you'll have query before :

$sql = "select id articles order id desc limit 1"; $result = mysqli->query($sql); $maxid = $result->fetch_array(mysqli_num);  // $maxid[0] contains value desired  // remove mysqli_insert_id() call - swap $getid ($maxid[0] + 1) // , u're go 

n.b. update name of ur primary key in query $sql.

edit :

assuming weakness of query , quick resarch did.

try replace $sql (don't forget update databasename & tablename values) :

$sql = select `auto_increment`         information_schema.tables        table_schema = 'databasename'        ,   table_name   = 'tablename'; 

that should . more info on link below :

stackoverflow : auto-inc value


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -