Php mysql dynamic web page programming error correction guide

Source: Internet
Author: User
Tags php mysql mysql command line metabase

With regard to the WEB programming of databases, the role of databases has become increasingly important. When talking about databases, PHP has very powerful database support functions. From FileMaker to Oracle, PHP can be seamlessly connected to almost all database systems. For the convenience of this article, we will take MySQL as an example. However, it is also applicable to other database applications.

The following statements are generally required for database operations using PHP:

<?
$ Host = "localhost ";
$ MySQL_UserName = "root ";
$ MySQL_UserPass = "password ";
$ MySQL_Database = "db ";
$ Query = "SELECT * FROM domain ";
Mysql_connect ($ Host, $ MySQL_UserName, $ MySQL_UserPass );
Mysql_select_db ($ MySQL_Database );
$ Result_ID = mysql_query ($ Query );
While ($ Result = mysql_fetch_row ($ Result_ID )){
Print ------------------ <BR>;
Print "$ Result [0] <BR> ";
Print "$ Result [1] <BR> ";
Print "$ Result [2] <BR> ";
Print "$ Result [3] <BR> ";
Print ------------------- <BR>;
}?>

The basic steps include establishing a connection to the MySQL database, selecting the database operation object, and then executing the query statement. In general, the prompts for errors in the above process can be more accurate and specific descriptions. For example, the "Connection failed due to a bad username" error report clearly indicates that the Connection to the database fails due to a user name error.

We can use the return values of these functions mentioned above to reduce unnecessary troubles. For example, the mysql_connect function returns a Connection ID when the connection is successful. If the connection fails, an error is returned. In this regard, we can make the following use:

If (! Mysql_connect ('localhost', 'root', 'password ')){
Print "Cannot connect to MySQL <BR> ";
Exit;
}

When there is a problem with the database connection, we can output an error prompt and terminate the program execution. In the long run, this is a very good precaution. In this way, we re-compile the script as follows:

<?
$ Host = "localhost ";
$ MySQL_UserName = "root ";
$ MySQL_UserPass = "password ";
$ MySQL_Datab = "db ";
$ Query = "SELECT * FROM domain ";
If (! Mysql_connect ($ Host, $ MySQL_UserName, $ MySQL_UserPass )){
Print "Cannot connect to MySQL:". mysql_error ();
Exit;
}
If (! Mysql_select_db ($ MySQL_Database )){
Print "Cannot select db <BR> ";
Exit;
}
If (! $ Result_ID = mysql_query ($ Query )){
Print "Query Error:". mysql_error ();
Exit;
}
While ($ Result = mysql_fetch_row ($ Result_ID )){
Print ------------------ <BR>;
Print "$ Result [0] <BR> ";
Print "$ Result [1] <BR> ";
Print "$ Result [2] <BR> ";
Print "$ Result [3] <BR> ";
Print ------------------- <BR>;
}?>

In this way, when a program encounters a problem, we can immediately find the root cause of the error, so as to be targeted.

Next, we can query the database. However, many times, when we run the compiled query statement, we do not get any returned data. What is an error? The best solution is to assign the SQL statement to a variable, for example:

<?
....
$ SQL = "SELECT * FROM $ TableName WHERE $ ColumnName> $ Limit ";
$ Result_ID = mysql_query ($ QUERY );
...?>

Then, when a problem occurs, run the "print" or "echo" command to display the statement. Check whether the spelling of $ ColumnName and $ Limit is correct and whether new variables are created unintentionally. The output display method can be used to easily locate and solve spelling errors. But what if we do not find any obvious error after displaying the SQL statement? Here we can paste the output statement into a command line tool like the Mysql command line interface to see if data can be returned. If the problem persists, check the user permissions of the account.

Today, we can use many free classes to perform most database operations. PHP Classes (http://phpclasses.upperdesign.com/) on there is a lot of related information, can be used for user reference. MetaBase can provide query and governance based on a database system. If you are using several different database systems at the same time, or want your programs to be transplanted to other database platforms, you can pay attention to the use of MetaBase.

Finally, we will summarize some of the issues that should be paid attention to when using PHP for programming, hoping to help you.

1. Check the symbols (), [], and {} to see if they are paired.
2. Check the string. If you want to use "" in "", you must use the escape character "".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.