Skip to content

Instantly share code, notes, and snippets.

@Brobin
Last active August 29, 2015 14:02
Show Gist options
  • Save Brobin/7ff74a924f6618e3c4e0 to your computer and use it in GitHub Desktop.
Save Brobin/7ff74a924f6618e3c4e0 to your computer and use it in GitHub Desktop.
Examples of mysqli for personal reference
<?php
/**
* These variables (strings) contain data to connect to the database
*/
$hostname = "";
$username = "";
$password = "";
$database = "";
/**
* Establishes a connection with the database
*/
$mysqli = new mysqli($this->hostname, $this->username, $this->password, $this->database);
if($mysqli->connect_errno > 0){
die('Unable to connect to database [' . $mysqli->connect_error . ']');
}
return $mysqli;
/**
* Retrieveing multiple things
*/
$result = $mysqli->query("SELECT * FROM posts ORDER BY id DESC");
$posts = array();
while($row = $result->fetch_assoc()) {
$post = new Post($row);
$posts[] = $post;
}
/**
* Getting one thing
*/
$result = $mysqli->query("SELECT name FROM categories WHERE id =".$id);
$row = $result->fetch_assoc();
return $row['name'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment