MySQL DB Query

<?php
	/* Your mysql server host name */
	$host = "localhost";
	
	/* Mysql database user name */
	$user = "root";
	
	/* Mysql database user password */
	$password = "k0i0n7g";
	
	/* Mysql database name */
	$database = "test";
	
	/* Open connection mysql server in mysqli procedural */
	$connect = mysqli_connect($host, $user, $password, $database);
	
	if($connect) {
		/* Create table query */
		$create = "CREATE TABLE users (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, email VARCHAR(200) NOT NULL, password VARCHAR(200) NOT NULL)";
		if(mysqli_query($connect, $create))
			echo "Table created successfully!";
		else
			echo "Error...Fail to create table!";
		
		/* Insert data into table query */
		$insert = "INSERT INTO users SET email = '[email protected]', password = md5('1234')";
		if(mysqli_query($connect, $insert))
			echo "<br />Data inserted successfully!";
		else
			echo "<br />Error...fail to insert data!";
		
		/* Fetch data from table query */
		$select = "SELECT * FROM users";
		$result = mysqli_query($connect, $select); 
		if(mysqli_num_rows($result) > 0) {
			while($row = mysqli_fetch_assoc($result))
				echo "<br />Email: ".$row['email']." Password: ".$row['password'] ;
		}
		else
			echo "<br />No data found!";
		
		/* Update table row query */
		$update = "UPDATE users SET email = '[email protected]' WHERE id = 1";
		if(mysqli_query($connect, $update))
			echo "<br />Data updated successfully!";
		else
			echo "<br />Error...fail to update data!";
		
		/* Delete table row query */
		$delete = "DELETE FROM users WHERE id = 1";
		if(mysqli_query($connect, $delete))
			echo "<br />Data deleted successfully!";
		else
			echo "<br />Error...fail to delete data!";
		
		/* Delete table from database query */
		$drop = "DROP TABLE IF EXISTS users";
		if(mysqli_query($connect, $drop))
			echo "<br />Table dropped successfully!";
		else
			echo "<br />Error...fail to drop table!";
		
	} 
	else
		die("<br />Connection to the Server could not be established!");
		
	mysqli_close($connect);
	
	
	
	
	/* Open connection mysql server in mysqli object oriented  */
	$connect = new mysqli($host, $user, $password, $database) or die("Connection to the Server could not be established!");
	
	if($connect) {
		/* Create table query */
		$create = "CREATE TABLE users (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, email VARCHAR(200) NOT NULL, password VARCHAR(200) NOT NULL)";
		if($connect->query($create))
			echo "Table created successfully!";
		else
			echo "Error...Fail to create table!";
		
		/* Insert data into table query */
		$insert = "INSERT INTO users SET email = '[email protected]', password = md5('1234')";
		if($connect->query($insert))
			echo "<br />Data inserted successfully!";
		else
			echo "<br />Error...fail to insert data!";
		
		/* Fetch data from table query */
		$select = "SELECT * FROM users";
		$result = $connect->query($select); 
		if($result->num_rows > 0) {
			while($row = $result->fetch_object())
				echo "<br />Email: ".$row->email." Password: ".$row->password;
		}
		else
			echo "<br />No data found!";
		
		/* Update table row query */
		$update = "UPDATE users SET email = '[email protected]' WHERE id = 1";
		if($connect->query($update))
			echo "<br />Data updated successfully!";
		else
			echo "<br />Error...fail to update data!";
		
		/* Delete table row query */
		$delete = "DELETE FROM users WHERE id = 1";
		if($connect->query($delete))
			echo "<br />Data deleted successfully!";
		else
			echo "<br />Error...fail to delete data!";
		
		/* Delete table from database query */
		$drop = "DROP TABLE IF EXISTS users";
		if($connect->query($drop))
			echo "<br />Table dropped successfully!";
		else
			echo "<br />Error...fail to drop table!";
	} 
		
	$connect->close();
?> 




/* Output */
Table created successfully!

Data inserted successfully!

Email: [email protected] Password: 81dc9bdb52d04dc20036dbd8313ed055

Data updated successfully!

Data deleted successfully!

Table dropped successfully!

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!