|
Posted by IchBin on 08/11/06 15:08
Markus Ernst wrote:
> IchBin schrieb:
>> The api specs states for a 'loadQuery' that I should user either
>> an existing DB connection or a valid dsn for the first parameter. Well
>> I got in to my head to use the $dsn var. Well I kept that var in
>> another php file 'config.php'. So come time to use it, even having an
>> include for that file, the running php script could not find it.
>
> Just a guess - the missing $dsn variable could be a namespace issue.
> Variables defined inside a function or a class will not be available in
> the global namespace. For example, if you connect via a function like:
>
> <?php
> function &connect_db($host, $user, $pass) {
> $dsn = array('host'=>$host, 'user'=>$user, 'pass'=>$pass);
> include_once('DB.php');
> $db =& DB::connect($dsn);
> return $db;
> }
> $db =& connect_db('myhost', 'myuser', 'mypassword');
> ?>
>
> $dsn is not set in the global namespace. Or the include happens inside a
> function or class:
>
> <?php
> function &connect_db() {
> include_once('config.php'); // contains the $dsn declaration
> include_once('DB.php');
> $db =& DB::connect($dsn);
> return $db;
> }
> $db =& connect_db();
> ...
> do_something($dsn); // I think that won't work
> ...
> include('config.php');
> do_something_else($dsn); // I think that won't work either
> ?>
>
> I assume that also here the scope of $dsn is limited to connect_db(),
> and include_once() prevents config.php from being included again.
>
Markus, thank you so much for your analysis. It's funny because last
night I sat down and wanted to find out the scope of Classes, functions,
and vars with respect to their reference. I would like to continue to
code in an OOD\OOP fashion as I do in Java. Since I am running php 5 I
am trying to find out the difference to Java's OOD/OOP implementation.
For the $dsn problem. I have not codded in OOD just yet. So in the
config.php I have the following for $dsn:
$dsn = array(
'phptype' => 'mysqli',
'username' => 'usern',
'password' => 'paswd',
'hostspec' => 'localhost',
'port' => '3306',
'database' => 'library',
);
$options = array(
'debug' => 2,
'portability' => DB_PORTABILITY_ALL,
);
I am not explicitly defining $dsn in a class or function. It is just in
the config.php file. The calling php file is called 'openDB.php' and has
a include for the 'config.file'. This is my openDB.php:
//
require 'DB.php';
require 'HTML/QuickForm.php';
require 'php_errors.php';
//
// Configure the database connection
include 'sql_selects.php';
include 'config.php';
$db =& DB::connect($dsn, $options);
if (PEAR::isError($db)) {
die($db->getMessage());
}
//
// return the requested DB resultset
Function getQueryData($query){
//
// Proceed with query...
$result =& $db->query($query);
//
// Check that result has no error
if (PEAR::isError($result)) {
die($res->getMessage());
}
return $result;
}
So in my main php file 'index.php' I would have the include for
'openDB.php' and eventually the reference to $dsn as in:
s->loadQuery($dsn, $category_dropdown_select);
where
s->loadQuery($db, $category_dropdown_select);
works and $dsn does not.
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Navigation:
[Reply to this message]
|