Jump to content

problem connecting to mysql db using included connect file


maxdean10

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.