How to Connect PHP Websites to a MySQL Database

In this post learn about how to access MySQL database tables using PHP programming language. PHP is most commonly used with freely available MySQL database during PHP web development. MySQL databases can be accessed directly through PHP scripts on a web page. This lets you read and write data to your database directly from your website.

  1. Connect to your MySQL server using the mysqli_connect statement. For example:
$con = mysqli_connect('HOSTNAME','USERNAME','PASSWORD');


Here if MySQL database is installed on the same PHP server then HOSTNAME will be localhost otherwise it will be IP address of the MySQL server.

2. Select the database that you want to access using mysqli_select_db. For example:

mysqli_select_db('DATABASENAME', $con)


Where 'DATABASENAME' is the name of your database. To know about the database details, follow the previous post How to Manage MySQL Database with phpMyAdmin.

After establishing the connection and selecting the database, you can query it using PHP.

To help you create your own connection string, follow the example below.

Example PHP MySQL connection string

This connect string will look in a database (your_dbusername), find a particular table (your_tablename), and then list all values in that table for a field (i.e. column) you specify (your_field).

<?php
	//Sample Database Connection Syntax for PHP and MySQL.
	
	//Connect To Database
	
	$hostname="your_hostname";
	$username="your_dbusername";
	$password="your_dbpassword";
	$dbname="your_dbusername";
	$usertable="your_tablename";
	$yourfield = "your_field";
	
	mysqli_connect($hostname,$username, $password) or 
die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),
history.go(-1)</script></html>");
mysqli_select_db($dbname); # Check If Record Exists $query = "SELECT * FROM $usertable"; $result = mysqli_query($query); if($result){ while($row = mysqli_fetch_array($result)){ $name = $row["$yourfield"]; echo "Name: ".$name."<br/>"; } } ?>

MySQL Functions available in PHP language

  • mysql_affected_rows — Get number of affected rows in previous MySQL operation
  • mysql_client_encoding — Returns the name of the character set
  • mysql_close — Close MySQL connection
  • mysql_connect — Open a connection to a MySQL Server
  • mysql_create_db — Create a MySQL database
  • mysql_data_seek — Move internal result pointer
  • mysql_db_name — Retrieves database name from the call to mysql_list_dbs
  • mysql_db_query — Selects a database and executes a query on it
  • mysql_drop_db — Drop (delete) a MySQL database
  • mysql_errno — Returns the numerical value of the error message from previous MySQL operation
  • mysql_error — Returns the text of the error message from previous MySQL operation
  • mysql_escape_string — Escapes a string for use in a mysql_query
  • mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
  • mysql_fetch_assoc — Fetch a result row as an associative array
  • mysql_fetch_field — Get column information from a result and return as an object
  • mysql_fetch_lengths — Get the length of each output in a result
  • mysql_fetch_object — Fetch a result row as an object
  • mysql_fetch_row — Get a result row as an enumerated array
  • mysql_field_flags — Get the flags associated with the specified field in a result
  • mysql_field_len — Returns the length of the specified field
  • mysql_field_name — Get the name of the specified field in a result
  • mysql_field_seek — Set result pointer to a specified field offset
  • mysql_field_table — Get name of the table the specified field is in
  • mysql_field_type — Get the type of the specified field in a result
  • mysql_free_result — Free result memory
  • mysql_get_client_info — Get MySQL client info
  • mysql_get_host_info — Get MySQL host info
  • mysql_get_proto_info — Get MySQL protocol info
  • mysql_get_server_info — Get MySQL server info
  • mysql_info — Get information about the most recent query
  • mysql_insert_id — Get the ID generated in the last query
  • mysql_list_dbs — List databases available on a MySQL server
  • mysql_list_fields — List MySQL table fields
  • mysql_list_processes — List MySQL processes
  • mysql_list_tables — List tables in a MySQL database
  • mysql_num_fields — Get number of fields in result
  • mysql_num_rows — Get number of rows in result
  • mysql_pconnect — Open a persistent connection to a MySQL server
  • mysql_ping — Ping a server connection or reconnect if there is no connection
  • mysql_query — Send a MySQL query
  • mysql_real_escape_string — Escapes special characters in a string for use in an SQL statement
  • mysql_result — Get result data
  • mysql_select_db — Select a MySQL database
  • mysql_set_charset — Sets the client character set
  • mysql_stat — Get current system status
  • mysql_tablename — Get table name of field
  • mysql_thread_id — Return the current thread ID
  • mysql_unbuffered_query — Send an SQL query to MySQL without fetching and buffering the result rows

For more information on PHP MySQL Functions visit the page at php.net.

Thanks for visiting. For queries and suggestions, emails are welcome at learnweb@hostingcolumn.com.

Subscribe to Hosting Column for latest updates and posts.