Login | Contact Us | Site Map | Resources

PHP Database Functions (MYSQL)

Home > PHP Tutorial > Running MYSQL Queries In PHP

Running MYSQL Queries In PHP

The mysql_query() function is a "catch all" that can run about any MYSQL query that you give it. Let's look into the execution of some standard insert, select, update and delete statements.

  $con = mysqli_connect("localhost","my_username","my_secret_password", "database_name");
  if (!$con) { die('Could Not Connect: ' . mysql_error($con) . mysql_errno($con)); }

  $insert = mysqli_query($con, "INSERT INTO table_name (col1, col2) VALUES('Value 1', 'Value 2' );");
  if (!$insert) { die (mysql_error($con)); }

  $select = mysqli_query($con, "SELECT * FROM table_name;");
  if (!$select) { die (mysql_error($con)); }

  $update = mysqli_query($con, "UPDATE table_name SET col2 = 'Value' WHERE col2 LIKE 'Value 2';");
  if (!$update) { die (mysql_error($con)); }

  $delete = mysqli_query($con, "DELETE FROM table_name WHERE col2 LIKE 'Value';");
  if (!$delete) { die (mysql_error($con)); }


As you can see, each time mysqli_query() is used, it can be assigned a handle that we can later use to identify the results of the statement. Also, the function is (optionally) given the opportunity to print out an error message and die if errors occurred during execution of the statement.


Function Description
mysqli_query() Performs a query against the database