Cara Cara

untung99.homes: PHP MySQL Select Data With WHERE Clause


Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.

Berikut adalah artikel atau berita tentang Harian untung99.homes dengan judul untung99.homes: PHP MySQL Select Data With WHERE Clause yang telah tayang di untung99.homes terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di koresponden@untung99.homes, Terimakasih.


Select and Filter Data From a MySQL Database

The WHERE clause is used to filter records.

The WHERE clause is used to extract only those records that fulfill a
specified condition.

SELECT column_name(s)
FROM table_name WHERE column_name operator value 

To learn more about SQL, please visit our SQL tutorial.


Select and Filter Data With MySQLi

The following example selects the id, firstname and lastname columns from the MyGuests
table where the lastname is “Doe”, and displays it on the page:

Example (MySQLi Object-oriented)

$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die(“Connection failed: ” . $conn->connect_error);
}




$sql = “SELECT id, firstname, lastname FROM MyGuests WHERE
lastname=’Doe'”;
$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”]. “
“;
  }
} else {
  echo “0 results”;
}
$conn->close();
?>










Run example »

Code lines to explain from the example above:

First, we set up the SQL query that selects the id, firstname and lastname columns from the MyGuests
table where the lastname is “Doe”. The next line of code runs the query and puts the resulting data into a
variable called $result.

Then, the function num_rows() checks if there are more than zero
rows returned.

If there are more than zero rows returned, the
function fetch_assoc() puts all the results into an associative array that we can loop
through. The while() loop loops through the result set and outputs the data from
the id, firstname and lastname columns.

The following example shows the same as the example above, in the MySQLi procedural way:

Example (MySQLi Procedural)

$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;


// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die(“Connection failed: ” . mysqli_connect_error());
}




$sql = “SELECT id, firstname, lastname FROM MyGuests
WHERE lastname=’Doe'”;
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
  // output data of each row
  while($row = mysqli_fetch_assoc($result)) {
    echo “id: ” . $row[“id”]. ” – Name: ” . $row[“firstname”]. ” ” . $row[“lastname”]. “
“;
  }
} else {
  echo “0 results”;
}







mysqli_close($conn);
?>

Run example »



You can also put the result in an HTML table:

Example (MySQLi Object-oriented)

$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die(“Connection failed: ” . $conn->connect_error);
}




$sql = “SELECT id, firstname, lastname FROM MyGuests WHERE
lastname=’Doe'”;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  echo “”;
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo “”;
  }
  echo “

ID Name
“.$row[“id”].” “.$row[“firstname”].” “.$row[“lastname”].”

“;
} else {
  echo “0 results”;
}
$conn->close();
?>














Run example »


Select Data With PDO (+ Prepared Statements)

The following example uses prepared statements.

It selects the id, firstname and lastname columns from the MyGuests table
where the lastname is “Doe”, and
displays it in an HTML table:

Example (PDO)

echo “”;
echo “”;

class TableRows extends RecursiveIteratorIterator {
  function __construct($it) {
    parent::__construct($it, self::LEAVES_ONLY);
  }


  function current() {
    return “

“;
  }

  function beginChildren() {
    echo “

“;
  }

  function endChildren() {
    echo “

” . “\n”;
  }
}

$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDBPDO”;


try {
  $conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $stmt = $conn->prepare(“SELECT id, firstname, lastname FROM MyGuests
WHERE lastname=’Doe'”);
  $stmt->execute();



  // set the resulting array to associative
  $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
    echo $v;
  }
}
catch(PDOException $e) {
  echo “Error: ” . $e->getMessage();
}
$conn = null;
echo “









Id Firstname Lastname
” . parent::current(). “

“;
?>






Run example »