A bit of PHP help...

Page may contain affiliate links. Please see terms for details.

Spinal

MB Enthusiast
Joined
Sep 14, 2004
Messages
4,806
Location
between Uxbridge and the Alps
Car
x254, G350, Duster, S320, Mach1, 900ss and a few more
I'm working on my site, and I've decided to redesign the backend so I can upload images remotely...

I've also fiddled with the format, so in essence I want to be able to edit some basic components without using dreamweaver...

Anyhow, I've made a file "database.php" with my database details, so that throughout the site I can just call that...

PHP:
<?php
$DEBUG = true;

$db_name = "dbname";
$db_server = "localhost";
$db_user = "dbuser";
$db_pass = "dbpassword";

//$db_table   = "photos";
//$another_table  ="some_other_table"; 
//...etc (not yet implemented for more than one table)

function runQuery($q) {
	//if ($DEBUG){
	echo "($db_server, $db_user, $db_pass)";
	//}

	$link = mysql_connect($db_server, $db_user, $db_pass);
	@mysql_select_db($db_name) or die( "Unable to select database");
	mysql_query($q);
	mysql_close();
}
?>

This way, anywhere I need to run a query, I should be able to do this:
PHP:
require 'database.php';
$query = "DROP TABLE photos";
runQuery($query);

right?

Now comes the part that I don't understand... See that echo statement (where I've commented out the if($DEBUG)?) - that shows me that all those variables are BLANK.... NULL... and I can't figure out why! Surely, as I've defined them as global variables three lines above they should still be instantiated?

M.
 
Scope issue - those variables are only global within the PHP file, not the local scope of the function, global declaration needed in the function - as example 3 here.
 
Scope issue - those variables are only global within the PHP file, not the local scope of the function, global declaration needed in the function - as example 3 here.

Thanks!

I thought it as a scope issue... but didn't know I could override the local variables with "pseudo"-globals (not really global if they can't be seen from within a function in the same file, now are they? :p)

Will try as soon as dreamweaver boots up...

Thanks again!
M.

EDIT: Yups - that sorted it! (I've decided that I didn't really have a need to have those variables as global, so I just moved them into the function...)

For some odd reason, I thought that PHP acted like Java and just saw all the global variables, but allowed you to override them within functions... live and learn :)

Thanks again
 
Last edited:

Users who are viewing this thread

Back
Top Bottom