Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 2nd, 2007, 4:11 AM   #1
tAK
Programmer
 
Join Date: Mar 2007
Posts: 33
Rep Power: 0 tAK is on a distinguished road
mysqli connect via included file

Hey guys,

i have around 7 pages that all connect to the same database, at the moment, i have been adding this:

$mysqli = new mysqli('localhost','root','password');
$mysqli->select_db('mydatabase');

to the top of every script, i also have a "functions.php" which i include which contains some custom functions.. i tried adding this to functions.php:
function connect_db(){
$mysqli = new mysqli('localhost','root','password07');
$mysqli->select_db('tassielans');
}

and replacing the db connect lines with connect_db();
but it didnt work.. i realise this is most definitely wrong (not just because it didnt work)

but i was wondering if anyone has a function that works for connecting to a database via mysqli ?? or a link to a site that explains it well, so far my web searches haven't turned up anything new on the topic.
tAK is offline   Reply With Quote
Old Apr 2nd, 2007, 5:33 AM   #2
RGCaldas
Newbie
 
Join Date: Nov 2006
Posts: 5
Rep Power: 0 RGCaldas is on a distinguished road
It would be easier to put the code in another file and include it. That way you can also protect direct access to the file.

in file connect.php
<?php
$mysqli = new mysqli('localhost','root','password07');
$mysqli->select_db('tassielans');
?>

in other files

include("safedir/connect.php");

Hope it helps
RGCaldas is offline   Reply With Quote
Old Apr 2nd, 2007, 6:05 PM   #3
tAK
Programmer
 
Join Date: Mar 2007
Posts: 33
Rep Power: 0 tAK is on a distinguished road
Ahh.. yeah, i think i see where i was going wrong now..
apart from leaving un-needed info in my code (hate when i do that)

more to the point, i was trying to do it as a function, when i dont have to.
tAK is offline   Reply With Quote
Old Apr 2nd, 2007, 7:04 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Say what?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 3rd, 2007, 6:40 AM   #5
tAK
Programmer
 
Join Date: Mar 2007
Posts: 33
Rep Power: 0 tAK is on a distinguished road
well.. i was making the connection into a function:
function dbconnect(){
$mysqli = new mysqli('localhost','root','password07');
$mysqli->select_db('mydatabase');
}

and i was then calling that function from the page that was loaded.. it kept failing. as RGCaldas noted, i just needed it at the top of the included file, and it would run.
tAK is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
converting string to float beginnerCCC C 22 Oct 2nd, 2006 11:59 PM
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 8:55 AM
Command Prompt SkyPioneer Coder's Corner Lounge 5 May 3rd, 2006 10:07 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC