Hi,

Let's say i have a very busy web site and runing this code. Would it produce results of someone else's selection rather than my selection($name)? For example: my code send "john" to function return results. What happens if x person sends "Dean" at same time as mine. Do i still get my result or other person's result?

Thanks

function func($name) {
  $q = "SELECT * FROM products WHERE name='$name'";
  $sql=mysql_query($q);

  if(mysql_num_rows($sql)!=0) {
     return $sql;
  }
}

$rs=func($_POST["name"]);

while($code = mysql_fetch_array($rs)) {
	echo $code[0];
	echo $code[1];
	echo $code[2];
}

Requests (depending on the server) are usually handled in separate threads so my request to blah.php and your request to blah.php at the exact same time happen simultaneously*, in a non-blocking manner. If my request somehow fails, yours wont and vice versa. And unless something catastrophic happens and by some magical mystical force I corrupt memory that is the only way my data could overwrite yours (read wont happen).


*that's a whole other discussion I'm not going to get into here

Now, database queries are a wee bit different because they can be blocking (my query can block an incoming query), once again, depending on the server and in MySQL's case the database engine used. This is why transactions and the idea of atomicity are important. Note: MyISAM (The default engine of MySQL) does not support transactions. Also, InnoDB uses row-locking for reads/writes instead of table-locking. So if I'm doing a SELECT on a table with MyISAM the table will be unavailable until that SELECT is finished. Whereas with InnoDB it will act in sort of a cascade effect so as soon as I'm done reading a row it will become freed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.