select query of mysql in php code example

Example 1: 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();

Example 2: get data from database in dropdown list php

<?php
$connect = new mysqli("127.0.0.1","root","");

			if ($connect -> connect_errno)
				{
			  		echo "Failed to connect to MySQL: " . $connect -> connect_error;
			  		exit();
				}

					$connect -> select_db("Your Database");
				if ($result = $connect -> query("Your query")) 
  				{
			  		
				}
				echo "<select>";
				while($row = mysqli_fetch_array($result)) 
				{
					echo "<option>" . $row['Your columns'] . "</option>"
				}
				echo "</select>"
?>

Tags:

Php Example