By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,839 Members | 2,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,839 IT Pros & Developers. It's quick & easy.

PHP + MySQL + Queries that depend on each other?

P: n/a
Hi there guys,

My doubt is related to PHP and MySQL usage, and it's related to check
if performed queries are performed with success, since we know that:

mysql_query() - Return Values:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
For other type of SQL statements, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error.
Each time we perform an sql query, we can check if it was performed
with success like this:

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query('sql query string');
  2. if (!$result) echo mysql_error();
  3.  
If it fails, it will display the succeeded error.

Now that I gave an example, here are my doubts:

1. Should this procedure be done every time a query is done, or,
should we assume that 'SELECT' queries are always done with success if
sql syntax is correct?

2. If we have a lot of queries that depend on each other (See the next
example), imagine that query1 and query2 are made with success, but
query3 fails, I will have inconsistent data since, the update is made
on query2, but no delete will be made on query3 since it fails.
How can we avoid this?

Expand|Select|Wrap|Line Numbers
  1. $result1 = mysql_query('SELECT ...');
  2. if (!$result1) {
  3. echo mysql_error();
  4. exit(0);
  5. }
  6.  
  7. $result2 = mysql_query('UPDATE ... with data supplied from $result1');
  8. if (!$result2) {
  9. echo mysql_error();
  10. exit(0);
  11. }
  12.  
  13. $result3 = mysql_query('INSERT / UPDATE / DELETE ... with data
  14. supplied from $result1');
  15. if (!$result3) {
  16. echo mysql_error();
  17. exit(0);
  18. }
  19.  
Hope you guys can understand my examples, if not, I'll try to explain
in a better way.

Thanks for your attention and spent time on reading this.

Jul 28 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Joćo Morais wrote:
Hi there guys,

My doubt is related to PHP and MySQL usage, and it's related to check
if performed queries are performed with success, since we know that:

mysql_query() - Return Values:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
For other type of SQL statements, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error.

Each time we perform an sql query, we can check if it was performed
with success like this:

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query('sql query string');
  2. if (!$result) echo mysql_error();
  3.  

If it fails, it will display the succeeded error.

Now that I gave an example, here are my doubts:

1. Should this procedure be done every time a query is done, or,
should we assume that 'SELECT' queries are always done with success if
sql syntax is correct?
Never assume a MySQL call (or a call to any other external resource) is
successful. And you shouldn't display the error message itself - it
exposes internals of your site to the user. Rather, give them a generic
message and log the real message.
2. If we have a lot of queries that depend on each other (See the next
example), imagine that query1 and query2 are made with success, but
query3 fails, I will have inconsistent data since, the update is made
on query2, but no delete will be made on query3 since it fails.
How can we avoid this?
Use Innodb and transactions.
Expand|Select|Wrap|Line Numbers
  1. $result1 = mysql_query('SELECT ...');
  2. if (!$result1) {
  3.      echo mysql_error();
  4.      exit(0);
  5. }
  6. $result2 = mysql_query('UPDATE ... with data supplied from $result1');
  7. if (!$result2) {
  8.      echo mysql_error();
  9.      exit(0);
  10. }
  11. $result3 = mysql_query('INSERT / UPDATE / DELETE ... with data
  12. supplied from $result1');
  13. if (!$result3) {
  14.      echo mysql_error();
  15.      exit(0);
  16. }
  17.  

Hope you guys can understand my examples, if not, I'll try to explain
in a better way.

Thanks for your attention and spent time on reading this.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 28 '07 #2

P: n/a
>
Expand|Select|Wrap|Line Numbers
  1.         
  2.                 >$result = mysql_query('sql query string');
  3. if (!$result) echo mysql_error();
  4.  
  5.  
>
If it fails, it will display the succeeded error.

Now that I gave an example, here are my doubts:

1. Should this procedure be done every time a query is done, or,
should we assume that 'SELECT' queries are always done with success if
sql syntax is correct?
It is always possible for a MySQL query to fail due to network
errors (even if the DB is on the local host), disk errors, permission
problems, etc. Also, it's not only SYNTAX that can cause errors
in a sql query. If the table no longer matches the query (table
doesn't exist, field doesn't exist, etc.) you'll get an error.

Note that returning 0 rows in response to a select (or affecting
0 rows for a delete) is *NOT* an error.

>2. If we have a lot of queries that depend on each other (See the next
example), imagine that query1 and query2 are made with success, but
query3 fails, I will have inconsistent data since, the update is made
on query2, but no delete will be made on query3 since it fails.
How can we avoid this?
You can't. It is always possible for someone to unplug a network cable
or take down the server between queries. But you can use transactions
to be sure that all or none of the queries are committed. Check your
queries and use ROLLBACK if something fails.
>
Expand|Select|Wrap|Line Numbers
  1. $result1 = mysql_query('SELECT ...');
  2. if (!$result1) {
  3.     echo mysql_error();
  4.     exit(0);
  5. }
  6. $result2 = mysql_query('UPDATE ... with data supplied from $result1');
  7. if (!$result2) {
  8.     echo mysql_error();
  9.     exit(0);
  10. }
  11. $result3 = mysql_query('INSERT / UPDATE / DELETE ... with data
  12. supplied from $result1');
  13. if (!$result3) {
  14.     echo mysql_error();
  15.     exit(0);
  16. }

Hope you guys can understand my examples, if not, I'll try to explain
in a better way.

Thanks for your attention and spent time on reading this.

Jul 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.