maxdean10 Posted February 12, 2011 Share Posted February 12, 2011 So, I feel like I am running up against some beginner's problem. So I am hoping this should be an easy fix. I have had a bit of trouble finding the right search terms to find a fix for this, so I am sorry ahead of time if there is already a post on this, please point me towards it. I am running PHP 4.4.9 and MySQL client API 4.1.22, in case that is of use. I can get the following to work and provide the appropriate output. Note in this first case that the connection info is in the same file as the call to it. <?php // set database access information define ('DB_USER', "*****"); define ('DB_PASSWORD', "*****"); define ('DB_HOST', "localhost"); define ('DB_NAME', "content"); // database connection protocol $dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL: Error #' . mysql_errno() . ' - ' . mysql_error()); mysql_select_db (DB_NAME) or die ('Could not connect to MySQL: Error #' . mysql_errno() . ' - ' . mysql_error()); $query = "SELECT content_element_title FROM content_main"; $result = mysql_query ($query); while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { echo ("<p>" . $row[content_element_title] . "</p>"); } unset($dbc); ?> My problem is, however, that when I separate the connection info into a separate file, I have been having problems. I think I got past the method calling the connection info not being able to see it, but now it isn't returning anything - should be returning "<p>" . $row[content_element_title] . "</p>", for which there are three rows in the db. Here is what the two files look like separated: kinnect.php <?php // set database access information define ('DB_USER', '*****'); define ('DB_PASSWORD', '*****'); define ('DB_HOST', 'localhost'); define ('DB_NAME', 'content'); //// database connection protocol $dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL: Error #' . mysql_errno() . ' - ' . mysql_error()); mysql_select_db (DB_NAME, $dbc) or die ('Could not connect to MySQL: Error #' . mysql_errno() . ' - ' . mysql_error()); ?> file.php <?php require_once ("correctpathto/kinnect.php"); $query = "SELECT content_element_title FROM content_main"; $result = mysql_query ($query, $dbc); while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { echo ("<p>" . $row[content_element_title] . "</p>"); } unset($dbc); ?> So what am I doing wrong? I am getting these error messages now. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Users/max/Sites/rdbase-llc/preliminary/connecttroubleshoot.php on line 18 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Users/max/Sites/rdbase-llc/preliminary/connecttroubleshoot.php on line 20 Thanks ahead of time for any help! Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/ Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 I pasted your files in locally, and just changed the require path, and the credentials to access my db, and it works fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173379 Share on other sites More sharing options...
dornizar Posted February 12, 2011 Share Posted February 12, 2011 from the error it doesn't pass the $dbc var. try to remove the resource link from mysql_query i mean: //$result = mysql_query ($query, $dbc); -->remove $dbc $result = mysql_query ($query); and please reply if there is still that problem or not. Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173382 Share on other sites More sharing options...
maxdean10 Posted February 12, 2011 Author Share Posted February 12, 2011 I am still stumped. Well, at least it works for you, Pickachu, maybe I have a configuration issue? Though my configuration has not been an issue in the past, as far as I can remember.I wouldn't know where to start, however, with troubleshooting a configuration problem. As for your suggestion dornizar, that gets me these error messages: Warning: mysql_query() [function.mysql-query]: Access denied for user: 'root@localhost' (Using password: NO) in /Users/max/Sites/rdbase-llc/preliminary/connecttroubleshoot.php on line 8 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /Users/max/Sites/rdbase-llc/preliminary/connecttroubleshoot.php on line 8 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Users/max/Sites/rdbase-llc/preliminary/connecttroubleshoot.php on line 10 So it is not connecting without the $dbc variable. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173390 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 Everything works fine when the credentials are pasted into the file, but not when you require_once() the config file? Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173393 Share on other sites More sharing options...
maxdean10 Posted February 12, 2011 Author Share Posted February 12, 2011 well, yeah, Pickachu is right, it works fine. I put the files together in the same folder, and called the configdb file using require once and just its filename, and it works fine. Problem is getting to it elsewhere in the file structure. Guess I need to look elsewhere in this forum or the internet for a primer on filepaths. I had been using an absolute filepath to the kinnect.php file with the db connection info, and was getting those error messages. Thanks for helping me identify my actual problem. Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173399 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 The require_once should have halted the script if the file wasn't found. Unless of course, there's another file in the directory that it did find with the same name, but wrong credentials? Is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173401 Share on other sites More sharing options...
PFMaBiSmAd Posted February 12, 2011 Share Posted February 12, 2011 I'm going to guess that your 'absolute' file path was actual a URL. Any chance you were using something like http://your_domain.com/kinnect.php in your require_once() statement? Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173404 Share on other sites More sharing options...
maxdean10 Posted February 12, 2011 Author Share Posted February 12, 2011 Sure was. kinnect.php WAS at http://127.0.0.1/~max/rdbase-llc/backnd/includes/kinnect.php so I used require_once ("http://127.0.0.1/~max/rdbase-llc/backnd/includes/kinnect.php"); I am using an apache server running on os 10.4, so I wasn't sure how to write the /home/etc... any suggestions or urls I could look at? Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173422 Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 This should do it, but you might have to tweak the path. require_once($_SERVER['DOCUMENT_ROOT'] . '/rdbase-llc/backnd/includes/kinnect.php'); Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173437 Share on other sites More sharing options...
jcbones Posted February 13, 2011 Share Posted February 13, 2011 The reason for you situation is that you are using a HTTP address. This will call the file through HTTP, upon which the PHP parser will parse the file, and return the results. So your file will not see the connection, as it has already been opened and closed. You need to use a file path instead of HTTP. Pik's solution should work. Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173455 Share on other sites More sharing options...
maxdean10 Posted February 13, 2011 Author Share Posted February 13, 2011 Thanks for the info, both of you. I foresee this solving other issues for me before they happen. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/227483-problem-connecting-to-mysql-db-using-included-connect-file/#findComment-1173484 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.