![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2007
Posts: 33
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Nov 2006
Posts: 5
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Mar 2007
Posts: 33
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Mar 2007
Posts: 33
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |