Press "Enter" to skip to content

PHP MySQL Read Data

Html template.

First create a folder named phpCrud then inside the folder create 3 files config.php, Database.php and read.php. Inside the read.php file pest the html code then open config.php file and pest the code then last open the Database.php file and pest the code i have given.

If you can not create the mysql database you can see video from youtube how mysql database create then you give the database name in config file.


<!doctype html>
<html>
<head>
 <title>PHP OOP CRUD</title>
 <style>
		 body{font-family:verdana}
	    .phpcoding{width:900px; margin: 0 auto;
		 background:#ddd}
	    .headeroption, .footeroption{background:#444;
		color:#fff;text-align:center;padding:20px;}
	    .headeroption h2, .footeroption h2{margin:0;font-size:24px}
	    .maincontent{min-height:400px;padding:20px;font-size:18px}
		 p{margin:0}
		 input[type="text"]{width:238px;padding:5px;}
		 select{font-size:18px;padding:2px 5px;width:250px;}
		 .tblone{width:100%;border:1px solid #fff;margin:20px 0}
		 .tblone td{padding:5px 10px;text-align:center;}
		 table.tblone tr:nth-child(2n+1){background:#fff;height:30px;}
		 table.tblone tr:nth-child(2n){background:#f1f1f1;height:30px;}
		 #myform{width:400px;border:1px solid #fff;padding:10px;}
 </style>
</head>
<body>

<div class="phpcoding">
 <section class="headeroption">
  <h2>CRUD Using OOP PHP and MYSQLi</h2>
 </section>
  <section class="maincontent">
  
  
  
  <table class="tblone">
	<tr>
		 <th width="10%">Serial</th>
		 <th width="35%">Name</th>
		 <th width="25%">Email</th>
		 <th width="15%">Skill</th>
		 <th width="15%">Action</th>
	</tr>
	<?php if($read){?>
	<?php
	$i=1;
	while($row = $read->fetch_assoc()){
	?>
	<tr>
		 <td><?php echo $i++ ?></td>
		 <td><?php echo $row['name']; ?></td>
		 <td><?php echo $row['email']; ?></td>
		 <td><?php echo $row['skill']; ?></td>
		 <td>
		 <a href="update.php?id=<?php echo urlencode($row['id']); ?>">Edit</a>
		 <a href="?delete=<?php echo urlencode($row['id']); ?>">Delete</a>
		 </td>
	</tr>

	<?php } ?>
	<?php } else { ?>
	<p>Data is not available !!</p>
	<?php } ?>

  </table>
  

</section>
<section class="footeroption">
<p>&copy 2020 I Am a PHP Learner</p>
<h2>www.programsolve.com</h2>
</section>
</div>
</body>
</html>

Config file.

First we create php file and the name of the file will be config.php, in config file we write this line of codeā€¦

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "db_crud");

Database file creation.

Here we follow full php oop method so, we use data insert file and the file name will be Database.php. This file will include the config.php file.

<?php
class Database{
	public $host   = DB_HOST;
	public $user   = DB_USER;
	public $pass   = DB_PASS;
	public $dbname = DB_NAME;
	
	public $link;
	public $error;
	
	public function __construct(){
		$this->connectdb();
	}
	
	private function connectdb(){
		$this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
		if(!$this->link){
			$this->error = "connection fail".$this->link->connect_error;
			return false;
		}
	}

	// Select Data
	public function select() {
        $query = "SELECT * FROM tbl_user";
        $result = mysqli_query($this->link ,$query) or die($this->link->error . __LINE__);
        if ($result->num_rows > 0) {
            return $result;
        } else {
            return false;
        }
    }

Finally i use this line of code inside the html file which name i gave read.php. This line of code need to pest inside body tag.

<?php 
	include "config.php";
	include "Database.php";
?>

<?php
	 $db = new Database();
	 $read = $db->select();
?>

<?php
	if(isset($_GET['msg'])){
	 echo "<span style='color:green'>".$_GET['msg']."</span>";
}
?>

At last i give some picture of file and after the code.

Files folder picture.

Second after run the code.

One Comment

  1. […] For making the delete functionality no need to make other html template. This functionality will work in read page. So, for that i will not the html template here. Only i will give the read page link, for the link click here… […]

Leave a Reply

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