Monday, June 14, 2010

Detecting Errors - javaScript

This is another way to detect error s in your php script. This also tells you that which is the place that code is currently running. This is a good but some complex calls of functions may not respond to this.

This is done by echoing a javascript.

    
echo
"<script>alert('Your message')</script>";

You can write any thing to your message.
Ex:
          1. A line number.
          2. A variable of php

Anything you want that helps to debug your code.This will alert you with a message and untill you click ok button of this alert message, the code will not continue to execute.

Have fun with this and remember to write comments. eeesY....
_______________

Detecting Errors - Log book

Here is the great way to detect bugs on your php script.This done by writing all outputs of the code to log file. It is very handy when you can't echo to the screen. Below shows how you can do it.

1.Create a file named log.txt in your root directory or other place you like.

2.Type the following code covering codes that you wish to debug.

        $fp = fopen("log.txt", "a+");
        ob_start();

        //put your code which is to be debugged here
       
        $content = ob_get_contents();
        ob_end_clean();
        fwrite($fp, $content);
        fclose($fp);


3.Next run the code and see the log.txt to check output and find where is the bug.

If you need any kind of help put a comment here.

Have fun with this.  eeesY

Friday, June 11, 2010

Insert queries in NULL condition

When you going to take some thing from a database, modify it and reinsert it as a new record you will find some errors.As each data records do not have full records. Some of them are null fields.So you have to validate and write dynamic queries to solve these issues.Here is the eesy PHP way.......

1.Make MySQL connection via mysql_connect(),mysql_select_db() 

 $connection mysql_connect("address_of_server","username","password"); 
 mysql_select_db("database", connection);

2.Query the data base to get needed records via mysql_query()

 $record mysql_query("SELECT * FROM table WHERE id=3;");

3.For avoid errors cast it to an array.

  $record = (Array)$record

4.Then check necessary fields for null and make the trick (to re insert it as null field)

 $name=(!empty($record ['name']) ? $record ['name'] : "NULL");
      At here it checks for the null condition and if null initialize new variable as null

5.Reinsert the data using new variables making new dynamic query.


 $result mysql_query("INSERT INTO table(id,name)VALUES(5,".$name."); ");

To deal with more than one record, combine this with foreach and mysql_num_rows($record)

  Have fun with this. eeesY

Array refer - foreach

 This is a good way to go through arrays in php. We do not want to know about array size or is it empty.We can do this for object arrays too.

Syntax:

foreach($array as $variable)
{
     //your code
}

Example:

$simple_array=Array('One','Two','Three');

foreach($simple_array as $point)
{
    echo $point."
";
}
?>

Output of the code:
One
Two
Three

Have fun with this. eeeeesY