1/12/19

PHP & MySQli Coding References - Part 1 | Connect database | view data's


This is my first php and mysqli tutorial part -1.  In this tutorial, we're going to learn how to handle the database in php using mysql. I have a one database file. first of all We have to install the database file in mysql that link is here..

Database file
after download, follow my instructions...

how to install the datavase file in mysql.
1. create database name as filename into database name.
2. export the .sql file or run & excute the query.
3. that's all.

1. Database Connection
<?php $mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli)); ?>


2. Includes PHP Files
<?php require_once 'FileName.php'; ?>


3. View the table information
<?php
$result= $mysqli->query("SELECT * FROM data") or die(mysqli_error($mysqli));
pre_r($result);
?>

<?php
function pre_r($array)
{
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>


4. View the data of table one by one
<?php
$mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli));
$result= $mysqli->query("SELECT * FROM data") or die(mysqli_error($mysqli));

// We have listed out 3 raws in data table
pre_r($result->fetch_assoc());
pre_r($result->fetch_assoc());
pre_r($result->fetch_assoc());
?>

<?php
function pre_r($array)
{
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>


5. How to view the data in table format
<?php
$result= $mysqli->query("SELECT * FROM data") or die(mysqli_error($mysqli));
?>

<div class="row justify-content-center">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Location</th>
<th clospan="2">Action</th>
</tr>
<tbody>

//Starting while loop
<?php while ($row = $result->fetch_assoc()): ?>

<tr>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['location'];?></td>
</tr>

//End of the while loop
<?php endwhile; ?>

</tbody>
</thead>
</table>
</div>

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *