Page 1 of 1

How to put it all in mysql_query?

Posted: Sat May 15, 2004 6:42 am
by yosuke_
Hi!
This code will create database and then tables.

Code: Select all

<?php
mysql_connect("localhost","root","q48cur3");
mysql_query("create database data");
mysql_query("use data");
mysql_query("create table users(username char(40))");
?>
Can I somehow write it more shorter like all these mysql commands will execute only one mysql_query, not all 3 of them? something like this:

Code: Select all

<?php
mysql_connect("localhost","root","q48cur3");
mysql_query("create database data; use data; create table users(username char(40))");
?>
I try to put ; in the end of each command, but It dosnt seem to work! Any ideas? Thank you

Posted: Sat May 15, 2004 11:04 am
by markl999
"From version 4.1, MySQL supports the execution of multiple statements specified in a single query string."
http://dev.mysql.com/doc/mysql/en/C_API ... eries.html

This is talking about a c api, and as PHP is written in c then you might be able to do multiple queries in one 'go' if you are using MySQL >= 4.1
I'm not using 4.1 so i can't test it, but you definetly can't do it with MySQL <= 4.1 since the function returns a code to indicate if it was successful or not, with multiple statements it wouldn't know which one of the statements failed.

I'd be interested to know if it works using MySQL >= 4.1 though or if it doesn't matter as the PHP c code doesn't take into account the new MySQL 4.1 functionality :o