Error Trapping Database Activity

This is a discussion on Error Trapping Database Activity within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi All, Can anyone advise me how to trap errors when carrying out database inserts/updates/deletes (Queries that do ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-07-2006
Simon Harris
 
Posts: n/a
Default Error Trapping Database Activity

Hi All,

Can anyone advise me how to trap errors when carrying out database
inserts/updates/deletes (Queries that do not return any rows).

Here is my code:

$query = "some sql statement;";
mysql_query($query);

At present, this is executing (Or appears to be!) but the database is
uneffected. I guess I need some sort of return value from mySQL so I can
tell my users that the update/whatever happened OK.

Thanks!
Simon.

--
-
* Please reply to group for the benefit of all
* Found the answer to your own question? Post it!
* Get a useful reply to one of your posts?...post an answer to another one
* Search first, post later : http://www.google.co.uk/groups
* Want my email address? Ask me in a post...Cos2MuchSpamMakesUFat!

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 10157 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Reply With Quote
  #2 (permalink)  
Old 10-07-2006
Ron Barnett
 
Posts: n/a
Default Re: Error Trapping Database Activity


"Simon Harris" <too-much-spam@makes-you-fat.com> wrote in message
news:CeKVg.2797$pa.1391@newsfe2-gui.ntli.net...
> Hi All,
>
> Can anyone advise me how to trap errors when carrying out database
> inserts/updates/deletes (Queries that do not return any rows).
>
> Here is my code:
>
> $query = "some sql statement;";
> mysql_query($query);
>
> At present, this is executing (Or appears to be!) but the database is
> uneffected. I guess I need some sort of return value from mySQL so I can
> tell my users that the update/whatever happened OK.
>
> Thanks!
> Simon.
>

here is a snip from the PHP manual on the php.net website

Example 1. mysql_error() example

<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");

mysql_select_db("nonexistentdb", $link);
echo mysql_errno($link) . ": " . mysql_error($link). "\n";

mysql_select_db("kossu", $link);
mysql_query("SELECT * FROM nonexistenttable", $link);
echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
?>


If you check mysql_error() and/or mysql_erno() you should get an idea of
what is or isn't going on.
You don't actually need to pass the link to these functions unless you have
multiple databases open as it will default to the last session anyway.

Cheers

Ron


Reply With Quote
  #3 (permalink)  
Old 10-07-2006
Simon Harris
 
Posts: n/a
Default Re: Error Trapping Database Activity

> If you check mysql_error() and/or mysql_erno() you should get an idea of
> what is or isn't going on.
> You don't actually need to pass the link to these functions unless you
> have multiple databases open as it will default to the last session
> anyway.


Great, thanks! :-)

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 10158 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Reply With Quote
  #4 (permalink)  
Old 10-07-2006
ED
 
Posts: n/a
Default Re: Error Trapping Database Activity


"Simon Harris" <too-much-spam@makes-you-fat.com> wrote in message
news:CeKVg.2797$pa.1391@newsfe2-gui.ntli.net...
> Hi All,
>
> Can anyone advise me how to trap errors when carrying out database
> inserts/updates/deletes (Queries that do not return any rows).
>
> Here is my code:
>
> $query = "some sql statement;";
> mysql_query($query);
>
> At present, this is executing (Or appears to be!) but the database is
> uneffected. I guess I need some sort of return value from mySQL so I can
> tell my users that the update/whatever happened OK.
>
> Thanks!
> Simon.
>
> --
> -
> * Please reply to group for the benefit of all
> * Found the answer to your own question? Post it!
> * Get a useful reply to one of your posts?...post an answer to another one
> * Search first, post later : http://www.google.co.uk/groups
> * Want my email address? Ask me in a post...Cos2MuchSpamMakesUFat!
>
> --------------------------------------------------------------------------------
> I am using the free version of SPAMfighter for private users.
> It has removed 10157 spam emails to date.
> Paying users do not have this message in their emails.
> Try SPAMfighter for free now!
>
>


Hi Simon,

mysql_query will return FASLE on error, so if you just need a simple check
to see if the query suceeded something like this should do it

if (mysql_query($query)) {
//OK
} else {
//handle error
}

cheers
ED


Reply With Quote
  #5 (permalink)  
Old 10-08-2006
Gleep
 
Posts: n/a
Default Re: Error Trapping Database Activity

On Sat, 07 Oct 2006 09:15:14 GMT, "Simon Harris" <too-much-spam@makes-you-fat.com> wrote:

>Hi All,
>
>Can anyone advise me how to trap errors when carrying out database
>inserts/updates/deletes (Queries that do not return any rows).
>
>Here is my code:
>
> $query = "some sql statement;";
> mysql_query($query);
>
>At present, this is executing (Or appears to be!) but the database is
>uneffected. I guess I need some sort of return value from mySQL so I can
>tell my users that the update/whatever happened OK.
>
>Thanks!
>Simon.



$sql = "SELECT * FROM table";
$qry = mysql_query($sq) or die(mysql_error());
if( mysql_num_rows($qry)==0 )
echo " No data fool";
else{
while( $row=mysql_fetch_assoc($qry) ){
$someData = stripslashes($row['whatever']);
echo "$someData<br />\n";
}
}



Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:31 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0