Example #1
0
 /**
  * Instantiates API object and creates MySQLi database connection
  * @param string $host The hostname where the database will be running (often "localhost");
  * @param string $db The database name
  * @param string $table The table name the api will use
  * @param string $username The username for the database
  * @param string $password The password for the database
  */
 public function __construct($host, $db, $table, $username, $password)
 {
     $this->config_errors = array();
     if (!Database::init_connection($host, $db, $table, $username, $password)) {
         $json_obj = new StdClass();
         $json_obj->config_errors[] = "database connection failed, make sure the connection info passed into API::__construct(\$host, \$db, \$table, \$username, \$password) is correct";
         die(json_encode($json_obj));
     }
 }
Example #2
0
<?php

require_once '../api_builder_includes/class.Database.inc.php';
Database::init_connection("localhost", "organization", "users", "username", "secret_password");
// Array containing the row to change. The only required values are the id and the column being changed.
// All other key => value pairs are ignored but are present here because often rows are updated in batch
// after being returned in 2D array fashion from Database::get_all_results();
$user = array("id" => 2, "first_name" => "Salvester", "last_name" => "Rinehart", "email" => "*****@*****.**", "phone_number" => "8042557684", "city" => "Richmond", "state" => "VA", "bio" => "Total badass.");
//sanitize user input
$user_cleaned = Database::clean($user);
//the 3rd parameter specifies this is an update statement by selecting which column from in the row to update
if (Database::execute_from_assoc($user_cleaned, Database::$table, "phone_number")) {
    echo $user['first_name'] . "'s phone number was changed to " . $user['phone_number'];
}