FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Converting Perl to PHP, testing CLI with $_POST (newbie)
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Converting Perl to PHP, testing CLI with $_POST (newbie) [message #172107 is a reply to message #172101] Sun, 30 January 2011 22:21 Go to previous message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma:
Senior Member
In article <syk1p.6215$lO1(dot)920(at)newsfe11(dot)iad>,
"P E Schoen" <paul(at)pstech-inc(dot)com> wrote:

> Good points. I had read the Variable Scope and it seemed to me that a
> function would see and be able to use the global variable, but apparently
> not in this case within "class MyDB extends SQLite3".

If you have:

$myvar = 27;
somefunc (33);

function somefunc ($xyzzz)
{

echo $myvar . "\n"; // Undefined value
echo $xyzzz . "\n"; // echoes 33

}

You need to have:

$myvar = 27;
somefunc (33);

function somefunc ($xyzzz)
{

global $myvar; // Allows function to use global

echo $myvar . "\n"; // echoes 27
echo $xyzzz . "\n"; // echoes 33

}


> The public method is
> default and the keyword is not needed, but certainly passing the database
> filename in the function argument is much preferred, and the explicit public
> declaration is better than relying on the default. However, the following is
> even simpler and I don't see why I shouldn't use it:
>
> $db = new SQLite3($dbFile);

Here's my "myconnectdb" function that I use with SQLite3:

function myconnectdb ($db)
{

global $datarootpath;

$connstr = "sqlite:" . $datarootpath . $db;
$dbh = new PDO ($connstr);

$dbh->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

return $dbh;

}


Then, because I very occasionally get false SQLite errors [1], I do
something like this:

function myquery ($dbh, $sql, $where)
{

try {
$res = $dbh->query ($sql);
return $res;
}

catch (Exception $e)
{

$earray = $dbh->errorInfo ();
$msg1 = " general code: " . $earray[0] . " error: " .
$earray[1] . ", " . $earray[2];

writeLog ("", "SQLite error: " . $msg1);
$msg2 = " at " . $where . ", sql: _ " . $sql . " _";
writeLog ("", $msg2);

tidyExit ();

}

}

(so my logfile shows what was being tried, what the SQL statement was,
and where it was being tried from)

Then in code doing a query I might have:

$dbh = myconnectdb ("Settings");

$res = myquery ($dbh, "select user,password from accounts where
account='$account'", "DM1");
$reg = $res->fetchAll (PDO::FETCH_ASSOC);

$user = $reg[0]["user"];
$pass = $reg[0]["password"];
$dbh = null;


Note I haven't bothered in this instance to check that I only get one
row back. To see how many rows, do:

$num = count ($reg);

I don't know whether what I have here is capable of improvement, but it
works fine for me.


[1] Usually it claims the db schema has changed which is poppycock.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: passing variable to a nested array
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 20:45:52 GMT 2024

Total time taken to generate the page: 0.07212 seconds