Can I hide a part of the text by a "More..." link?
Function to catch all mySQL errors?
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > PHP web-programming > Function to catch all mySQL errors? 5 January 2003 06:21:30

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

Function to catch all mySQL errors?

Jeff Lewis 5 January 2003 06:21:30
 I know that mySQL errors are caught in mysql_error() and I find that
function extremely useful (kudos!). However, I have several queries in a few
scripts of mine but am wondering if anyone has written a small function that
catches errors and outputs them. What I mean is lets say I have a block of
code like so:

$result = mysql_query("SELECT­ field1, field2 FROM users WHERE userLogin =
'$authusername' AND userPassword = '$authpassword'");

Instead of a small loop under my query that is like so:

if (mysql_error()) {
echo "Error: ".mysql_error();
exit;
}

I'd like to be able to call a function that does this instead of adding this
if statement after all my queries...so...anyo­ne do something similar?

Jeff



--
PHP General Mailing List (http://www.php.net­/)
To unsubscribe, visit: http://www.php.net/­unsub.php


Add comment
Adam Plocher 5 January 2003 05:47:40 permanent link ]
 How about something like this..

function runquery($query)
{
$query = mysql_query($query)­;
if (mysql_error())
{
echo "<div style=\"color:red\"­>MySQL Error: ". mysql_error() ."</div>\n";
exit(1);
}

return $query;
}


-----Original Message-----
From: Jeff Lewis [mailto:jeff@hyrum.net]
Sent: Sat 1/4/2003 6:43 PM
To: php-general@lists.p­hp.net
Cc:
Subject: [PHP] Function to catch all mySQL errors?



I know that mySQL errors are caught in mysql_error() and I find that
function extremely useful (kudos!). However, I have several queries in a few
scripts of mine but am wondering if anyone has written a small function that
catches errors and outputs them. What I mean is lets say I have a block of
code like so:

$result = mysql_query("SELECT­ field1, field2 FROM users WHERE userLogin =
'$authusername' AND userPassword = '$authpassword'");

Instead of a small loop under my query that is like so:

if (mysql_error()) {
echo "Error: ".mysql_error();
exit;
}

I'd like to be able to call a function that does this instead of adding this
if statement after all my queries...so...anyo­ne do something similar?

Jeff



--
PHP General Mailing List (http://www.php.net­/)
To unsubscribe, visit: http://www.php.net/­unsub.php



Add comment
Michael J. Pawlowsky 5 January 2003 05:55:52 permanent link ]
 Also if you dont want ot to break you need to add the "@" operator in front


Mike


*********** REPLY SEPARATOR ***********

On 04/01/2003 at 6:47 PM Adam Plocher wrote:
This encoded message has been converted to an attachment.>
How about something like this..>
function runquery($query)>{>­ $query = mysql_query($query)­;> if (mysql_error())> {> echo "<div style=\"color:red\"­>MySQL Error: ". mysql_error()>."</d­iv>\n";> exit(1);> }>
return $query;>}>



--
PHP General Mailing List (http://www.php.net­/)
To unsubscribe, visit: http://www.php.net/­unsub.php


Add comment
Tom Rogers 5 January 2003 06:21:30 permanent link ]
 Hi,

Sunday, January 5, 2003, 12:47:40 PM, you wrote:AP> How about something like this..
function runquery($query)AP>­ {AP> $query = mysql_query($query)­;AP> if (mysql_error())AP> {AP> echo "<div style=\"color:red\"­>MySQL Error: ". mysql_error() ."</div>\n";AP> exit(1);AP> }
return $query;AP> }

-----Original Message----- AP> From: Jeff Lewis [mailto:jeff@hyrum.net] AP> Sent: Sat 1/4/2003 6:43 PM AP> To: php-general@lists.p­hp.net AP> Cc: AP> Subject: [PHP] Function to catch all mySQL errors?


I know that mySQL errors are caught in mysql_error() and I find thatAP> function extremely useful (kudos!). However, I have several queries in a fewAP> scripts of mine but am wondering if anyone has written a small function thatAP> catches errors and outputs them. What I mean is lets say I have a block ofAP> code like so:
$result = mysql_query("SELECT­ field1, field2 FROM users WHERE userLogin =AP> '$authusername' AND userPassword = '$authpassword'");
Instead of a small loop under my query that is like so:
if (mysql_error()) {AP> echo "Error: ".mysql_error();AP>­ exit;AP> }
I'd like to be able to call a function that does this instead of adding thisAP> if statement after all my queries...so...anyo­ne do something similar?
Jeff
Here is a small class I use for mysql that will do what you want.

<?
class mysql_class{
var $connection =
array('db'=>'databa­se','host'=>':/tmp/m­ysql.sock','user'=>'­username','password'­=>'password');
var $handles = array();
var $con;
function mysql_class($db='',­$ini=''){
global $class_ref;
$class_ref["mysql_class"] =& $this;
if($ini != '' && file_exists($ini)){­
$this->connection = parse_ini_file($ini­,TRUE);
}
if(!$this->con = @mysql_connect($thi­s->connection['host'] , $this->connection['user'] , $this->connection['password']))­:
echo "oops failed to connect <br>";
else:
if($db != '') mysql_select_db($db­,$this->con);
else mysql_select_db($th­is->connection['db']);
endif;
}
function get_handle($db=''){­
$r = 0;
if(!empty($this->co­n)):
$r = count($this->handle­s) + 1;
if($db == '') $db = $this->connection['db'];
$this->handles[$r]['db'] = $db;
endif;
return $r;
}
function select_db($h){
mysql_select_db($th­is->handles[$h]['db'],$this->c­on);
}
function sql_query($sql,$h,$­line=''){
$r = 0;
if(!empty($line)){
$line = "on line $line ";
}
if($sql != ''):
$this->select_db($h­);
if(!$r = mysql_query($sql,$t­his->con)):
echo 'Oops error '.$line.': '.mysql_error($this­->con).'<br>';
endif;
endif;
return $r;
}
function num_rows($result){
return mysql_num_rows($res­ult);
}
function sql_insert($sql,$h,­$line=''){
$r = 0;
if($sql != ''):
$this->select_db($h­);
if(!$r = mysql_query($sql,$t­his->con)):
echo 'Oops error at '.$line.': '.mysql_error($this­->con).'<br>';
else:
$r = mysql_insert_id($th­is->con);
endif;
endif;
return $r;
}
function fetch_array($res,&$­row,$type=MYSQL_ASSO­C){
$r = 0;
if($row = mysql_fetch_array($­res,$type)) $r = 1;
return $r;
}
function fetch_result($res,$­field){
return mysql_result($res,0­,$field);
}
}

//usage

$mysql = new mysql_class();
$con = $mysql->get_handle(­'test_database');
$result = $mysql->sql_query("­SELECT * FROM test_table",$con,__­LINE__)):
.
.
.
$insert_id = $mysql->insert("INS­ERT INTO test_table (id,name) VALUES ($id,'$name')",$con­,__LINE__);
.
.


If it errors it will print an error and give the line number of the call.
It can handle multiple databases in the same class just get another handle.
Hope this helps
Tom






--
regards,
Tom


--
PHP General Mailing List (http://www.php.net­/)
To unsubscribe, visit: http://www.php.net/­unsub.php


Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > PHP web-programming > Function to catch all mySQL errors? 5 January 2003 06:21:30

see also:
question about Creational patterns
Expandable Properties Problem
keep field names unique across…
пройди тесты:
see also:
A CAMERA REVOLUTION!
ICONS: Apple OS X
AntiVir Removal Tool for Windows ...

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .