/ Published in: PHP
URL: http://www.mechanicmatt.com
Similar to my OOP MySQL class, but converted to use the OOP style of MySQLi
Expand |
Embed | Plain Text
<?php class database_mysqli { var $host = NULL; var $username = NULL; var $password = NULL; var $databaseName = NULL; var $link = NULL; var $queries = NULL; var $errors = NULL; function database_mysqli($host, $username, $password, $database) { $this->host = $host; $this->databaseName = $database; $this->link = ""; $this->link = new mysqli($this->host, $username, $password, $database) or die("Could not connect to Database"); } function justquery($sql) { $this->queries[] = $sql; return $this->link->query($sql); } function loadResult($sql) { if (!($cur = $this->justquery($sql))) { return null; } $ret = null; if ($row = $cur->fetch_row()) { $ret = $row[0]; } mysqli_free_result( $cur ); return $ret; } function loadFirstRow($sql) { if (!($cur = $this->justquery($sql))) { return null; } $ret = null; if ($row = $cur->fetch_object()) { $ret = $row; } mysqli_free_result( $cur ); return $ret; } function insertid() { return $this->link->insert_id; } function query($sql, $key = "", $returns = true, $batch = false) { switch ($batch) { default: case true: foreach ($sql as $index => $query) { $this->queries[] = $query; $answer = $this->link->query($query); if (!$answer) { $this->errors[] = $this->link->error; } else { if ($returns != false) { if ($answer->num_rows > 0){ while ($row = $answer->fetch_object()) { if ($key != ""){ $result[$index][$row->$key] = $row; } else { $result[$index][] = $row; } } } else {} } else {} } } break; case false: $this->queries[] = $sql; $answer = $this->link->query($sql); if (!$answer) { $this->errors[] = $this->link->error; $result = false; } else { if ($returns != false) { if ($answer->num_rows > 0){ while ($row = $answer->fetch_object()) { if ($key != ""){ $result[$row->$key] = $row; } else { $result[] = $row; } } } else {} } else { $result = true; } } break; } return $result; } function loadObject( $sql, &$object ) { if ($object != null) { if (!($cur = $this->justquery($sql))) { return false; } else {} if ($array = $cur->fetch_assoc()) { mysqli_free_result( $cur ); $this->bindArrayToObject( $array, $object); return true; } else { return false; } } else { if ($cur = $this->justquery($sql)) { if ($object = $cur->fetch_object(W)) { mysqli_free_result( $cur ); return true; } else { $object = null; return false; } } else { return false; } } } function bindArrayToObject( $array, &$obj) { return (false); } $ak = $k; $obj->$k = $array[$ak]; } } } return true; } function formatCSVCell($data) { $useQuotes = false; "\"" => "\"\"", "," => ",", "\n" => "\n" ); foreach ($quotable as $char => $repl) { $useQuotes = true; } else {} } if ($useQuotes == true) { foreach ($quotable as $char => $repl) { } $data = "\"" . $data . "\""; } else { } return $data; } } ?>
You need to login to post a comment.
