Page 1 of 1
dam php bug :/
Posted: Fri Jul 09, 2004 2:54 am
by qads
I was improving my mysql class, plan was to batch process the queries in one query so i can cut down on the load, made few functions to format the query, and the data etc, everything working great, i just pass the query from the pages, and it keeps holding insert, update and delete queries till the last min....then i found out that you cant use more then one query with php

...i.e.
Code: Select all
<?php
$query = "insert into table (f1,f2,f3) values (1,1,1);\r\n
insert into table (f1,f2,f3) values (2,2,2);";
mysql_query($query);
?>
it just wont do it, but there is some good news, my class will work fine with php5, gona have to use [php_man]mysqli_multi_query()[/php_man] for it.
anyone found a way around it? i dont mean useing a loop to do it either, thats still no help for the load

.
thanks
(i am posting it here cos i dont really need help with it, i am just shareing my experience i guess

.)
Posted: Fri Jul 09, 2004 4:05 am
by Wayne
not sure if its a good way around .... but how about writing the queries to a file and then kicking off a command line mysql command to process the file? Its a work around, not sure if its what your looking for though.
Posted: Fri Jul 09, 2004 4:47 am
by qads
its a good tip, but i use this class for major projects, so createing a file, then reading it is not always the best option, but its a good tip, i will try it out just for the hell of it

.
Posted: Sun Jul 11, 2004 2:02 am
by James M.
I dont know if this is what you are lookig for but you can always try this:
Code: Select all
<?php
$link=mysql_connect();
if(!$link)
{
die("Could not connect to MySQL");
}
mysql_select_db('database', $link);
$query[0]="Some query";
$query[1]="Some other query"
....
for($x=0; $x<='how ever many querys you have minus 1'; $x++)
{
$result=mysql_query($query[$x], $link);
if(!$result)
{
echo"Error in query $x: ".mysql_error()."<br><br><br>";
}
else
{
echo"Query $x was successful!<br>";
}
}
mysql_close($link);
?>
you can just modify whats in the for statement to what ever you want.
Posted: Sun Jul 11, 2004 3:03 am
by PrObLeM
for the 'how ever many querys you have minus 1' just do count($query) that will give you the number of elements in an array
http://us2.php.net/count
Posted: Sun Jul 11, 2004 3:20 am
by feyd
continuing on PrObLeM's line of thought, if you use count/sizeof, make sure you set the condition of the while loop to
or you'll have undefined index errors..
Posted: Sun Jul 11, 2004 8:24 am
by Bill H
Instead of:
Code: Select all
<?php
for($x=0; $x<='how ever many querys you have minus 1'; $x++)
?>
why not:
Code: Select all
<?php
for($x=0; $x<'how ever many querys you have'; $x++)
?>
Posted: Sun Jul 11, 2004 9:42 am
by qads
nope, useing a loop is no diffrent then quering the database 1 query at a time

.
Posted: Sun Jul 11, 2004 11:20 am
by Bill H
That wasn't my point. My post was a question to James M.
a. calculate the number of queries
b. subtract one from that number
c. run a for loop with <=
or
a. calculate the number of queries
b. run a for loop with <
Elementary coding practice question.

Posted: Sun Jul 11, 2004 3:31 pm
by James M.
I forgot about the count function when i created that for my own script, im not exactly a veteran to php, ive still got plenty to learn.