Code has been added to clipboard!

MySQL Database for MySQLi Object-Oriented Way

Example
<?php
  $server = 'server'; 
  $user = 'user'; 
  $pass = 'pass'; 
  $db = 'db';
  // establish connection
  $con = new mysqli($server, $user, $pass, $db);
  // see if connection has been established properly
  if ($conn->connect_error) { 
    die("Failed to connect: " . $con->connect_error);
  } 
  $sql = "SELECT user_id, name, surname FROM users";
  $result = $con->query($sql);
  if ($result->num_rows > 0) {
    // display data from the query
    while($row = $result->fetch_assoc()) {
      echo "User Id: " . $row["user_id"]. " - Name: " . $row["name"]. " - Surname: " . $row["surname"]. "<br/>";
    }
  } else {
    echo "0 results";
  }
  $con->close();  
?>