Enjoy an ad free experience by logging in. Not a member yet?
Register .
03-07-2013, 04:58 PM
PM User |
#1
Regular Coder
Join Date: Jun 2010
Location: Earth
Posts: 305
Thanks: 27
Thanked 2 Times in 2 Posts
database connection with mysqli
I am trying to do an include for my db connection with mysqli but cannot not find any examples of how do to it.
This is what I have
PHP Code:
include( 'library/login2.php' );
login ();
PHP Code:
function login ()
{
$con = mysqli_connect ( 'localhost' , 'stuff' , 'stuff' , 'stuff' );
// tried this as well $con = new mysqli(
}
So what is the extra step that I need to take? I know there is something missing in my function but everything I have tried does not work.
My db connection works directly in the page
This is the error I am getting
Quote:
Fatal error: Call to a member function query() on a non-object in /home/content/51/10528751/html/login.php on line 49
Line 49 is
Quote:
$result=$mysqli->query($sql);
Last edited by harkly; 03-07-2013 at 05:02 PM ..
03-07-2013, 05:39 PM
PM User |
#2
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
What's the scope of the $mysqli->query call? Mysqli is (rightfully) not a global resource like mysql was. So you cannot do this:
PHP Code:
function connect () { $mysqli = new Mysqli ( 'localhost' , 'user' , 'password' , 'db' ); } connect (); $mysqli -> query ( '...' );
Since $mysqli doesn't exist outside the scope of connect.
__________________
PHP Code:
header ( 'HTTP/1.1 420 Enhance Your Calm' );
03-07-2013, 06:11 PM
PM User |
#3
Regular Coder
Join Date: Jun 2010
Location: Earth
Posts: 305
Thanks: 27
Thanked 2 Times in 2 Posts
I see so the best thing is to keep it on the pages that need it?
I have many many queries calling it in a large variety of pages
03-07-2013, 07:02 PM
PM User |
#4
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
I don't understand what you are referring to about keeping it on the pages that need it.
What I'm referring to is that mysqli obeys the scope of code unlike the mysql library. So if you need to use it in functions you'll need to pass it in.
__________________
PHP Code:
header ( 'HTTP/1.1 420 Enhance Your Calm' );
03-07-2013, 07:08 PM
PM User |
#5
Regular Coder
Join Date: Jun 2010
Location: Earth
Posts: 305
Thanks: 27
Thanked 2 Times in 2 Posts
I was just looking to move my db connection into an include file so when it gets changed I don't have to modify all the files just 1. Since its not a global resource as mysql it appears as though I need to keep the db connection on all the pages that require it to be there.
I don't know how to do it any other way, have looked but to no avail, hence this posting, to see if it is possible.
Last edited by harkly; 03-07-2013 at 07:10 PM ..
03-07-2013, 08:27 PM
PM User |
#6
Master Coder
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,528
Thanks: 0
Thanked 503 Times in 494 Posts
The quickest solution is to define the database connection as global inside each of the functions tat needs to use if. For example:
Code:
function login()
{
global $mysqli;
$mysqli = mysqli_connect('localhost', 'stuff', 'stuff', 'stuff');
}
03-07-2013, 08:38 PM
PM User |
#7
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Globalizing a variable can create horrific debugging.
But it's true, it is the easiest fix.
__________________
PHP Code:
header ( 'HTTP/1.1 420 Enhance Your Calm' );
03-08-2013, 03:10 AM
PM User |
#8
New Coder
Join Date: Feb 2013
Posts: 39
Thanks: 14
Thanked 0 Times in 0 Posts
I think what you are looking for is
PHP Code:
require_once( 'somefile.php' );
Place it at the start of your PHP on each page that needs a connection.
Inside of somefile.php:
PHP Code:
<?php $connection = mysqli_connect ( 'localhost' , 'stuff' , 'stuff' , 'stuff' ); if ( mysqli_connect_errno ()) { printf ( "Connect failed: %s\n" , mysqli_connect_error ()); exit(); } ?>
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 07:52 AM .
Advertisement
Log in to turn off these ads.