06-05-2008, 07:33 PM
|
#2 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
PHP: MySQL - Manual
essentially if you just want to get back an array of items from the database
PHP Code:
$aInfo = array();
//aInfo is now an associative array with all of 'db' info
$aInfo = mysql_fetch_assoc(mysql_query("SELECT * FROM `db`"));
Personally, I hate having to write functions over and over again so I just use this wrapper I made a fair long while ago - essentially you use it like so
PHP Code:
$aDbInfo = array('localhost','root','password','db');
$objDb = new mysql_wrap($aDbInfo);
$objInfo = $objDb->query("SELECT `username` WHERE `email`='%s'",$email);
echo $objInfo->fields['username']
[/php]
Though you might want to look up mysqli - or have someone speak of it, I'm not very learned with it.
The aforementioned class.
PHP Code:
<?php
define("NO_FIELDS",'--accept-none--');
class mysql_wrap{
private $connectionID;
private $queryID;
private $fieldCount;
public function __construct($host,$user='',$pass='',$table=''){
if(is_array($host)){
$host = $host['host'];
$user = $host['user'];
$pass = $host['pass'];
$table = $host['table'];
}
$this->connectionID = mysql_connect($host,$user,$pass) or user_error("Connection to MySQL server could not be made");
mysql_select_db($table);
}
public function reqs(){
}
public function query(){
$args = func_get_args();
$argCount = func_num_args();
if($argCount == 1){
$sql = $args[0];
$this->queryID = mysql_query($sql);
$this->fields = @mysql_fetch_assoc($this->queryID);
$this->sqlFieldCount = @mysql_num_rows($this->queryID);
return;
}elseif($argCount == 2){
if($args[1] == '--accept-none--'){
$this->queryID = @mysql_query($sql);
return;
}
}
if(is_array($args[0])){
$args = $args[0];
$argCount = count($args);
}
if($args[$argCount-1] == '--accept-none--'){
$input = array_splice($args,0,$argCount-1);
$sql = array_splice($input,0,1);
//print_r($input);
$input = call_user_func_array('mysql_real_escape_string',$input);
$sql = (is_array($sql)) ? $sql : array($sql);
$input = (is_array($input)) ? $input : array($input);
$this->queryID = mysql_query(call_user_func_array('sprintf',array_merge($sql,$input)));
return new Record($this->queryID,$this->connectionID);
}
$sql = array_splice($args,0,1);
$arguments = array_map('mysql_real_escape_string',$args);
$sql = (is_array($sql)) ? $sql : array($sql);
$arguments = (is_array($arguments)) ? $arguments : array($arguments);
$this->queryID = mysql_query(call_user_func_array('sprintf',array_merge($sql,$arguments)));
return new Record($this->queryID,$this->connectionID);
}
public function count(){
return mysql_num_rows($this->queryID);
}
}
class Record{
private $count;
private $curCount;
private $queryID;
private $connectionID;
public $fields;
public $EOF;
public function __construct($queryID,$connectionID){
$this->queryID = $queryID;
$this->connectionID;
$this->count = @mysql_num_rows($count);
$this->fields = @mysql_fetch_assoc($queryID);
}
public function movenext($len=0){
$this->fields = @mysql_fetch_assoc($this->queryID);
++$this->curCount;
if($len > 0 and $this->curCount >= $len){
$this->EOF = true;
}
if($this->curCount >= $this->count){
$this->EOF = true;
}
}
}
|
|
|
|