Hi I am creating an hotel reservation site for a friend.I found a nice tutorial on html.it and the code runs fine from the command line, no problem. Now I need to build an interface say to insert clients's data. So I have a form.My problem is that for the site to work I need to save last_insert_id to a variable called @codCliente.Can somebody just write for me that line of code so that I can insert it in the php file that handles the form? Please don't send me to some url looking for the answer, I've tried them all. Thank you.
P.S:If you need the whole code please let me know.

5 Years
Discussion Span
Last Post by cereal

Hi thanks for your answer but did not work.You may wonder why I want to do that.The reason is because I don't know any other way. I have not understood yet how to execute MySQL queries from a web page except by using php.If you want to take time to look at this site:http://www.html.it/pag/32165/tutorial-di-esempio-database-prenotazioni-alberghiere-i/
you can probably see what I am trying to do. In the 1st part you create the DB and tables, populate the various tables etc.In the 2nd part you actually execute MySQL queries to insert client's data, reserve the room, calculate the price and supplements etc. and all is revolving around that @codCliente.I am sorry is in Italian but I am sure you can figure it out.As I said if I run everything from the command line or phpMyAdmin it works fine, but my problem is that I have to deliver a web site from which to execute the various tasks. Thank you


MySQL library doesn't support multiple queries, so if you set a MySQL variable as @codCliente it will not be considered by the subsequent query. In order to do so, you have to use MySQLi or PDO libraries.

In other words:

$q = mysql_query('SELECT LAST_INSERT_ID() INTO @codCliente');

will not work. Instead this should work:


$dbhost = 'hostname';
$dbname = 'database';
$dbuser = 'username';
$dbpass = 'password';

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", "$dbuser", "$dbpass");

# insert query
$conn->query("INSERT INTO clienti SET nominativo = 'Rossi Mario', indirizzo = 'via Manzi, 2 - 00153 Roma', telefono = '06 86123920'");

# set variable @codCliente
$conn->query('SELECT LAST_INSERT_ID() INTO @codCliente');

# retrieve @codCliente
$cod = $conn->query('SELECT @codCliente as codiceCliente')->fetch();

echo $cod['codiceCliente'];

More information:

Edited by cereal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.