473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

PHP + MySQL + Queries that depend on each other?

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
2 2192
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zaphod Beeblebrox | last post by:
As much of this question relates to mysql, it may be OT? I'm trying to make a search engine for a reasonably complex database that was originally developed by someone else in Access. I've ported...
10
by: Marcus | last post by:
Quick question on how I setup my mysql database(s)... If my setup is such that I have multiple clients, and each client gets 10 tables to store their data, is it better for performance if I put...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
7
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
4
by: Federico | last post by:
Hi everybody, I'm evaluating the possibility of using MySQL 5.0 as a database backend for an application we are doing and I'd like to have hardware requirements rough estimates and/or real world...
13
by: Ciaran | last post by:
Hi All, Is it faster to have mySql look up as much data as possible in one complex query or to have php do all the complex processing and submit lots of simple queries to the mysql database? ...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.