PHP Functions for MySQL to HTML

PHP has several functions that interact with MySQL databases that you can use to get information from them and display on your HTML pages. These include connection, selection and query functions. Save the results of the connection functions in PHP variables to keep the connection open on the Web page until you have collected all the information you need from the database.

  1. Mysql_connect Function

    • Before you can get any information from MySQL tables to your Web page, you need to connect to the database itself. The PHP mysql_connect function opens the connection to the server. At a minimum, you need to pass the MySQL server, username and password as parameters in the function, either as string literals or variables. You can also pass two additional parameters: one used to differentiate between multiple calls to the mysql_connect function for different database servers, and the other to set client flags. The function either establishes the connection or returns false.

    Mysql_select_db Function

    • The PHP mysql_select_db function selects a database that exists on the MySQL server, which especially helps when multiple databases exist. At a minimum, you only need to pass the database name as a parameter, either as a string literal or in a variable. However, you have the option to pass an identifier as a second parameter if you have established connections to several MySQL databases. The function returns true if it finds the database, or false if it fails.

    Mysql_query Function

    • When you have completed a connection to a MySQL database, the PHP mysql_query function lets you query the tables for information. You use SQL syntax in the queries, including select, insert, update and delete queries. You only need to pass the query as a parameter to the mysql_query function, either as a string literal or a variable. However, you do not have to include a semi-colon at the end of the query. You can also pass an identifier parameter similar to the mysql_select_db function. The function returns the results of the query if it finds any.

    Mysql_close Function

    • The PHP mysql_close function closes a connection to a MySQL database. If you have only called the mysql_connect function once on the Web page, you do not need to pass any parameters to the mysql_close function. If you have established multiple connections, pass the identifier value to the close function to specify that you want to close that connection. If you have multiple open connections but do not specify which one you want to close, the mysql_close function terminates the most recently opened connection.

Related Searches:

References

Comments

Related Ads

Featured