Re: Solution

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Gary wrote:
> For those that were looking to see a solution

[snip]

If anybody really wants to know how to handle this, (it's not officially
supported by PHP, but is in MySQL 5.1+, and I'm not condoning it) here's
how you do it:

<?php

// enable C API support for multiple statement execution
// note the 65536 flag
$connection = mysql_connect( $host, $user , $password , true , 65536 );

// select the database
mysql_select_db( 'test' , $connection );

// line up multiple queries separated by a semicolon (;)
$query1 = "DROP TABLE IF EXISTS test_table;
           CREATE TABLE test_table(id INT);
           INSERT INTO test_table VALUES(10);
           UPDATE test_table SET id=20 WHERE id=10;";

// run the multi-query (should output bool(true) on success)
var_dump( mysql_query( $query1 , $connection ) );

// big caveat!, the connection will die so we need to reconnect..
$connection = mysql_connect( $host, $user , $password , true , 65536 );
mysql_select_db( 'test' , $connection );

// now a quick test to verify
$test_result = mysql_query( "SELECT * FROM test_table" , $connection );
$result_rows = mysql_num_rows( $test_result );
if ($result_rows == 0) {
  throw new Exception( 'we failed' );
}
for ($r=0;$r<$result_rows;$r++) {
   print_r( mysql_fetch_assoc( $test_result ) );
}
mysql_free_result( $test_result );

?>


Probably worth somebody investigating this further and seeing how it
works with mysqli etc (unsure if mysqli users mysql_connect or
mysql_real_connect at the C API level.)

Regards,

Nathan

http://dev.mysql.com/doc/refman/5.1/en/c-api-multiple-queries.html


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux