我在http://www.phpclasses.org/browse/file/24355.html找到了简单的数据库包装器,但我无法获取行
<?php
class DBConnect
{
static public $HOST;
static public $USER;
static public $PASS;
static public $BASE;
static private $LINK = -1;
public function Prepare($h,$u,$p,$b)
{
self::setHost($h);
self::setUser($u);
self::setPass($p);
self::setBase($b);
}
public function setHost($h) { self::$HOST = $h; }
public function setUser($u) { self::$USER = $u; }
public function setPass($p) { self::$PASS = $p; }
public function setBase($b) { self::$BASE = $b; }
private function setLink($l) { self::$LINK = $l; }
public function getHost() { return self::$HOST; }
public function getUser() { return self::$USER; }
public function getPass() { return self::$PASS; }
public function getBase() { return self::$BASE; }
public function getLink()
{
if( self::$LINK != -1 )
{
return self::$LINK;
}
else
{
try
{
$link = mysql_connect(self::getHost(),self::getUser(),self::getPass());
self::setLink($link);
}
catch(Exception $e)
{
echo "Could not establish a link to the specified database.\n" . $e->getMessage();
self::setLink(-1);
}
return self::$LINK;
}
}
public function SelectDb($db='')
{
try
{
if( $db == '' )
mysql_select_db(self::getBase(),self::getLink());
else
mysql_select_db($db,self::getLink());
}
catch(Exception $e)
{
echo "Could not select the specified database '$db'.\n" . $e->getMessage();
}
}
public function Select($q)
{
try
{
$result = mysql_query($q,self::getLink());
if( is_resource($result) )
return $result;
}
catch(Exception $e)
{
echo "Could not query datasource.\n" . $e->getMessage();
}
return 0;
}
public function Insert($q)
{
try
{
mysql_query($q,self::getLink());
return mysql_insert_id(self::getLink());
}
catch( Exception $e )
{
echo "Bad insert: " . $e->getMessage();
return -1;
}
}
public function Update($q)
{
return mysql_query($q,self::getLink());
}
public function Delete($q)
{
return mysql_query($q,self::getLink());
}
public function getRow($r)
{
if( is_resource($r) ) return mysql_fetch_assoc($r);
return null;
}
public function Close()
{
if( is_resource(self::$LINK) ) mysql_close(self::$LINK);
}
public function Escape($s)
{
return mysql_real_escape_string($s,self::$LINK);
}
}
以下是获取行的代码:
包括'DBConnect.php';
DBConnect::Prepare("127.0.0.1","myUser","myPass","mySchema");
$mysql_result = DBConnect::Select("SELECT * FROM myTable");
while($row = DBConnect::getRow($mysql_result)){
echo $row[id];
}
答案 0 :(得分:1)
首先需要SelectDb
答案 1 :(得分:0)
我不明白为什么这不起作用。我唯一想改变的是你的echo语句
echo $row[id]; to echo $row['id'];
同样如下所述,你需要选择你想要处理的数据库,我们没有引用表,而是表所属的数据库
你需要使用它:
$db = DBConnect::SelectDb("dbname");