开发者

MySQL Wrapper and fetch rows

开发者 https://www.devze.com 2023-01-24 13:11 出处:网络
I found simple database wrapper at http://www.phpclasses.org/browse/file/24355.html but I cannot fetch rows

I found simple database wrapper at http://www.phpclasses.org/browse/file/24355.html but I cannot fetch rows

<?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
    {
 开发者_StackOverflow社区     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);
  }

}

Below is the code for fetch rows:

include '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];

}


you need SelectDb first


I cannot see why this wouldnt work. Only thing I would change is on your echo statement

echo $row[id];

to

echo $row['id'];

Also as mentioned below you need to select the DB you want to work on, We are not refering to the table but the database the table belongs to

You need to use this:

$db = DBConnect::SelectDb("dbname");
0

精彩评论

暂无评论...
验证码 换一张
取 消