PHP and MySqli CRUD Operation Tutorial

PHP and MySqli CRUD Operation Tutorial

Today, We want to share with you PHP and MySqli CRUD Operation Tutorial.
In this post we will show you PHP and MySqli CRUD Operation Tutorial, hear for PHP and MySqli CRUD Operation Tutorial we will give you demo and example for implement.
In this post, we will learn about PHP and MySqli CRUD Operation Tutorial with an example.

Mysqli – SELECT Statement | mysqli select query

//all the get products list
function getProducts() {

	// simple Connect to the database
	$mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);

	// all the output any connection error
	if ($mysqli->connect_error) {
	    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
	}

	// the query in mysqli
	$query = "SELECT * FROM products ORDER BY product_desc ASC";

	// mysqli select query
	$results = $mysqli->query($query);

	if($results) {

		print '<table class="table table-striped table-bordered" id="data-table"><thead><tr>

				<th><h4>Product</h4></th>
				<th><h4>Description</h4></th>
				<th><h4>Action</h4></th>

			  </tr></thead><tbody>';

		while($row = $results->fetch_assoc()) {

		    print '
			    <tr>
					<td>'.$row["product_name"].'</td>
				    <td>'.$row["product_desc"].'</td>
				    <td><a href="product-edit.php?id='.$row["product_id"].'" class="btn btn-primary btn-xs"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a> </td>
			    </tr>
		    ';
		}

		print '</tr></tbody></table>';

	} else {

		echo "<p>There are no products to display.</p>";

	}

	// Simple Frees the memory(all memory) associated with a result
	$results->free();

	//mysqli close connection 
	$mysqli->close();
}

Mysqli – SELECT Update | mysqli Update query

// Update product list
if($action == 'update_product') {

	// simple output any connection error
	if ($mysqli->connect_error) {
	    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
	}

	// latest product information
	$getID = $_POST['id']; // id
	$product_name = $_POST['product_name']; // product name
	$product_desc = $_POST['product_desc']; // product desc

	// the query - update
	$query = "UPDATE products SET
				product_name = ?,
				product_desc = ?
			 WHERE product_id = ?
			";

	/*simple  Prepare statement */
	$stmt = $mysqli->prepare($query);
	if($stmt === false) {
	  trigger_error('Wrong SQL: ' . $query . ' Error: ' . $mysqli->error, E_USER_ERROR);
	}

	/*all the Bind parameters. TYpes: s = string, i = integer, d = double,  b = blob */
	$stmt->bind_param(
		'sss',
		$product_name,$product_desc,$getID
	);

	//execute the query
	if($stmt->execute()){
	    //if saving success
		echo json_encode(array(
			'status' => 'Success',
			'message'=> 'your Product has been updated successfully.!'
		));

	} else {
	    //if check condition unable to create new record
	    echo json_encode(array(
	    	'status' => 'Error',
	    	//'message'=> 'There has been an error, please try again.'
	    	'message' => 'There has been an error, please try again.<pre>'.$mysqli->error.'</pre><pre>'.$query.'</pre>'
	    ));
	}

	//mysqli close database connection here
	$mysqli->close();
	
}

Mysqli – DELETE | mysqli DELETE query

if($action == 'delete_product') {

	// DELETE functionality output any connection error
	if ($mysqli->connect_error) {
	    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
	}

	$id = $_POST["delete"];

	//DELETE the query 
	$query = "DELETE FROM products WHERE product_id = ?";

	/* DELETE Prepare statement */
	$stmt = $mysqli->prepare($query);
	if($stmt === false) {
	  trigger_error('Wrong SQL: ' . $query . ' Error: ' . $mysqli->error, E_USER_ERROR);
	}

	/* Bind parameters. TYpes: s = string, i = integer, d = double,  b = blob */
	$stmt->bind_param('s',$id);

	//DELETE execute the query
	if($stmt->execute()){
	    //if saving success
		echo json_encode(array(
			'status' => 'Success',
			'message'=> 'Product has been deleted successfully!'
		));

	} else {
	    //if unable to create new record
	    echo json_encode(array(
	    	'status' => 'Error',
	    	//'message'=> 'There has been an error, please try again.'
	    	'message' => 'There has been an error, please try again.<pre>'.$mysqli->error.'</pre><pre>'.$query.'</pre>'
	    ));
	}
	// close connection 
	$mysqli->close();
}

Also Read :  Login and Signup System using PHP MySQLi

Example

We hope you get an idea about PHP and MySqli CRUD Operation Tutorial
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.

Leave a Reply

Your email address will not be published. Required fields are marked *