PHP SQL HELP plz

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wildbat
Forum Newbie
Posts: 5
Joined: Wed Apr 05, 2006 7:22 am

PHP SQL HELP plz

Post by wildbat »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have some trouble for syncing the SQL and PHP value 
if try running the code in multi-browsers ( say 10 ) in the same time 
somehow some way 50001, or even 50002 pop up 

anyone have any tip about it ?

Code: Select all

<? 
$conn = mysql_connect("localhost", "root", "pass");
mysql_select_db("test",$conn);
while (!$done) {
	$sql = "SELECT * FROM `T`";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	$newArray = mysql_fetch_array($result);
	$id = $newArray['id'];
	$i = $newArray['i'];
	for( $x =0; $x <= 100;$x++); // simulate some job
	if ($i < 50000) {
		$sql = 'UPDATE `T` SET `i` = `i` + 1 WHERE `id` = 1 LIMIT 1';
		// execute the SQL statement
		if (!mysql_query($sql, $conn)) 
			echo "something went wrong";	
	}else{
		$done = true;
		echo "DONE! i = ". $i;
	}
}
?>

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by wildbat on Wed Apr 05, 2006 3:38 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

by having the script update a single record you're creating a race condition among the browsers. At various points multiple updates will happen before the next selection can be performed.
wildbat
Forum Newbie
Posts: 5
Joined: Wed Apr 05, 2006 7:22 am

Post by wildbat »

any solution to this kinda of situation ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Don't do it?

It's hard to give much of a recommendation when I don't know what you're trying to accomplish.
wildbat
Forum Newbie
Posts: 5
Joined: Wed Apr 05, 2006 7:22 am

Post by wildbat »

well i am just showing the simple case here .
i am trying to fix a kinda stock simulation PHP
It have an AI call every time a user is loading the page
the AI will do random buy and sell.
however, I think the race condition kicked in and mess up the SQL.
condition is pretty much the same like here:

Code: Select all

$sql = "SELECT * FROM `T`"; 
    $result = mysql_query($sql, $conn) or die(mysql_error()); 
    $newArray = mysql_fetch_array($result);               //read in the stock data
    $id = $newArray['id']; 
    $i = $newArray['i']; 
    for( $x =0; $x <= 100;$x++);                                  // simulate some jobs/ maths/ etc
    $sql = 'UPDATE `T` SET `i` = `i` + 1 WHERE `id` = 1 LIMIT 1';
    mysql_query($sql, $conn);                                     // update the stock data
is there a lock or same kind i can use for the race?
Last edited by wildbat on Sat Apr 08, 2006 7:12 am, edited 1 time in total.
wildbat
Forum Newbie
Posts: 5
Joined: Wed Apr 05, 2006 7:22 am

Post by wildbat »

anyone have idea to work with the problrm or work around ?
Post Reply