Welcome Guest ( Log In | Register )

2 Pages V   1 2 >  
Reply to this topicStart new topic
> Php Tips And Tricks | Debugging Your Mysql Queries, Intermediate
Phryxus
post Aug 13 2005, 07:51 AM
Post #1


Uptight Dot
Group Icon
Group: Moderator
Posts: 1,168
Joined: 20-October 04
Member No.: 916





Debugging Your Mysql Queries

What you should know before you start
Before you start with this tutorial it is best to know a couple of things about php. We are going to use a lot of functions! Functions in php are very easy to learn. So if you don't know how to use them and you do want to start with this tutorial, search Google and try to study them for an hour or two. You won't regret it!

We are also going to use mysql. You'll need a basic understanding on how the mysql queries work and how you can make a mysql connection. Jambo has written an excellent tutorial on this one. But I'm sure there are more tutorials on the web that'll teach you how to do that.

What you are going to learn
In this tutorial you are going to learn how to make your scripts easier to debug and how to make your errors more user friendly. It will take some time to make the necessary changes in your script, but it will save you time at the end!

Step 1 - Setting up your php files
The right structure of files will help you a lot when you are going to change anything later on. You don't want to search an hour for something small... the right structure to use in php scripts is different for every php programmer. I'll give you mine as an example. I advise you - for know - to use the same structure.

Open a new document and name it index.php. Copy the code below inside it.

index.php:
CODE
<?php
/* website settings */
include "../includes/settings.inc.php
/* functions */
include "../includes/functions/main.inc.php"
/* connection with database */
include "../includes/connect.inc.php";

/* this page */
?>


The reason I use '../' is that I always place index.php in it's own folder. Like www.website.com/home/index.php.

- In www.website.com/includes/settings.inc.php are all the general web settings
- In www.website.com/includes/functions/main.inc.php are all the general functions
- In www.website.com/includes/connect.inc.php the database connection is placed.

I will explain all these 3 files in the rest of the tutorial.

Please note: In connect.inc.php I will not teach you how to connect to a database! But what you can do to make it work better (with an user friendly and webmaster friendly error handling).

Step 2 - Debug settings
Inside settings.inc.php you are going to place all the debug related settings. I always use the define() function to set these settings. It's an easy to use function.

Open a new document and name it settings.inc.php. Also copy the code I've written to this document and place it in the right directory!

settings.inc.php:
CODE
<?php
// debug settings
define("DEBUG_ADVANCED_ERROR", true);
define("DEBUG_EMAIL", true);
define("DEBUG_EMAIL_TO", "your@mailaddress.com");
define("DEBUG_EMAIL_FROM", "error@mailaddress.com");
?>


As you can see I give a name in headcase only at the left side of the comma and I give the value at the right side. When you place the website outside the development you can easily set them all to false or just a few of them. It depends on yourself.

Step 3 - Creating a nice error report inside your connection
First create a new document and name it connect.inc.php. Also place this one in the right directory.

Copy the code below. It's still just a standard connection script.

connect.inc.php:
CODE
<?php
// connect to databasedatabase
$DBHost = "localhost";
$DBLogin = "***";
$DBPassword = "***";
$DBDatabase = "***";
$connection = mysql_connect($DBHost, $DBLogin, $DBPassword) or die(mysql_error());
mysql_select_db($DBDatabase) or die(mysql_error());
?>


Inside this standard connection script we are going to replace the mysql_error() with a own made functions. Give this new function the name you like. I prefer using a prefix (like 'web_' or 'tep_') and behind that prefix the name of the standard mysql function. So you get the name 'tep_mysql_error()'.

After you've placed this function instead of the mysql_error() you'll have something like this.

connect.inc.php:
CODE
<?php
// connect to databasedatabase
$DBHost = "localhost";
$DBLogin = "***";
$DBPassword = "***";
$DBDatabase = "***";
$connection = mysql_connect($DBHost, $DBLogin, $DBPassword) or die(tep_mysql_error());
mysql_select_db($DBDatabase) or die(tep_mysql_error());
?>


The newly added function will go inside the /includes/functions/main.inc.php.

Step 4 - Making the mysql error function
Open the document main.inc.php or create this document now.

Just start the function tep_mysql_error like every other function.

main.inc.php:
CODE
<?php
// error function
function tep_mysql_error() {

 // script here

}
?>


Inside this function we are going to use the defines we've made in settings.inc.php. The define() function is global. So we can freely use those values inside the function!

It's quite easy actually. I've written an example of the script below. I've commented it for you in the best possible way. There's nothing really hard at it. Except for mysql_errno() it is all standard.
The function mysql_errno() returns the 'php documented error code'.

functions.inc.php example 1:
CODE
<?php
// error function
function tep_mysql_error() {

 // if advanced error is true
 if (DEBUG_ADVANCED_ERROR == true) {
 
  // print a text line to make it easier to find the place of the error
  echo '<b>The error below is inside the document ' . __FILE__ . ' at line ' . __LINE__ . ':</b><br>' .
  // print advanced error
  mysql_errno() . ': ' . mysql_error() . '<br><br>';

 // else we print a standard error
 } else {

  // we doesn't print the full line, because we need the DEBUG_EMAIL to make the right and.
  echo 'An error accured in this file. Please try again. If that doesn\'t work ';

  // if mail is true
  if (DEBUG_EMAIL == true) echo 'try it again at another moment. And e-mail has been send to the webmaster to fix the problem when needed. ';
  else echo 'please e-mail the webmaster. ';
  // sorry message
  echo '<br>We\'re sorry for any inconvenience.';

 }

 // if mail is true >> we send an e-mail to the webmaster... the body of this e-mail is the same as the advanced error.
 if (DEBUG_EMAIL == true) {

  //e-mail subject
  $mesSubject = 'MySQL Error';
  // e-mail body
  $mesBody = 'Hello,' . "\n\n" .    
    'The error below is inside the document ' . __FILE__ . ' at line ' . __LINE__ . ':' . "\n" .
             mysql_errno() . ': ' . mysql_error() . "\n\n";
  // e-mail header from
  $mesFrom = 'From:' . DEBUG_EMAIL_FROM;
  // send e-mail
  mail(DEBUG_EMAIL_TO, $mesSubject, $mesBody, $mesFrom);

 }

}
?>



