Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   mysqli connect via included file (http://www.programmingforums.org/showthread.php?t=12921)

tAK Apr 2nd, 2007 5:11 AM

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.

RGCaldas Apr 2nd, 2007 6:33 AM

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

tAK Apr 2nd, 2007 7:05 PM

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.

DaWei Apr 2nd, 2007 8:04 PM

Say what?

tAK Apr 3rd, 2007 7:40 AM

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.


All times are GMT -5. The time now is 2:12 AM.

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