dam php bug :/

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

dam php bug :/

Post 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 :evil:...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 :P.

thanks
(i am posting it here cos i dont really need help with it, i am just shareing my experience i guess :roll:.)
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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 :P.
User avatar
James M.
Forum Contributor
Posts: 119
Joined: Wed Mar 31, 2004 6:41 pm
Location: Tallahassee

Post 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.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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

Code: Select all

$x < sizeof($query)
or you'll have undefined index errors..
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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++)
?>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

nope, useing a loop is no diffrent then quering the database 1 query at a time :P.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
:?:
User avatar
James M.
Forum Contributor
Posts: 119
Joined: Wed Mar 31, 2004 6:41 pm
Location: Tallahassee

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