Problem with MySQL connection

works elsewhere though

Page 1 of 1

4 Replies - 1536 Views - Last Post: 09 April 2007 - 11:14 AM Rate Topic: -----

#1 DilutedImage   User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Problem with MySQL connection

Post icon  Posted 09 April 2007 - 03:54 AM

PROBLEM SOLVED - solution is two posts down

----- original post -----

I'm receiving this error:

Quote

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /blah/blah/blah/main.php on line x

I'm sure I've probably made a simple oversight, but I'm tired and cannot find it ... Any input would be greatly appreciated.

"index.php":
<?php
require('php/functions.php');
startSession();
requireLogin();
$indexCheck = true;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>blah</title>
<link href="css/blah.css" rel="stylesheet" type="text/css" />
</head>
<body>

<?php
switch ($page){
	case "blah":
		include('blah.php');
		break;
	default:
		include('main.php');
}
?>

</body>
</html>


excerpt from "main.php" (everything up to the error):
<?php
if(!$indexCheck) {
	require('php/admin_functionsMain.php');
	startSession();
}
requireLogin();

connectToData(); // function in "functions.php"
$query = "SELECT * FROM myTable";
$result = mysql_query($query,$dbConn) or die(mysql_error()); // THROWS ERROR HERE *****
mysql_free_result($result);
?>


from "functions.php":
function connectToData() {
	$db_hostname = 'localhost';
	$db_username = '_username_';
	$db_password = '_password_;
	$db_database = '_database_';
	$dbConn = mysql_connect($db_hostname, $db_username, $db_password) or die('Unable to connect to database.'.mysql_error());
	mysql_select_db($db_database) or die( 'Unable to select database.');
	echo $dbConn; // WORKS: echoes "Resource id #3" *****
}


Apache: 1.3.37 (Unix)
PHP: 4.4.6
MySQL: 5.0.27-standard

??

This post has been edited by DilutedImage: 09 April 2007 - 04:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with MySQL connection

#2 DilutedImage   User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Problem with MySQL connection

Posted 09 April 2007 - 04:18 AM

Also, if I replace the "connectToData()" call (within main.php) with the actual function code (from functions.php), no error is thrown. ..

Can an include not call a function of the file that opened it?
Was This Post Helpful? 0
  • +
  • -

#3 DilutedImage   User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Problem with MySQL connection

Post icon  Posted 09 April 2007 - 04:36 AM

I got it working.

In answer to my previous question: yes, an include can call a function of the file that opened it. However, unlike the parent file, the included file cannot access variables within the scope of that function. The parent file isn't supposed to be able to access those function variables either, but it does nonetheless, which is why the function call works in the parent file.

To correct the error, I defined $dbConn as a global variable, by adding the line "global $dbConn" before the line "$dbConn = ...".
Was This Post Helpful? 0
  • +
  • -

#4 Styx   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: Problem with MySQL connection

Posted 09 April 2007 - 08:20 AM

You can also use a class.
Was This Post Helpful? 0
  • +
  • -

#5 DilutedImage   User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Problem with MySQL connection

Posted 09 April 2007 - 11:14 AM

I hadn't thought of that. I just might explore that option. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1