Reputation:
I am working on a project, and I am getting undefined errors for $host, $dbname, $user, and $pass.
But the errors only occur when those are inside of the dbConnect() function.
Here's the code (upload.php):
<?php
error_reporting(E_ALL);
require('config.php');
$filename = htmlentities($_FILES['file']['name']);
$tmpname = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
$filetype = $_FILES['file']['type'];
function dbConnect() {
try {
global $dbcon;
$dbcon = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
if (dbConnect()) {
print('worked');
}
config.php:
<?php
global $host, $user, $pass, $dbname;
$host = "localhost"; // MySQL Hostname
$user = "root"; // MySQL User
$pass = "mypass"; // MySQL Password
$dbname = "files";
Upvotes: 0
Views: 1600
Reputation: 57388
You need to declare those vars GLOBAL inside dbConnect too.
function dbConnect() {
try {
global $dbcon;
global $host, $user, $pass, $dbname;
$dbcon = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
EDIT
Yes, globals aren't a really good idea - they hinder code reuse and "pollute" the namespace, you never known which variables are there and which aren't, and risk changing a variable which was used somewhere else.
A better way to approach the problem would be to pass the required information as "parameters". The same applies to the return value, which could be a resource (if everything went well) or a string representing an error message.
function dbConnect($host, $dbname, $user = 'nobody', $pass = '') {
try {
return new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch (PDOException $e) {
return $e->getMessage();
}
}
You would call such a function passing the parameters and checking its output, which if everything went well, is expected to be an object:
if (!is_object($conn = dbConnect($host, $dbname, $user, $pass) {
die("There was an error: $conn");
}
Another advantage of parameters is that you can have default values for parameters (e.g., if you wrote dbConnect($host, $dbname), the function would "understand" and use 'nobody' and an empty password for the remaining parameters).
Upvotes: 0
Reputation: 8767
I would suggest that you use an array to store your connection string info.
First, create a function in your config.php
page that returns the necessary DB connection string info. To use, you would simply declare a variable within upload.php
of $dbconfig
that stores the values returned from the loadDBConfig()
function in your config.php
file. You will then execute the dbConnect()
function by declaring your $dbcon
variable and setting the value to dbConnect()
. This will return the result of your function to the variable, which you can then check for the desired result.
This solution removes the need for global variables and improves the organization.
Note: Your entire DB interaction should technically be moved to a class for improved portability.
upload.php:
...
function dbConnect() {
$dbconfig = loadDBConfig();
try {
$dburl = "mysql:host=" . $dbconfig['host'] . ";dbname=" . $dbconfig['dbname'];
return new PDO($dburl, $dbconfig['user'], $dbconfig['pass']);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
$dbcon = dbConnect();
...
config.php:
<?php
function loadDBConfig(){
$host = "localhost"; // MySQL Hostname
$user = "root"; // MySQL User
$pass = "mypass"; // MySQL Password
$dbname = "files";
return array('host' => $host, 'user' => $user, 'pass' => $pass, 'dbname' => $dbname);
}
?>
Upvotes: 0
Reputation: 239270
You need to pass the variables into the function as parameters. Variables declared outside a function are not available inside that function:
function dbConnect($user, $pass, $host, $dbname) {
try {
global $dbcon;
$dbcon = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
# ...
Read more about Variable Scope in PHP.
Upvotes: 1