here is my problem:
index.php contents:
require_once('phpcommonscripts/connections/connection.php');
require_once('phpcommonscripts/functions/logging.php');
Func_LogToDB($logType, $actionType, $errMsg, $varUser);
connection.php contents:
$hostname_MYDB = "localhost";
$database_MYDB = "MYDB";
$username_MYDB = "USER";
$password_MYDB = "PASS";
$MYDB = mysql_pconnect($hostname_MYDB, $username_MYDB, $password_MYDB) or trigger_error(mysql_error(), E_USER_ERROR);
logging.php contents:
function Func_LogToDB($lType, $lAction, $lMessage, $lUser) {
mysql_select_db($database_MYDB, $MYDB);
}
ERROR MESSAGE:
Notice: Undefined variable: database_MYDB in /home/notes/public_html/phpcommonscripts/functions/logging.php on line 22
( line 22 is the mysql_select_db() line... )
Am I doing something wrong here ?
Am I doing something wrong here ?
Yes you are :)
Those variables $database_MYDB, $MYDB aren't in same scope.
You cannot access variables which are defined outside a function from within a function.
Related
I know this is little bit strange but some variables are undefined when including these variables.
This is my web structure:
details.php:
$login['server'] = 'server';
$login['database'] = 'db';
$login['username'] = 'user';
$login['password'] = 'pass';
define ('constant1' , 'ok');
main.php:
include ('subfile.php');
subfile.php:
echo $login;
echo constant1;
index.php:
include ('details.php');
include ('main.php');
And output is undefined $login and ok. I already tried with get_included_files() in subfile.php and this function shows details.php as included file.
I'm working with mysql and I'm trying to make a simple login to the database, but whenever I try to do it I get an error
Notice: Undefined variable: db_hostname in D:\xampp\htdocs\index.php on line 3
Notice: Undefined variable: db_username in D:\xampp\htdocs\index.php on line 3
Notice: Undefined variable: db_password in D:\xampp\htdocs\index.php on line 3
here is my code for the login.php
<?php//login.php
$db_hostname = 'localhost';
$db_database = 'publications';
$db_username = 'root';
$db_password = '';
?>
code for index.php
<?php
require_once('login.php') ;
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if(!$db_server) die ("Unable to connect to MySQL: " . mysql_error());
?>
I have no idea what I'm doing wrong.
change this (remove commented line or give space before it)
<?php//login.php
$db_hostname = 'localhost';
$db_database = 'publications';
$db_username = 'root';
$db_password = '';
?>
to,
<?php //login.php
$db_hostname = 'localhost';
$db_database = 'publications';
$db_username = 'root';
$db_password = '';
?>
In PHP the <?php directive must be followed by whitespace, newline, or EOF (the file ends after the directive).
You normally get a fatal error and your script stops.
However you're using an outdated and unsupported version of PHP which just passes your "to be" PHP code directly to stdout. This can have security implications, like in your case, you've published the database credentials (if any).
I'm having problems when trying to connect to a database with PHP. I am getting the following errors
Notice: Undefined variable: dbhandle in /opt/lampp/htdocs/connection/Connection.php on line 17
Warning: mysql_select_db() expects parameter 2 to be resource, null given in /opt/lampp/htdocs/connection/Connection.php on line 17
Could not select test
My connection file:
<?php
function Connection() {
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
mysql_query("SET NAMES utf8");
}
function SelectDatabase() {
$name = "test";
$selected = mysql_select_db("$name",$dbhandle)
or die("Could not select $name");
}
?>
Index.php
<html>
<head>
<?php include 'Connection.php'; ?>
</head>
<body>
<?php Connection() ?>
<?php SelectDatabase() ?>
</body>
</html>
$dbhandle is out of scope when used in SelectDatabase. You might want to have Connection() return $dbhandle. That way, you could do:
<?php SelectDatabase(Connection()) ?>
Of course, you would have to modify SelectDatabase to take the connection variable as a parameter.
Or, alternatively, you could make $dbhandle a global variable so you can use it anywhere in your script.
It's a scoping problem: $dbhandle is local to Connection()
You can either use a global variable (put global $dbhandle; at the start of both functions), which is not very elegant, or
function Connection() {
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
mysql_query("SET NAMES utf8");
return $dbhandle;
}
function SelectDatabase($dbhandle) {
$name = "test";
$selected = mysql_select_db("$name",$dbhandle)
or die("Could not select $name");
}
and
</head>
<body>
<?php $db=Connection() ?>
<?php SelectDatabase($db) ?>
</body>
</html>
The problem here is easy to spot; you never pass the $dbhandle parameter to your function. You need to do this so the function can use the parameter; due to scope issues, not all parameters are immediately available to all functions.
Try the following instead:
function SelectDatabase($dbhandle) {
$name = "test";
$selected = mysql_select_db("$name",$dbhandle)
or die("Could not select $name");
}
And then, when you call the function:
SelectDatabase($dbhandle);
Make sure that $dbhandle is global.
Either that, or as others have pointed out, have Connection() return the variable name, and use it within the call.
Secondly, mysql_* functions are deprecated. I would suggest you use PDO instead.
you don't make your life easier :p
you just have to do this :
$connexion = new PDO("mysql:host=localhost;dbname=test","root","password");
and if you want to make sql request just :
$connexion->query("SQL REQUEST ....");
My problem is,
This is the file that is being included ,
<?php
$dbhost = "localhost";
$dbuser = "****";
$dbpass = "***";
$dbname = "****";
$MYSQL_ERRNO = "";
$MYSQL_ERROR = "";
// Connect To Database
function db_connect() {
global $dbhost, $dbuser, $dbpass, $dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$link_id) {
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to $dbhost.";
return 0;
}
else if(!mysql_select_db($dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}
?>
The main file has the following code to make db connection ,
<?php
require_once 'file.php';
$link_id = db_connect($dbname);
......
?>
But i got function undefined error.
Using Apache in Windows with PHP 5.3
1) Check relative paths for your includes.
2) Just because the PHP documentation says you can use require_once without parenthesis doesn't mean you should. Stay consistent, use require_once("file.php");.
3) Be consistent with curly braces in your function. You're missing one or two up there. I'd like to know if you rewrite your function the following way if it works:
function db_connect() {
global $dbhost, $dbuser, $dbpass, $dbname, $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbuser, $dbpass);
if ($link_id === false) {
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to $dbhost.";
return 0;
} elseif (mysql_select_db($dbname) === false) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
} else {
return $link_id;
}
}
And remember, checking your web server error logs usually helps.
Edit:
According to the comments for this answer, you're not including the file you think you are. Use an absolute path for the include so you're 100% sure of the file being included.
A logic.
Sometimes we need it.
Let's sort out your case.
you can see whatever "function undefined error." (Although I'd prefer literal and exact PHP error messages as they contain A LOT of useful information, let's assume it's regular PHP error thrown right in this place).
We can conclude from the (1) that you can see alll errors occurred.
So, from (2) we can tell the if there was a file not found error, you'd be notified of it.
So, we can assume from (3) that there is no error and file being included correctly.
What's next?
The most possible reason for you getting this error is the wrong code you posted here.
For example, if you include your file not as file.php but as http://example.com/file.php the result will be exactly as described.
Or there can be 2 files named 'file.php', one contains the function definition and another without it.
Or there can be a typo in the function's name as well.
So, you just have to double check your names and print some debugging info to be sure you are including the right file and calling the right function.
This question already has answers here:
Giving my function access to outside variable
(6 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
Hello I am pretty sure they are in the same scope and am unsure why I am getting this error now as it was working before.
These are the other files in the same folder as
the php file :
dbh.php
recipeLookUp.php
It is line 9 that I am getting the error
this is the recipeLookUp.php
<?php
function lookup($sql,$column_name){
$results_search=array();
include_once 'dbh.php';
$results=mysqli_query($conn,$sql);
$resultsCheck = mysqli_num_rows($results);
if ($resultsCheck > 0) {
while ($row = mysqli_fetch_assoc($results)){
$info= $row[$column_name];
$results_search[]=$info;
}
return $results_search;
}
}
and here is my other file
<?php
$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'Recipe';
$conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);
Im sure it probaly something small but I can't find it please help!!!
The include_once 'dbh.php'; within your function seems fishy. What are you gonna do if you have other functions which need to perform SQL queries as well?
Instead, I would put the include_once 'dbh.php'; outside (above) your function, and perhaps use require_once instead of include once. And then inside your function, put global $conn; above the mysqli_query call, so your $conn variable is defined there when you pass it along to mysqli_query.
However, if you are worried about creating unnecessary SQL connections (e.g. in case your lookup function is never actually called) I think it would be even better to put this in your dbh.php :
<?php
$conn = null;
function Query($sql)
{
global $conn;
if (!$conn) // will only connect if connection does not exist yet
{
$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = 'xxxxxxxxxxx';
$dbName = 'Recipe';
$conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);
}
return mysqli_query($conn,$sql);
}
?>
And now in your main PHP file, or any PHP file where you include require_once 'dbh.php'; you can directly use Query($sql) to perform queries, which will take care of the connection itself (and only do so if necessary).