MySQL to MySQLi

Converting a posted script

Page 1 of 1

1 Replies - 5703 Views - Last Post: 07 March 2009 - 01:27 PM Rate Topic: -----

#1 Vomster  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 198
  • Joined: 15-May 08

MySQL to MySQLi

Posted 07 March 2009 - 11:56 AM

I've taken some code that joeyadms posted and changed it from using mysql to mysqli and I'm running into some problems.

It worked fine with the mysql in there but I really wanted to convert it to mysqli

If someone could guide me in the right direction here, I'd appreciate. I've researched the functions that are giving the problems but they seem correct to me but obviously I'm missing something.

The errors I'm getting are the following:
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home/html/ooconn.php on line 16

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/html/ooconn.php on line 42

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/html/ooconn.php on line 44

PHP
class DB{

private $host = 'stuff';
private $un = 'stuff';
private $pw = 'stuff';
private $db = 'stuff';
private static $_link;

public function __construct($db=null){
	if(!is_null($db)){
		$this->db = $db;
	}
	$this->_link = mysqli_connect($this->host,$this->un,$this->pw);
16-->	mysqli_select_db($this->db,$this->_link);
}

public function __destruct(){
	mysqli_close($this->_link);
}

	public function getInstance($db = null){
		if(is_null(self::$_link)){
			self::$_link = new DB($db);
		}
		return self::$_link;
	}

public function prepare($var){
	if(is_array($var)){
		foreach($var as $key=>$val){
			$clean[$key] = mysqli_real_escape_string($val,$this->_link);
		}
	} else {
		$clean = mysqli_real_escape_string($val,$this->_link);
	}
	return $clean;
}

public function query($sql){
42 ->  $result = mysqli_query($sql,$this->_link);
	$i=0;
44 ->while($array = mysqli_fetch_assoc($result)){
		foreach($array as $key=>$val){
			$return[$i][$key] = $val;
		}
		$i++;
	}
	return $return;
	}
}



HTML
// get instance
include('ooconn.php');
$db = DB::getInstance();

//query
$result = $db->query("SELECT * FROM `members`");
print_r($result);



Is This A Good Question/Topic? 0
  • +

Replies To: MySQL to MySQLi

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: MySQL to MySQLi

Posted 07 March 2009 - 01:27 PM

Actually, your mysqli_select_db() is doing the arguments in the wrong order. Put the link first, and the DB name second. Same with mysqli_query().

This post has been edited by Valek: 07 March 2009 - 02:02 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1