| mysqli_connect | connect to MySQL Server instance |
| mysqli_select_db | select database |
| mysqli_query | issue query to table/view/stored procedure/function. |
| mysqli_fetch_array | fetch result set based on query. single/multiple |
| mysqli_error | if any error occurs detailed error message will be included in this |
Here I have sample EMP table
1) Database Name "HR"
2) Table Name : "EMP"
3) EMP table has 3 columns 1.idEMP 2.ename,3.SalStep 1) Connect to Database using mysqli_connect
$link = mysqli_connect('localhost','root','password');
HostName: Localhost
Username:root
Password:your password
Note: If MySQL server instance is running under port 3306, u can type hostname as localhost:3306
Step 2) Select database in MySQL
mysqli_select_db($link,'HR'))
Here HR is the Database Name
Step 3) Issue DML query to a Table i.e EMP table
$result=mysqli_query($link, "select * from emp");
Query: select * from emp
result set: $result.
Step 4) Fetch data from Result Set
$row=mysqli_fetch_array($result))
OUTPUT:
| Emp ID | Emp Name | Salary |
|---|---|---|
| 2 | shyam | 5500.56 |
| 3 | benegal | 5555.00 |
Here is the Complete Source Code
<?php
$link = mysqli_connect('localhost','root','password');
if(!$link)
{
$output='Unable to connect to database server'; echo $ouput;
exit();
}
if(!mysqli_select_db($link,'HR'))
{
$output = 'Unable to locate the HR database.';
echo $output;
exit();
}
$result=mysqli_query($link, "select * from emp");
if(!$result)
{
$error = 'Error ...'.mysqli_error($link);
echo $error;
exit();
}
echo "success";
$output="<table border='2' align='center'>";
$output.="<tr style='background-color:teal;color:white;'><th>Emp ID</th><th>Emp Name</th><th>Salary</th></tr>";
while($row=mysqli_fetch_array($result))
{
$output.="<tr><td>".$row['idEMP']."</td><td>".$row['ename']."</td><td>".$row['Sal']."</td></tr>";
}
$output.="</table>";
echo $output,"<br>";
?>
Tags:How to query MySQL Table using PHP,Query MySQL in PHP,Connect to Database using Mysqli in PHP, Query MySQL database using PHP, Retreive mysql Database in PHP, PHP & MYSQL Query,mysqli_fetch_array,mysqli_error,mysqli_select_db,mysqli_connect

No comments:
Post a Comment