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 '';

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

		    print '
			    
		    ';
		}

		print '

Product

Description

Action

'.$row["product_name"].' '.$row["product_desc"].'
'; } else { echo "

There are no products to display.

"; } // 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.
'.$mysqli->error.'
'.$query.'

'
));
}

//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.
'.$mysqli->error.'
'.$query.'

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

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 Comment