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: -
$result = mysql_query('sql query string');
-
if (!$result) echo mysql_error();
-
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? -
$result1 = mysql_query('SELECT ...');
-
if (!$result1) {
-
echo mysql_error();
-
exit(0);
-
}
-
-
$result2 = mysql_query('UPDATE ... with data supplied from $result1');
-
if (!$result2) {
-
echo mysql_error();
-
exit(0);
-
}
-
-
$result3 = mysql_query('INSERT / UPDATE / DELETE ... with data
-
supplied from $result1');
-
if (!$result3) {
-
echo mysql_error();
-
exit(0);
-
}
-
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. 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:
-
$result = mysql_query('sql query string');
-
if (!$result) echo mysql_error();
-
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. -
$result1 = mysql_query('SELECT ...');
-
if (!$result1) {
-
echo mysql_error();
-
exit(0);
-
}
-
$result2 = mysql_query('UPDATE ... with data supplied from $result1');
-
if (!$result2) {
-
echo mysql_error();
-
exit(0);
-
}
-
$result3 = mysql_query('INSERT / UPDATE / DELETE ... with data
-
supplied from $result1');
-
if (!$result3) {
-
echo mysql_error();
-
exit(0);
-
}
-
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
==================
> -
-
>$result = mysql_query('sql query string');
- if (!$result) echo mysql_error();
-
-
-
> 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.
> - $result1 = mysql_query('SELECT ...');
- if (!$result1) {
-
echo mysql_error();
-
exit(0);
- }
- $result2 = mysql_query('UPDATE ... with data supplied from $result1');
- if (!$result2) {
-
echo mysql_error();
-
exit(0);
- }
- $result3 = mysql_query('INSERT / UPDATE / DELETE ... with data
- supplied from $result1');
- if (!$result3) {
-
echo mysql_error();
-
exit(0);
- }
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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?
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |