1

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
    {
      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];

}

2 Answers 2

1

you need SelectDb first

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I forgot: $mysql_result = DBConnect::Select("SELECT * FROM myTable");
i mean its not enough to connect the db you need to select the database you want to work on it!
0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.