Source for file mysql.php
Documentation is available at mysql.php
* dbscript for PHP 4 & 5 - restful crud framework
* @version 0.1.2 -- 19-Feb-2007
* @author Brian Hendrickson <brian@dbscript.net>
* @link http://dbscript.net/
* @copyright Copyright 2007 Brian Hendrickson
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* adapter for the MySQL database system
* $db = new MySQL ( 'hostname', 'database_name', 'username', 'password' );
* {@link http://dbscript.net/mysql}
* @author Brian Hendrickson <brian@dbscript.net>
* @todo support array datatypes
'float' => 'float', // precise to 23 digits
'double' => 'float', // 24-53 digits
'decimal' => 'float', // double stored as string
$argnames = array('host','dbname','user','pass','opt1','opt2'); for ($i = 0; $i < count($args); $i++ ) {
$this->$argnames[$i] = $args[$i];
$this->alias_array = array();
function connect() { /* function to re/establish the DB connection */
function escape_string( $string ) { /* watch for bad characters in each SQL query */
if (!(strlen($string) > 0)) { return $string; }
function get_result( $sql, $returnfalse = NULL ) { /* run an SQL query */
if (isset ($request->params)) {
$request->set_param( 'currentquery', $sql );
if (!$result && $returnfalse == NULL) {
} elseif (!$result && $returnfalse) {
function last_insert_id( &$result, $pk, $table ) { /* returns the id of the most recently modified record */
function result_value( &$result, $resultindex, $field ) { /* get a single value from a result set */
if (!$res && $res != 0) {
if ( isset ( $args[0] ) ) {
header( "Location:" . $args[0] );
if ( isset ( $this->models[$table] ) )
require_once $data_model;
function &model($model) {
return " LIMIT " . $offset . "," . $limit;
$ret['k'] = $rec->primary_key;
$ret['i'] = $rec->attributes[$rec->primary_key];
$sql = "INSERT INTO " . $rec->table . " (";
foreach (array_unique($rec->modified_fields) AS $modified_field) {
$this->pre_insert( $rec, $modified_field, $datatype );
if ( !( $datatype == 'blob' && ( !(strlen( $rec->attributes[$modified_field] ) > 0 ) ) ) ) {
$fields .= $comma . $modified_field;
$sql .= $fields . ") VALUES (" . $values . ")";
$sql .= $rec->table . ' SET ';
foreach (array_unique($rec->modified_fields) AS $modified_field) {
$this->pre_update( $rec, $modified_field, $datatype );
if ( !( $datatype == 'blob' && ( !(strlen( $rec->attributes[$modified_field] ) > 0 ) ) ) ) {
$sql .= " WHERE " . $rec->primary_key . "='" . $rec->attributes[$rec->primary_key] . "'";
return "SELECT ". $rec->selecttext. " FROM ". $rec->table. " WHERE ". $rec->primary_key. " = '". $id. "'";
$sql = 'DELETE FROM ' . $rec->table . ' WHERE ' . $rec->primary_key . ' = ' . $rec->$pkfield;
return "SELECT DISTINCT $field, " . $this->models[$table]->primary_key . " FROM $table ORDER BY $orderby DESC";
return $modified_field . "='" . $this->escape_string($rec->attributes[$modified_field]) . "'";
return "'" . $this->escape_string($rec->attributes[$modified_field]) . "'";
function pre_insert( &$rec, $modified_field, $datatype ) {
if (isset ($this->models[$rec->table]->field_attrs[$modified_field]['required'])) {
if (!(strlen( $rec->attributes[$modified_field] ) > 0))
trigger_error( "$modified_field is a required field", E_USER_ERROR );
if (isset ($this->models[$rec->table]->field_attrs[$modified_field]['unique'])) {
$result = $this->get_result("select ". $modified_field. " from ". $rec->table. " where ". $modified_field. " = '". $rec->attributes[$modified_field]. "'");
trigger_error( "$modified_field must be unique!", E_USER_ERROR );
if ($datatype == 'time' && !(strlen($rec->attributes[$modified_field]) > 0))
$rec->attributes[$modified_field] = date("Y-m-d H:i:s",strtotime("now"));
$rec->attributes[$modified_field] = & $this->large_object_create( $rec->table, $rec->attributes[$modified_field] );
if ($datatype == 'bool') {
$rec->attributes[$modified_field] = "1";
$rec->attributes[$modified_field] = "false";
function pre_update( &$rec, $modified_field, $datatype ) {
if (isset ($this->models[$rec->table]->field_attrs[$modified_field]['required'])) {
if (!(strlen( $rec->attributes[$modified_field] ) > 0))
trigger_error( "$modified_field is a required field", E_USER_ERROR );
if (isset ($this->models[$rec->table]->field_attrs[$modified_field]['unique'])) {
$result = $this->get_result("select ". $modified_field. " from ". $rec->table. " where ". $modified_field. " = '". $rec->attributes[$modified_field]. "' and ". $rec->primary_key. " != '". $rec->attributes[$rec->primary_key]. "'");
trigger_error( "$modified_field must be unique!", E_USER_ERROR );
if ($datatype == 'bool') {
$rec->attributes[$modified_field] = "1";
$rec->attributes[$modified_field] = "false";
if ( ($datatype == 'blob') && (strlen( $rec->attributes[$modified_field] ) > 0) ) {
if ( strlen( $rec->attributes[$modified_field] ) > 0 ) {
$rec->attributes[$modified_field] = & $data;
if (!$result) { trigger_error("Sorry, the record could not be saved due to a database error.", E_USER_ERROR ); }
$pkfield = $rec->primary_key;
$rec->attributes[$pkfield] = $pkvalue;
$rec->$pkfield = & $rec->attributes[$pkfield];
$handle = fopen($file,"r");
if (!$handle) { trigger_error("Error creating large object in fopen", E_USER_ERROR ); }
if (!$buffer) { trigger_error("Error creating large object in fread", E_USER_ERROR ); }
if (!$result) { trigger_error("Error creating large object in fclose", E_USER_ERROR ); }
$sql = "SELECT $blobcol FROM $table WHERE $pkfield = '$pkvalue'";
if (!(count($field_array)> 0)) trigger_error( "Error creating table, no fields are defined. Use \$model->auto_field and \$model->text_field etc.", E_USER_ERROR );
$sql = "CREATE TABLE $table (";
foreach ( $field_array as $field => $data_type ) {
$sql .= "$comma $field $data_type";
function add_field( $table, $field, $data_type ) {
$sql = "ALTER TABLE $table ADD COLUMN $field $data_type";
$sql = "SHOW tables FROM ". $this->dbname;
foreach($arr as $key=> $value) {
$sql = "SHOW columns FROM $table";
if (!$result) return $datatypes;
foreach($arr as $key=> $value) {
} elseif ($key == "Type") {
} elseif ($key == "Key") {
$datatypes[$table. "_primary_key"] = $field; // yuck
$datatypes[$field] = $type;
* data model for a single MySQL table
* $people =& $db->model( 'people' );
* {@link http://dbscript.net/mysqltable}
* @author Brian Hendrickson <brian@dbscript.net>
$db->models[$table] = & $this;
$this->params = array('resource'=> $table);
$this->set_field( $field, "int(11) not null auto_increment primary key" );
function get_query( $id= NULL, $find_by= NULL ) {
$sql .= "$table.$pkfield as \"$table.$pkfield\", " . "\n";
foreach ($this->field_array as $fieldname=> $datatypename) {
if (!(!(strpos($fieldname,".") === false)))
$fieldname = $table . "." . $fieldname;
$fieldstring .= "$fieldname as \"$fieldname\", " . "\n";
if (count($relfields) > 0) {
foreach ($relfields as $key=> $val) {
$spl = split("\.",$val["fkey"]);
if (($val["type"] != 'child-many') && isset ($db->models[$spl[0]]))
foreach ($relfields as $key=> $val) {
$spl = split("\.",$val["fkey"]);
if (($val["type"] == 'child-many') || !(isset ($db->models[$spl[0]]))) {
foreach ($db->models[$spl[0]]->field_array as $fieldname=> $datatypename) {
$fieldstring .= $spl[0]. ".". $fieldname. " as \"". $spl[0]. ".". $fieldname. "\", " . "\n";
$leftsql .= " left join " . $spl[0] . " on ". $table. ".". $val["col"]. " = " . $val["fkey"];
$fieldstring = substr($fieldstring,0,- 3) . " " . "\n";
$sql .= " WHERE $table.$find_by = '$id' ";
$this->orderby = $table . "." . $pkfield;
if (!(isset ($this->order))) {
if (!(isset ($this->offset))) {
if (!(isset ($this->limit))) {
$sql .= " ORDER BY " . $this->orderby . " ";
|