Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-07-2013, 04:58 PM   PM User | #1
harkly
Regular Coder

 
Join Date: Jun 2010
Location: Earth
Posts: 305
Thanks: 27
Thanked 2 Times in 2 Posts
harkly is an unknown quantity at this point
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..
harkly is offline   Reply With Quote
Old 03-07-2013, 05:39 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 03-07-2013, 06:11 PM   PM User | #3
harkly
Regular Coder

 
Join Date: Jun 2010
Location: Earth
Posts: 305
Thanks: 27
Thanked 2 Times in 2 Posts
harkly is an unknown quantity at this point
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
harkly is offline   Reply With Quote
Old 03-07-2013, 07:02 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 03-07-2013, 07:08 PM   PM User | #5
harkly
Regular Coder

 
Join Date: Jun 2010
Location: Earth
Posts: 305
Thanks: 27
Thanked 2 Times in 2 Posts
harkly is an unknown quantity at this point
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..
harkly is offline   Reply With Quote
Old 03-07-2013, 08:27 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,528
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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'); 
}
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-07-2013, 08:38 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 03-08-2013, 03:10 AM   PM User | #8
bemore
New Coder

 
Join Date: Feb 2013
Posts: 39
Thanks: 14
Thanked 0 Times in 0 Posts
bemore is an unknown quantity at this point
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();
}
?>
bemore is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:52 AM.


Advertisement
Log in to turn off these ads.