473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

Object Oriented mysqli connection function

PsychoCoder
465 Expert Mod 256MB
Recently a friend was having a hard time connecting using mysqli, so I showed an OO way of accomplishing the task with this code

Expand|Select|Wrap|Line Numbers
  1. class db_connection extends mysqli 
  2. {
  3.     public function __construct($dbhost, $username, $password, $db) 
  4.     {
  5.         parent::__construct($dbhost, $username, $password, $db);
  6.  
  7.         if (mysqli_connect_error()) 
  8.         {
  9.             die('Connection Error: (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
  10.         }
  11.     }
  12. }
  13.  
  14. $db = new db_connection('localhost', 'username', 'password', 'db');
  15.  
  16. echo 'Success... ' . $db->mysqli_get_host_info($db) . "\n";
  17.  
  18. $db->close();
Jan 10 '12 #1
1 4049
Dormilich
8,658 Expert Mod 8TB
if it is not required to use MySQLi, I recommend to use PDO. if that has a connection problem, a PDOException is thrown automatically (and you can set PDO to use Exceptions for every error it encounters).
Jan 18 '12 #2

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

Similar topics

5
by: Tim | last post by:
I have trouble with MySQL 5 and PHP 5.0.2, using XP SP1 with IIS. I can get PHP to access MySQL using CGI and the (older) MySQL functions, but when I try and use ISAPI and MySQLi functions I have...
2
by: Jozef | last post by:
Hello, I'm trying to create a central function that runs a connection to an SQL Server database. The connection etc works, but when I try to call it, I get an error saying "Runtime-Error 91:...
1
by: IGotYourDotNet | last post by:
I have a sub named ConnectToDb which has my db connections commands in it. When I call it from another page that sub routine will not execute and i can't connect to the DB via that sub. why is it...
7
by: Citrus | last post by:
Hey, i'm looking for the best possible way for connecting to my database without too much of an effort. The example works fine and fast. What i need to know is if it's safe, stable and the way...
7
by: momo | last post by:
Hello to all, I need some help. I am looking for a database connection function that I can use in my ASP.NET site. This would be in VB.NET. What I want to be able to do is call the function and...
13
by: Schmidty | last post by:
If you do a page reload with $_SERVER will your program lose a mysqli connection upon the reload of the page? Would this code work? I need to know how to carry over a connection between methods as...
7
by: Schmidty | last post by:
Okay...I have another 'newbie' question; I have a function that loads a page and the action is $_SERVER; In the form that is in a function(method?) within a class a variable is passed back to...
2
by: Michael | last post by:
Hi, I try to use mysqli object instead of standard mysql functions. Is it ok to create mysqli object within my class or schould I pass mysqli object to my object. The problem is, with code...
1
by: Tuyet | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.