6 Replies - 1455 Views - Last Post: 27 November 2010 - 05:23 PM Rate Topic: -----

#1 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Global MySQL error handle

Posted 27 November 2010 - 11:08 AM

Just a quickie. Is there a way to globally set the way mysql errors are handled without having to set behavior for EVERY error type (I know a little about the set_error_handler)?

This would be incredibly useful because I'm tired of having to put [or die("MySQL Error: ".mysql_error()."<br />SQL: ".$sql);] after EVERY mysql query.
Is This A Good Question/Topic? 0
  • +

Replies To: Global MySQL error handle

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Global MySQL error handle

Posted 27 November 2010 - 11:11 AM

use PDO and set its error handling to Exceptions.

see this tutorial
Was This Post Helpful? 1
  • +
  • -

#3 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: Global MySQL error handle

Posted 27 November 2010 - 11:43 AM

View PostDormilich, on 27 November 2010 - 10:11 AM, said:

use PDO and set its error handling to Exceptions.

see this tutorial


The only thing is that I'm not using OOP, and find it extremely confusing in terms of the MMO I'm developing.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Global MySQL error handle

Posted 27 November 2010 - 12:25 PM

So write your own wrapper function and call that instead?
Was This Post Helpful? 0
  • +
  • -

#5 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: Global MySQL error handle

Posted 27 November 2010 - 03:27 PM

View PostJackOfAllTrades, on 27 November 2010 - 11:25 AM, said:

So write your own wrapper function and call that instead?


Wrapper function? Something like this?
function doQuery($sql) {
  mysql_query($sql);
  if (mysql_errno()) {
    die("MySQL Error: ".mysql_error());
  else {
    return mysql_query($sql);
  }
}

This post has been edited by the1corrupted: 27 November 2010 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Global MySQL error handle

Posted 27 November 2010 - 03:45 PM

Yup, you got the idea!

EDIT: Sort of. Why are you calling mysql_query twice?

Here.

function doQuery($sql)
{
    $result = mysql_query($sql);
    if (!$result)
    {
        die("SQL Query $sql failed: " . mysql_error());
    }
    return $result;
}

This post has been edited by JackOfAllTrades: 27 November 2010 - 03:47 PM

Was This Post Helpful? 1
  • +
  • -

#7 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: Global MySQL error handle

Posted 27 November 2010 - 05:23 PM

View PostJackOfAllTrades, on 27 November 2010 - 02:45 PM, said:

Yup, you got the idea!

EDIT: Sort of. Why are you calling mysql_query twice?

Here.

function doQuery($sql)
{
    $result = mysql_query($sql);
    if (!$result)
    {
        die("SQL Query $sql failed: " . mysql_error());
    }
    return $result;
}

EDIT: Assuming the best, I was wondering if these functions would work?
I'm scared to try it just yet.
Oh, and I am a big fan of the procedural method. OOP gives me headaches.


I've created a small batch of working mySQL functions
include ("db_conn.php"); //$link is defined in here as the mysqli_connect()
###	MYSQL GLOBAL FUNCTIONS	###
function doQuery($sql) {
	#	This function is intended to process INSERT and UPDATE queries.
	global $link;
	mysqli_query($link, $sql) or die ("MySQL Error (".mysqli_errno($link)."): ".mysqli_error($link)."<br />Query: ".$sql);
}

function makeQuery($sql) {
	#	This function is intended to process queries that need further processing.
	global $link;
	$result=mysqli_query($link, $sql);
	if (!$result) {
		die ("MySQL Error (".mysqli_errno($link)."): ".mysqli_error($link)."<br />Query: ".$sql);
	}
	return $result;
}

function getData($sql) {
	#	This function is intended to fetch ONE ROW of data.
	$query=makeQuery($sql);
	return mysqli_fetch_assoc($query);
}

function isRow($sql) {
	$query=makeQuery($sql);
	$num=mysqli_num_rows($query);
	if ($num>0) {
		return true;
	}
	return false;
}

This post has been edited by the1corrupted: 27 November 2010 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1