Home  >  Article  >  Backend Development  >  PHP MYSQL dynamic web page programming error correction guide_PHP tutorial

PHP MYSQL dynamic web page programming error correction guide_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:03:44647browse

About databases In WEB programming, the role of databases cannot be ignored more and more. When it comes to databases, it should be said that PHP has very powerful database support functions. From FileMaker to Oracle, it can achieve seamless connection with almost all database systems. In order to facilitate the explanation of this article, we will mainly use MySQL as an example. However, it is also applicable to other database applications.

Using PHP to perform database operations generally requires the following statements:

​$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 ------------------
;
​print "$Result[0]
";
​print "$Result[1]
";
​print "$Result[2]
";
​print "$Result[3]
";
​print -------------------
;
}?>

The basic steps include establishing a connection to the MySQL database, selecting the database operation object, and then executing the query statement. Generally speaking, the error message that occurs during the above process can describe the problem more accurately and specifically. For example, the "Connection failed due to a bad username" error report clearly indicates that the connection to the database failed due to a bad username.

We can make use of the return values ​​of the functions mentioned above to reduce unnecessary trouble. For example, the mysql_connect function will return a connection ID identification when the connection is successful, and issue an error message if the connection fails. In this regard, we can make use of the following:

​if (!mysql_connect (’localhost’, ’root’, ’password’)){
​print "Cannot connect to MySQL
";
exit;
}

When there is a problem with the connection to the database, we can output an error message and terminate the execution of the program. This is a very good precaution in the long run. In this way, we rewrite 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
";
exit;
}
​if (!$Result_ID=mysql_query ($Query)){
​print "Query Error: ".mysql_error();
exit;
}
​while ($Result=mysql_fetch_row($Result_ID)){
​print ------------------
;
​print "$Result[0]
";
​print "$Result[1]
";
​print "$Result[2]
";
​print "$Result[3]
";
​print -------------------
;
}?>

In this way, when a problem occurs in the program, we can immediately find the source of the error, so that we can be targeted.

Next, we can query the database. However, many times, when we run the written query statement, we do not get any return data. What exactly went wrong? 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, use the "print" or "echo" command to display the statement. Pay attention to check whether the spelling of $ColumnName and $Limit is correct and whether new variables are inadvertently created. Spelling errors can be easily found and resolved using the output display method. But what if we still don’t find any obvious errors after displaying the SQL statement? Here we can paste the output statement into a command line tool such as the Mysql command line interface to see if the data can be returned. If you still can't solve the problem, you should check the user permissions of the account you are using.

Today, we can use many freely available classes to complete most database operations. There is a lot of relevant information on PHP Classes (http://phpclasses.upperdesign.com/), which can be used as a reference for interested users. Among them, MetaBase can provide query and management that does not rely on a certain database system. If the user is using several different database systems at the same time, or hopes that his program can be transplanted to other database platforms, he can pay attention to the use of MetaBase.

Things to pay attention to Finally, we have summarized some issues that should be paid attention to when using PHP for programming, hoping to be helpful to everyone.

1. Check (), [], and {} symbols to see if they appear in pairs.
2. Check the string and note that if you want to use "" inside "", you must use the escape character "".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630909.htmlTechArticleAbout the database In WEB programming, the role of the database cannot be ignored more and more. When it comes to databases, it should be said that PHP has very powerful database support functions, from FileMaker to Oracl...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn