Answers for "retrieve data from mysql database"

PHP
17

query sql in php

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
Posted by: Guest on February-05-2020
0

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
     }
}
Posted by: Guest on June-05-2021

Code answers related to "retrieve data from mysql database"

Browse Popular Code Answers by Language