473,394 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to use mysqli _construct with database connection

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
1 2401
Dormilich
8,658 Expert Mod 8TB
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

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

Similar topics

3
by: R Reyes | last post by:
Hi, I'm trying to modularize my database connections a little better and get more out of my project with less code. First check out this common dbOpen() function inside class clsDatabase. I...
1
by: Macca | last post by:
Hi, I have a C# Solution/Application that contain 4 projects. Each of these projects needs at some time to access the same database. I would like to know how to share a single connection...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
5
by: Matt | last post by:
Hello, What is the best way to handle the database connection string for a class library project that will be compiled and used as a .dll? This .dll will be accessed via classic ASP and in...
6
by: Arsalan Ahmad | last post by:
Hi all, I am creating a website in which in an Item detail page there are a number of web controls (7 or 8) and what is happening that inside each of control's Page_Load() function I am creating...
3
by: fniles | last post by:
In the Windows application (using VB.NET 2005) I use connection pooling like the following: In the main form load I open a connection using a connection string that I stored in a global variable...
4
by: Mike P2 | last post by:
Hi. I'm writing controls that have to query the database, and it bothers me that I might have several of these controls on a page that each create, open, and close their own connection with the...
3
eboschi
by: eboschi | last post by:
Hi all, i'm new to .net programming and i have some problems with database connection. I have written two web application. Both of them use ADODB Connection to Sql Server 2000 database. The problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.