How to put it all in mysql_query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

How to put it all in mysql_query?

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
Post Reply