Tuesday 20 February 2018

PHP MySQL Select Query

Select statement:
It is used to retrieve data from table.
Syntax:
SELECT statement is used to select data from one or more tables:
SELECT column_name(s) FROM table_name;
We can use the * character to select ALL columns from a table
Select*from  table name;
Example:
select * from emp;

Types of MySQLi functions:
There are two other MySQLi functions used in select query.
1)mysqli_num_rows(mysqli_result $result): returns number of rows.
2)mysqli_fetch_assoc(mysqli_result $result): returns row as an associative array. Each key of the array represents the column name of the table. It returns NULL if there are no more rows

Example:
<?php
$servername= 'localhost’;
$username = 'root';
$password = '';
$dbname='lp'
$con = mysqli_connect($servername, $username, $password,$dbname);
if(! $con )
{
  die('Could not connect: ' . mysqli_error());
}
echo 'Connected  successfully';

$sql = 'select * from emp;'; 
$result=mysqli_query($con, $sql); 
 
if(mysqli_num_rows($result) > 0){ 
 while($row = mysqli_fetch_assoc($result)){ 
    echo "Employee Id :{$row['id']}  <br> ". 
         "Employee name: {$row['name']} <br> ". 
         "Employee salary: {$row['salary']} <br> ";
   }
  }

else{ 
echo "0 results"; 
}  
mysqli_close($con);
?>
Output:
Connected successfully
Employee Id: 101
Employee name: Dipak
Employee salary: 45500.78


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...