ItsWesYo Posted July 5, 2006 Share Posted July 5, 2006 I really must be annoying you all now with my questions :-[ I'm such a php newbie ;DAnyway. Instead of having all of this 'connecting-to-mysql' coding everywhere, I want it on one page, where I can include it wherever I want.I've tried it before and it comes up with errors.Any help?I just need it to have it connecting to the db with the user/pass and such. Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/ Share on other sites More sharing options...
mrwhale Posted July 5, 2006 Share Posted July 5, 2006 here:[code]mysql.php----------------<?php// put all the stuff u wanna include here?>yourfile.php----------------<?phpinclude_once( "mysql.php" );// your files contents?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53336 Share on other sites More sharing options...
xyn Posted July 5, 2006 Share Posted July 5, 2006 Simple:[code=php:0]<?PHP$host="localhost" //Hostname (default is localhost)$login="user"; //username to db$pwd="pass"; //users pass to db$db="dbname"; //dbname$conn = mysql_connect("$host", $login, $pwd) or die(mysql_error());mysql_select_db($db) or die(mysql_error());?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53337 Share on other sites More sharing options...
micah1701 Posted July 5, 2006 Share Posted July 5, 2006 very simplejust create a php page and call it something like "config.php"put your connection info in it, like:[code]<?php $dbhost = "localhost"; $dbuser = "root"; $dbpassword = "password"; $dbname = "myDatabase"; //you could leave this out if you use mulitple databases. mysql_connect($dbhost, $dbuser, $dbpassword) or die("Couldn't establish connection");mysql_select_db($dbname); //again, leave this out if you are using multiple databases. ?>[/code]then on any page that needs to query the database just use the include() function:[code]<?php include('config.php');//query stuff$query = mysql_query("SELECT * FROM table WHERE column = '$option' ");........ etc .......?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53338 Share on other sites More sharing options...
ItsWesYo Posted July 5, 2006 Author Share Posted July 5, 2006 Thanks guys.Yeah, I knew how to connect and stuff.Everytime I add the mysql coding to it, I get errors. Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53356 Share on other sites More sharing options...
trq Posted July 5, 2006 Share Posted July 5, 2006 Can we see? Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53357 Share on other sites More sharing options...
ItsWesYo Posted July 5, 2006 Author Share Posted July 5, 2006 EDIT: Alright ......http://www.evermoreforums.com/wes/test.phpThe code on the page:[code]<?phpinclude ("header.php");include_once( "mysql.php" );$query = mysql_query("SELECT * FROM items");while($r=mysql_fetch_array($result)){ $name=$r["name"];echo "$name<br>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53362 Share on other sites More sharing options...
freakus_maximus Posted July 5, 2006 Share Posted July 5, 2006 Just a side note, I always put my db connect files in a directory above the root level just in case someone were to be able to bypass my index and see the file listed. Just a little security tip once you have your main problem resolved.I call it like this:[code]include ('../includes/yourconnection.php')[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53365 Share on other sites More sharing options...
freakus_maximus Posted July 5, 2006 Share Posted July 5, 2006 The error looks like there is something wrong in your mysql.php. It says line 3, but most likely before that. Check for a missing ";" or something with the syntax. Or post your code for that and we can see if there is something else missing. Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53367 Share on other sites More sharing options...
ItsWesYo Posted July 5, 2006 Author Share Posted July 5, 2006 Nevermind freakus. Xyn forgot ";" and that's the code I took.I get a new error now. Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53368 Share on other sites More sharing options...
xyn Posted July 5, 2006 Share Posted July 5, 2006 Ohh yeah sorry :P I didn't spot that. Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53369 Share on other sites More sharing options...
xyn Posted July 5, 2006 Share Posted July 5, 2006 Why not Post your query, Then your error and maybe can help... Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53370 Share on other sites More sharing options...
ItsWesYo Posted July 5, 2006 Author Share Posted July 5, 2006 mysql.php:[code]<?PHP$host="localhost";$login="hidden";$pwd="hidden";$db="hidden";$conn = mysql_connect("$host", $login, $pwd) or die(mysql_error());mysql_select_db($db) or die(mysql_error());?>[/code]test.php:[code]<?phpinclude ("header.php");include_once("mysql.php");$query = mysql_query("SELECT * FROM items");while($r=mysql_fetch_array($result)){ $name=$r["name"];echo "$name<br>";}?>[/code]the error says:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/evermore/public_html/wes/test.php on line 5 Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53373 Share on other sites More sharing options...
freakus_maximus Posted July 5, 2006 Share Posted July 5, 2006 Here's the problem:[code]$query = mysql_query("SELECT * FROM items");while($r=mysql_fetch_array($result))[/code]Should be:[code]$query = mysql_query("SELECT * FROM items");while($r=mysql_fetch_array($query))[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53374 Share on other sites More sharing options...
ItsWesYo Posted July 5, 2006 Author Share Posted July 5, 2006 Thanks guys, it worked! Quote Link to comment https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.