473,396 Members | 2,158 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,396 software developers and data experts.

mysql, classes, exceptions and error handling


This mysql_error doesnt give me an error below, but when I run it at the
global scope it does give me errors. I went ahead and checked for
mysql_error(), but normally I dont have to, why?

public function runQuery() {

try {
/// Use mysql_error() to check for errors
if (!empty($this->build_query)) {
$this->sql_result = mysql_query($this->build_query);

$this->lastinsertid = mysql_insert_id();
$err = trim(mysql_error());
if (!empty($err)) {
throw new Exception("".$err);
} // end of if - check error

} // end of if //

} catch(Exception $e) {

$this->error_message = "ERR: ".$e->getMessage();

if ($this->verbose == 1)
echo $this->error_message;

} /// end of try - catch //

return $this->error_message;
} // end of the method //
--
Ramza from Atlanta
http://www.newspiritcompany.com
Sep 19 '05 #1
2 1678
>From your post, I presume you must be using classes... It seems from my
limited knowledge that the problem is related to the scope of your
mysql_* function rather that a problem with it. If you are not
declaring the connection object in your function, it will not be
available to give an error...
Remember that you must reference your connection object throughout your
class..
For a similar class, I've done this:

//(connecting to mysql)
$this->dbConnection = mysql_connect($this->host, $this->user,
$this->password);

//Later on the "runQuery" function:
$this->sql_result = mysql_query($this->$sql,$this->dbConnection) ;

//Finally the error handling uses:
mysql_error($this->dbConnection);

Hope this helps...
--
Bugz
http://bugz.doutromundo.com

Sep 20 '05 #2
Ramza Brown wrote:

This mysql_error doesnt give me an error below, but when I run it at the
global scope it does give me errors. I went ahead and checked for
mysql_error(), but normally I dont have to, why?

public function runQuery() {

try {
/// Use mysql_error() to check for errors
if (!empty($this->build_query)) {
$this->sql_result = mysql_query($this->build_query);

$this->lastinsertid = mysql_insert_id();
$err = trim(mysql_error());
if (!empty($err)) {
throw new Exception("".$err);
} // end of if - check error

} // end of if //

} catch(Exception $e) {

$this->error_message = "ERR: ".$e->getMessage();

if ($this->verbose == 1)
echo $this->error_message;

} /// end of try - catch //

return $this->error_message;
} // end of the method //


Well, let's see here.

First of all - you should check the response of mysql_query() (in
$this->sql_result in your code) to determine if you have an error. And
you need to do that before you call mysql_insert_id().

Only call mysql_insert_id() if it is indeed an insert.

And you should ALWAYS check the response of ANY mysql call to see if it
worked!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: kelvSYC | last post by:
What I want to do is to read a 32-bit unsigned integer (let's call that u32) in little-endian form from an fstream. Would it be better if my function went like this: // returns false if an...
59
by: kk_oop | last post by:
Hi. I wanted to use exceptions to handle error conditions in my code. I think doing that is useful, as it helps to separate "go" paths from error paths. However, a coding guideline has been...
10
by: Cool Guy | last post by:
Consider: void Start() { if (!TryToDoSomething()) ShowErrorMessage(); }
10
by: Razzie | last post by:
Hi all, The title of this post may sound a bit weird, but I was wondering about the following nonetheless. I have a class libray containing, say, 4 classes: A, B, C, D. Class A somehow has a...
6
by: Tappy Tibbons | last post by:
I do not know exactly how to explain what I am asking for, but here goes... Say I have a simple set of classes: ========= Public Class clsPerson Public FirstName As String Public LastName As...
4
by: Peter Aitken | last post by:
It seems that some classes have their own exception handling built in. For example if I try to use FileStream to open a file that does not exist, my Try...Catch block seems to be ignored and...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
8
by: RichardOnRails | last post by:
I have a Stack class that works fine. In particular, when it encounters an error, it cout's a msg and exits. However, I'd like to change it to report the error and continue with dummy data as...
5
by: adam.timberlake | last post by:
I've just finished reading the article below which goes into some depth about exceptions. The article was rather lucid and so I understand how to implement it all, the thing I'm having trouble with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.