By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

How to use mysqli _construct with database connection

P: 3
I am attempting to connect to the database (which was successfully). However, I can not extend the connection to execute other functions. In my example below,
the output is not giving me what I expected. There seem to be two connections and my escaped text which I called ($data = $query->select($text);) does not execute.

OUTPUT:
Connection established! Now:Connection established! Now:
text passed =Brian O'Dell
text can be escaped =


INDEX.PHP (main page):

Expand|Select|Wrap|Line Numbers
  1. include("database_connect.php");
  2. include("query_builder.class.php");
  3.  
  4. $text = "Brian O'Dell";
  5.  
  6. $query = new QueryBuilder;
  7. $data = $query->select($text);

DATABASE_CONNECT.PHP:

Expand|Select|Wrap|Line Numbers
  1. public $host = 'localhost';
  2. public $db_username = 'root';
  3. public $db_password = 'password';
  4. public $db_name = 'customer';
  5.  
  6. protected $mysqli;
  7.  
  8. public function __construct() {}
  9.  
  10. public function connect()
  11. {
  12. $this->mysqli = new mysqli($this->host,$this->db_username,$this->db_password,$this->db_name);
  13.  
  14. if ($mysqli->connect_error) {
  15. die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
  16. }        
  17. else
  18. {
  19. echo "Connection established! Now:";
  20. }
  21.  
  22.  
  23. public function db_close()
  24. {
  25. $this->mysqli->close();
  26.  
  27. public function __destruct() {}

QUERY_BUILDER.CLASS.PHP:

Expand|Select|Wrap|Line Numbers
  1. class QueryBuilder
  2. {
  3.  
  4. public function __construct()
  5. {
  6.     $this->connect = Db_Connect::connect();    
  7. }
  8.  
  9. public function __destruct()
  10. {
  11.     Db_Connect::db_close();
  12. }
  13.  
  14. public function select($sql=false)
  15. {
  16.  
  17.     echo "<br>text passed =".$sql."<br>";
  18.     $newtxt =  $this->mysqli->real_escape_string($sql);
  19.     echo "text can be escaped = ".$newtxt;
  20.  
  21. }
  22. }
Aug 30 '10 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
as far as I can see, you donít have a $this->mysqli instance. $this->connect is null (Db_Connect::connect() doesnít return anything) and I guess Db_Connect should be a Singleton (though itís currently called incorrectly (in static context)).

Ö Iíd probably find more issues if I see the whole code
Aug 30 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.