Jump to content

Undefined variables?!


evanct

Recommended Posts

So I have the mysql connecting going on here in connect.php:

 

require_once('config.php');

$connect=mysql_connect($db_host,$db_user,$db_password) or die('Could not connect to mysql: '.mysql_error());

 

and those connection variables set here in the required config.php:

 

$db_host='localhost';
$db_user='root';
$db_password='a';

 

I was getting an error when trying to mysql connect(Access denied for user 'ODBC'@'localhost' (using password: NO)) so I switched error_reporting to return notices. And now it's telling me that the variables db_host, db_user, and db_password are undefined. The require_once link in connect.php is correct. What's going on here?

Link to comment
https://forums.phpfreaks.com/topic/158870-undefined-variables/
Share on other sites

Then you have some code that is either overwriting the values, unsetting the variables, or is inside of a conditional statement.

 

The only way for anyone to help you with what your code is doing is if you post all the relevant code and the error message because what you have stated does not match reality.

Link to comment
https://forums.phpfreaks.com/topic/158870-undefined-variables/#findComment-837922
Share on other sites

Then you have some code that is either overwriting the values, unsetting the variables, or is inside of a conditional statement.

 

To be sure I changed the names of the variables, but... no, same error message.

 

Here is the entire connect.php:

<?php
require_once('config.php');

$connect=mysql_connect($sql_host,$sql_user,$sql_password) or die('Could not connect to mysql: '.mysql_error());
mysql_select_db($sql_database) or die('Could not select database: '.mysql_error());

function dbQuery($sql) {
return mysql_query($sql) or die('Could not query the database: '.mysql_error());
}

function numRows($result) {
return mysql_num_rows($result) or die('Could not fetch rows: '.mysql_error());
}

function fetchArray($result,$result_type=MYSQL_NUM) {
return mysql_fetch_array($result) or die('Could not fetch array: '.mysql_error());
}

function fetchAssoc($result) {
return mysql_fetch_assoc($result) or die('Could not fetch assoc: '.mysql_error());
}

?>

 

and config.php:

 

<?php

$sql_host='localhost';
$sql_user='root';
$sql_password='a';
$sql_database='gallery';

//the rest is just defining other variables, there is nothing here that could interfere
   
?>

 

the function that's trying to connect via connect.php:

 

function adminLogin($username,$password) {

require_once('../lib/connect.php');
$sql="SELECT user_id FROM users WHERE user_name=$username AND password=MD5($password)";
$result=dbQuery($sql);
if (dbNumRows($result)==0) {
	setError("Incorrect username or password.");
	header('location:login.php');
} else {
	$_SESSION['admin']=1;
	$sql="INSERT INTO sessions(login) VALUES(NOW())";
	dbQuery($sql);
}
}

 

and finally, the error messages:

 

Notice: Undefined variable: sql_host in C:\Apache2\htdocs\gallerytest\lib\connect.php on line 4

Notice: Undefined variable: sql_user in C:\Apache2\htdocs\gallerytest\lib\connect.php on line 4

Notice: Undefined variable: sql_password in C:\Apache2\htdocs\gallerytest\lib\connect.php on line 4

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Apache2\htdocs\gallerytest\lib\connect.php on line 4
Could not connect to mysql: Access denied for user 'ODBC'@'localhost' (using password: NO)

Link to comment
https://forums.phpfreaks.com/topic/158870-undefined-variables/#findComment-837926
Share on other sites

Try to comment to see either config variables are some where saved already:

 

//require_once('config.php');

 

The 2nd option u can try is, directly put the variables of config.php in connect.php to test.

<?php
$sql_host='localhost';
$sql_user='root';
$sql_password='a';
$sql_database='gallery';

$connect=mysql_connect($sql_host,$sql_user,$sql_password) or die('Could not connect to mysql: '.mysql_error());
mysql_select_db($sql_database) or die('Could not select database: '.mysql_error());

function dbQuery($sql) {
   return mysql_query($sql) or die('Could not query the database: '.mysql_error());
}

function numRows($result) {
   return mysql_num_rows($result) or die('Could not fetch rows: '.mysql_error());
}

function fetchArray($result,$result_type=MYSQL_NUM) {
   return mysql_fetch_array($result) or die('Could not fetch array: '.mysql_error());
}

function fetchAssoc($result) {
   return mysql_fetch_assoc($result) or die('Could not fetch assoc: '.mysql_error());
}

?>

Link to comment
https://forums.phpfreaks.com/topic/158870-undefined-variables/#findComment-837932
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.