Step 5 - How you can use it
You can now use this in every mysql_query(). But not only that. Like I've done in this tutorial it also works inside mysql_connect(), mysql_select_db() and many others.

The End
If you have any problems, feel free to ask it in the forums.

If you have made another function, instead of my example, please post it below. I will add it inside the tutorial (with you Nickname next to it off course). Make sure you comment your script, so that other members know what you are doing.

I've spend a lot of hours writing this tutorial. This tutorial is restricted to 13Dots use only! If you want others to show it, just give them a link to this topic.


IPB
Gelderblom Webdesign {SITE24 - BLA Rotterdam}

Open Source alternatives:
 
+Quote Post  Go to the top of the page
RedDragon
post Aug 15 2005, 04:57 AM
Post #2


insert coin
Group Icon
Group: Main Team
Posts: 5,344
Joined: 24-May 04
From: Maastricht
Member No.: 35





Extensive tutorial smile.gif Nice work wouter!


IPB
 
+Quote Post  Go to the top of the page
cmonney
post Aug 15 2005, 09:31 AM
Post #3


~ Soul Rebel ~
Group Icon
Group: Main Team*
Posts: 3,624
Joined: 18-May 04
From: a mind-altering psychedelic trip
Member No.: 3





Yes very Nice!


smile.gif


IPB
cmonney
"Oh!, Great Creator of Being, grant us one more hour to perform our art and perfect our lives... We live, we die, and death not ends it..."

Topsites Is Back Open - Add Your Site
 
+Quote Post  Go to the top of the page
Eddie_K
post Aug 15 2005, 10:41 AM
Post #4


Hush now, I'm thinking...
Group Icon
Group: *Premier*
Posts: 3,977
Joined: 25-May 04
From: Heaven
Member No.: 47





Looks complicated. I need to learn MySQL now if I learned some PHP victory.gif

Nice tut. Thanks flowers.gif

Eddie

This post has been edited by EddiE_K: Aug 15 2005, 10:41 AM


IPB
Take the time to relax. Enjoy a game of Fallen Sword, great multiplayer RPG!

 
+Quote Post  Go to the top of the page
Phryxus
post Aug 16 2005, 01:41 AM
Post #5


Uptight Dot
Group Icon
Group: Moderator
Posts: 1,168
Joined: 20-October 04
Member No.: 916





Thank you guys!


IPB
Gelderblom Webdesign {SITE24 - BLA Rotterdam}

Open Source alternatives:
 
+Quote Post  Go to the top of the page
*D*
post Aug 16 2005, 07:34 AM
Post #6


Merry KissMoose
Group Icon
Group: Main Team
Posts: 15,988
Joined: 18-May 04
From: North Pole
Member No.: 2





WEll explained thankyou Phryxus flowers.gif
 
+Quote Post  Go to the top of the page
Adrian
post Aug 23 2005, 06:49 AM
Post #7


Addicted Dot
Group Icon
Group: Moderator
Posts: 7,033
Joined: 26-September 04
Member No.: 763





Great work, excellent for beginners.

-Adrian


IPB
 
+Quote Post  Go to the top of the page
Phryxus
post Aug 23 2005, 02:48 PM
Post #8


Uptight Dot
Group Icon
Group: Moderator
Posts: 1,168
Joined: 20-October 04
Member No.: 916





Thank you for the comments Trueer and Adrian!


IPB
Gelderblom Webdesign {SITE24 - BLA Rotterdam}

Open Source alternatives:
 
+Quote Post  Go to the top of the page
blu3t00th
post Aug 23 2005, 06:57 PM
Post #9


Banned
*
Group: Banned
Posts: 6
Joined: 23-August 05
Member No.: 6,413





great tut im going to start making my code like this now but i have one question whats with the .inc? why not just like functions.php or settings.php? and wuts with the define thing i dont really get it

CODE
<?php
// debug settings
define("DEBUG_ADVANCED_ERROR", true);
define("DEBUG_EMAIL", true);
define("DEBUG_EMAIL_TO", "your@mailaddress.com");
define("DEBUG_EMAIL_FROM", "error@mailaddress.com");
?>

can u just do like

$poois = "my poo is brown";

?? thanks

Matt
 
+Quote Post  Go to the top of the page
Phryxus
post Aug 24 2005, 12:39 AM
Post #10


Uptight Dot
Group Icon
Group: Moderator
Posts: 1,168
Joined: 20-October 04
Member No.: 916





You aren't stricted to use .inc.php as extension, but it is a great way to see which documents are included and which documents are main files. So do what you feel is best for you. bigwink.gif

The define function is quite easy. You can see it as saving something in a variable.

CODE
<?php
define("MY_TEXT", "green");
echo MY_TEXT;
?>


This will simply echo the text 'green'.

It's better to use the define() then a simple variable. Because something defined can be used inside functions and classes.

Do you understand it now blu3t00th? smile.gif

This post has been edited by BooGieMen: Aug 24 2005, 01:09 AM


IPB
Gelderblom Webdesign {SITE24 - BLA Rotterdam}

Open Source alternatives:
 
+Quote Post  Go to the top of the page

2 Pages V   1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: