Verification: a143cc29221c9be0

Php catch error in include

PHP mysqli_connect function

The PHP mysql connect function is used to connect to a MySQL database server.

It has the following syntax.

HERE,

  • “$db_handle” is the database connection resource variable.
  • “mysqli_connect(…)” is the function for php database connection
  • “$server_name” is the name or IP address of the server hosting MySQL server.
  • “$user_name” is a valid user name in MySQL server.
  • “$password” is a valid password associated with a user name in MySQL server.

PHP mysqli_select_db function

The mysqli_select_db function is used to select a database.

It has the following syntax.

  HERE,

  • “mysqli_select_db(…)” is the database selection function that returns either true or false
  • “$database_name” is the name of the database
  • “$link_identifier” is optional, it is used to pass in the server connection link

PHP mysqli_query function

The mysqli_query function is used to execute SQL queries.

The function can be used to execute the following query types;

  • Insert
  • Select
  • Update
  • delete

It has the following syntax.

  HERE,

  • “mysqli_query(…)” is the function that executes the SQL queries.
  • “$query” is the SQL query to be executed
  • “$link_identifier” is optional, it can be used to pass in the server connection link

PHP mysqli_num_rows function

The mysqli_num_rows function is used to get the number of rows returned from a select query.

It has the following syntax.

  HERE,

  • “mysqli_num_rows(…)” is the row count function
  • “$result” is the mysqli_query result set

PHP mysqli_fetch_array function

The mysqli_fetch_array function is used fetch row arrays from a query result set.

It has the following syntax.

  HERE,

  • “mysqli_fetch_array(…)” is the function for fetching row arrays
  • “$result” is the result returned by the mysqli_query function.

PHP mysqli_close function

The mysqli_close function is used to close an open database connection.

It has the following syntax.

  HERE,

  • “mysqli_close(…)” is the PHP function
  • “$link_identifier” is optional, it is used to pass in the server connection resource

Let’s look at practical examples that take advantage of these functions.

Creating the MySQL database This tutorial assumes knowledge of MySQL and SQL, if these terms are unfamiliar to you, refer to our MySQL and SQL tutorials.

We will create a simple database called my_personal_contacts with one table only.

Below are the steps to create the database and table.

  • Connect to MySQL using your favorite access tool such as MySQL workbench, phpMyAdmin etc.
  • Create a database named my_person_contacts
  • Execute the script shown below to create the table and insert some dummy data

  We now have a database set up that we will manipulate from PHP.

Reading records from the database We will now create a program that prints the records from the database.

';         
		echo 'Full Names: ' . $row['full_names'] . '
';         echo 'Gender: ' . $row['gender'] . '
';         echo 'Contact No: ' . $row['contact_no'] . '
';         echo 'Email: ' . $row['email'] . '
';         echo 'City: ' . $row['city'] . '
';         echo 'Country: ' . $row['country'] . '

';     } } mysqli_close($dbh); //close the database connection ?>

  Executing the above code returns the results shown in the diagram shown below

Inserting new records

Let’s now look at an example that adds a new record into our table. the code below shows the implementation.

  Executing the above code outputs “Poseidon has been successfully added to your contacts list” go back to the select query example and retrieval your contacts again.

See if Poseidon has been added to your list.

Updating records Let’s now look at an example that updates a record in the database.

Let’s suppose that Poseidon has changed his contact number and email address.

Deleting records

Let’s now look at an example that removes records from the database. Let’s suppose that Venus has a restraining order against us, and we must remove her contacts info from our database.

Example 1.

The following code will evaluate to true because the main string $str contains the substring ‘This‘ in it. This will print “true”.

$str = 'This is Main String';

if (strpos($str, 'This') !== false) {

    echo 'true';

}

?>

Output: true

Example 2.

The following code will evaluate to false because the main string $str doesn’t contains the substring ‘Hello‘ in it. This will nothing print.

$str = 'This is Main String';

$substr = "Hello";

if (strpos($str, $substr) !== false) {

    echo 'true';

}

?>

Output: none

Example 3.

The following code will check if a String contains a substring at start. The following code will evaluate to true because the main string $str contains the substring ‘This‘ at start.

$str = 'This is Main String';

if (strpos($str, 'This') === 0 ) {

    echo 'true';

}

?>

Output: true