This is the first post on your Nucleus CMS. Blog. This blog will contain articles on Tips on Web Site Design, plus Tip and Help with Computer Repairs, plus Advice and Help with SEO or Search Engine optimization. We are quite new to writing blogs, but I am sure that we ill get the hang of it fast. The very first article or blog is going to be on how to debug you SQL queries in PHP. Yep a tricky subject, but a necessary one!?. You see when you write a PHP script to place user input into a database you will of course, firstly connect to the MySQL Server, and then you will select the database you want to use. Then you will write some SQL to insert the data in to the table you choose. Then you will use a PHP function to run that sql query, but look if you just run it and for some reason it does not run correctly, you need to know why.. so Read on......

Normally, if you are going to write some PHP to run the SQL Query, you write something like this..

if (mysql_query($sql, $conn)){ then some action in here like a message }
else { some other action in here }

But of course there is no error trapping here, and so if you run it you will just find that the else bit runs, and this might be just a message to say something whent wrong!! Look if you want to trap the error you need another function .

Lets get clear whats what.
1. $sql contains the actual sql statement you want to run
2. $conn contains the reference to the mysql server connection is could be something like:

$conn = mysql_connect("localhost", "auser_in_here", "that_user_password") or die(mysql_error());

The function you want to get the Myssql error and see it is:
$result = mysql_errno();
echo 'result ='."$result";

If you place these lines inside the else part, where you will be when the sql query fails you should see a mysql error number on the screen and this will help you no end!!?

Assumptions made in this article:
In order for you to connect to the mysql server using something like:

$conn = mysql_connect("localhost", "auser_in_here", "that_user_password") or die(mysql_error());

the MySQL Server must be on the same machine as the web server, since you have used localhost.

If you are going to use a specific user to make the connection to the My SQl Server, you must ensure that user does exist, i.e. they must be a valid real MySQL User, secondly they must connect using the correct password, and they must have the necessary rights to connect to that MySQL Server.
The Database you select, when you do, must of course exist, and you must spell it correctly when you call the function;

mysql_select_db("blsr_blsr",$conn) or die(mysql_error());

Obviously blsr_blsr is the database name!!

Lastly , when writing a new script to connect to a MySql Server you can place code in to test for errors at every stage, i.e. test to see if the connection to the MySQl server has been made, test to see if the database you want has been selected ok. Once all is ok you can take the tests out for a live script on a public server just by commenting them out, with //
or with /* */

» Read More