Interface of PHP with DataBases by yung This is a quick guide for you to learn about about the interaction with MySQL databases that can be done from PHP scripts. From this guide we're assuming that you have knowledge on how to use SQL code, we are also assuming you already have some knowledge on php scripting. For more information on MySQL you could visit www.mysql.com and for php you could read our PHP lecture or go to www.php.net. Examples of what can be done with PHP and MySQL: Connecting to a Server: mysql_connect("your.domain.com", "login", "password"); This command will cause the service of PHP to connect to the host of your choosing, and login with the login/password information given. You won't need to worry about the web users of seeing your password, PHP processes the information before it is sent to client side. This instruction must be used before using any other instruction related to MySQL on PHP. Example: Let's think that your domain is "no.domain.com", and your username for your MySQL database is "yung" and the password ir "letsgo". Then you can use the following code to connect: $result=mysql_connect("no.domain.com","yung","letsgo"); ?> Consulting the Server: mysql_db_query("database name", "MySQL Query"); This command will perform a query to the MySQL database. The results are usually stored on a variable so that they can be accessed later for displaying on the web page. Example: To execute a query on a database called "data", we must first select all the data from the table "local_data" and put them on a variable called $result, we can do this by executing the following line of code on our web pages: $result = mysql_db_query("data", "select * from local_data"); ?> Converting the data into PHP array data: mysql_fetch_array($result); This command will convert all the results from the MySQL query into PHP for easier management of data. $result should be the variable where you stored the MySQL query results. Example: To convert the data that was stored on a variable called $result and storing the converted data on an array variable called $rows, we should execute the following command: $rows = mysql_fetch_array($result); ?> Counting the selected data: mysql_num_rows($result); This command will return the number of rows/lines of data that have been stored via a query. The result of the command mysql_num_rows is usually putten on a variable to be used on a structured cycle. $result should be replaced with the name of the variable where you have stored all the results of the query using the command mysql_db_query. Example: To retrieve the number of rows returned on a query that returns your results on a variable called $result, and store this value on a variable called $num_rows, you'll have to use the following command: $num_rows = mysql_num_rows($result); ?> Obtaning the number of fields used: mysql_num_fields($result); The command mysql_num_fields can be used to obtain the number of fields that result from a MySQL query. This value is usually assigned to a variable for use on a cycle. $result should be replaced with the name of the variable of your choice that stores the result of a query that has been performed earlier. Example: To obtain the number of fields returned on a query which results were stored on a variable called $result, and storing the value on a variable called $num_fields, you should use the following command: $num_fields = mysql_num_fields($result); ?> Displaying information: echo $variable; This command is used to display the value of a variable on the web page. $variable should be replaced with the name of the variable that you desire to display. Also can be used in conjunction with mysql_results to display on HTML pages. Example: To display the content of a variable $num_rows to a page, you should use the following command in the page where you desire to display it: echo $num_rows; ?> Displaying Results: mysql_result($result, 0, "Description"); This command is used to determine the value of a specific row and field that has been resulted from a query. Example: To return the value of a field called "LastName" from the first row of results of a query that has been stored on $result, you should use the following command: echo mysql_result($result,0,"LastName"); ?> Putting everything together: The lines that start with a # are comments, were it should explain what we're trying to do on a specific line of code.
| ?> | ?> | ?> | ?> | ?> |