A wide range of PHP MySQL database manipulation functions is already included in PHP. We’ll talk about PHP MySQLi Functions today. Prior to these, we also covered PHP Exception And Error Handling Tutorial , Forms in PHP, PHP Session, PHP Operators, and Mail Function In PHP. If you don’t know about these, then act quickly!
Access MySQL database servers using the MySQLi functions.
The final optional parameter link_identifier is accepted by the majority of MySQL functions. If it is omitted, the most recent connection established is used. If it is absent, connection attempts are made using the php.ini default parameters. Functions return FALSE if the operation is unsuccessful.
Installation / Runtime Configuration
It is necessary to compile PHP with support for the MySQLi extension if you want the MySQLi functions to be available.
With PHP 5.0.0, the MySQLi extension was introduced. In PHP 5.3.0, the MySQL Native Driver was added.
For installation, go to: http://php.net/manual/en/mysqli.installation.php
For runtime configuration, go to: http://php.net/manual/en/mysqli.configuration.php
PHP MySQLi Functions
PHP mysqli_connect function
To establish a connection with a MySQL database server, use the PHP MySQL connect function.
Syntax:
<?php;
$db_handle = mysqli_connect($db_server_name, $db_user_name, $db_password);
?>
PHP mysqli_select_db function
The function mysqli_select_db is used to choose a database.
Syntax:
<?php
mysqli_select_db($db_handle,$database_name);
?>
PHP mysqli_query function
To run SQL queries, use the mysqli_query function. The following query types can be run using the function.
- Insert
- Select
- Update
- delete
Syntax:
<?php
mysqli_query($db_handle,$query) ;
?>
PHP mysqli_num_rows function
The number of rows returned from a select query can be determined using the mysqli_num_rows function.
Syntax:
<?php
mysqli_num_rows($result);
?>
PHP mysqli_fetch_array function
Row arrays are fetched from a query result set using the mysqli_fetch_array function.
Syntax:
<?php
mysqli_fetch_array($result);
?>
PHP mysqli_close function
An open database connection can be closed using the mysqli_close function.
Syntax:
<?php
mysqli_close($db_handle);
?>
Let’s examine some real-world examples of how these features are used.
The MySQL database creation This tutorial assumes that the reader has some familiarity with MySQL and SQL; if not, please consult the tutorials on those topics.
The steps to create the database and table are listed below. Here we have created a form that has connected to the database and you can insert the data and you can delete the data.
Filename: fileinsert.php
<?php if(isset($_POST['submit'])){ $server="localhost"; $username="root"; $password=""; $database='mpc_trip'; $con = mysqli_connect($server,$username, $password, $database); if(!$con){ die("Connection Failed".mysqli_connect_error()); }else{ // echo "Successfully connected"; $name= $_POST['name']; $image= $_FILES['image']["name"]; $gender= $_POST['gender']; $age= $_POST['age']; $date= $_POST['date']; $email= $_POST['email']; $phone= $_POST['phone']; $country= $_POST['country']; $message= $_POST['message']; if (isset($_POST['cbox'])) { $sms="YES"; } elseif (!isset($_POST['cbox'])) { $sms="NO"; } else { echo "Some error occurred"; } $query2="select * from `formmpc` where name='$name'"; $result1=mysqli_query($con, $query2); if(mysqli_num_rows($result1)>0){ echo '<script> alert("Already exits"); </script>'; } else{ $sql="INSERT INTO `mpc_trip`.`formmpc` (`id`, `name`, `image`,`gender`, `age`, `date`, `email`, `phone`, `country`, `message`, `emailverify`) VALUES ('', '$name', '$image','$gender', '$age', '$date', '$email', '$phone', '$country', '$message','$sms');"; $result = mysqli_query($con,$sql); if($result){ // echo "Msg= $sms"; move_uploaded_file($_FILES["image"]["tmp_name"], "image/".$_FILES['image']["name"]); echo '<script>alert("Inserted Successfully")</script>'; } // if($sql){ // echo "Data Inserted Successfully"; // } // else{ // echo "Data not Inserted Successfully"; // } } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Starting Form </title> <link href="https://fonts.googleapis.com/css2?family=Roboto+Serif:ital,[email protected],100;0,200;0,300;1,100;1,200&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Redressed&display=swap" rel="stylesheet"> <link rel="Stylesheet" href="style.css"> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> </head> <body> <!-- <img class="bg" src="" alt="" style="height: 57em;"> --> <div class="Container bg-success p-2 text-white bg-opacity-40" > <h1>Registration Form</h1> <p>Enter Your Details and submit this form to Confirm your participation in the Trip</p> <!-- <p class="submitmsg">Thanks for submitting your form. We are happy to see you joining with us</p> --> <form action="" method="post" enctype="multipart/form-data"> <input type="text" name="name" id="Name" placeholder="Enter Your Name" style="margin:11px;"> <div class=" images mb-2" style=" margin-right: 26em;margin-top: 1em;position: relative;"> <label for="exampleFormControlInput2" class="form-label">Profile Photo</label> <input type="file" name="image" id="image" placeholder="" style=""> </div> <div class="form-check" style="margin-right: 45em;margin-top: 1.5em;"> Select your Genders <br> <input type="radio" name="gender" value="male" style="position: relative;top: 25px;left: 1em;"> Male <input type="radio" name="gender" value="female" style="position: relative;top: 25px;left: 1em;"> Female </div> <input type="text" name="age" id="age" placeholder="Enter Your Age" style="margin:11px;margin-top: 1em;"> <div> <div class="mb-3" style=" margin-right: 45em;margin-top: 1em;position: relative;left: 13px;"> <label for="exampleFormControlInput1" class="form-label">Date of Birth</label> <input type="date" class="form-control" name="date" id="exampleFormControlInput1" placeholder="date"> </div> </div> <input type="email" name="email" id="Email" placeholder="Enter Your Email" style="margin:11px;"> <input type="tel" name="phone" id="phone" placeholder="Enter Your Mobile Number" style="margin:11px;"> <div style="margin-right:37em;margin-left: 1em;"> <label for="country">Country: </label> <select name="country" id="country" required style="margin: 22px 6px; border: 1px solid #030303c2; border-radius: 6px;height: 39px;"> <option value="please select a country">please select a country</option> <option value="India">India</option> </select> </div> <textarea name="message" id="Description" cols="30" rows="10" placeholder="About Your Information" style=" height: 138px; position: relative;left: 8px;"></textarea> <div class="form-check" style="margin-right: 45em;margin-top: 1em;"> <input class="form-check-input" name="cbox" type="checkbox" value="" style=" margin-left: 1em;"> <label class="form-check-label" style="margin-left:10px;"> send sms in Email </label> </div> <div class="button" style="border: 1px solid rebeccapurple;background: antiquewhite;border: 1px solid rebeccapurple;background: antiquewhite;width: 95px;"> <button class="btn" name="submit">Submit</button> </div> <div class="anchor" style="border: 1px solid black; margin-left: 18em;position: relative;bottom:40px;padding: 7px 6px;background: antiquewhite;width: 8em;"> <a href="viewdetails.php" style="color: black; text-decoration: none;">View Details</a> </div> </form> <!-- <button class="btn">Reset</button> --> </div> </body> </html>
OUTPUT: