MySQL get data from database code example

Example 1: mysql get data from database

//creating a connection to the db
$conn = new mysqli('host ip addr','uname','pwd','dbname');


//the selection query with a where clause to filter out the data
$sel = "SELECT * FROM `table` WHERE `something`='something'";

//the result returned from the query
$result = $conn->query($sel);

//checking if there are more than one rows in the result
if($result->num_rows > 0){
  
  	//getting all the rows from the result with a while loop
	 while($row = $result->fetch_assoc()){
     	 echo $row['data1'] . ' ' . $row['data2'];//get all the data from the row array
     }
}

Example 2: php + get sql query

echo $select->__toString();

Tags: