|
Posted by Schraalhans Keukenmeester on 05/28/07 13:45
At Mon, 28 May 2007 00:55:35 -0700, Patrick let h(is|er) monkeys type:
> Hi
>
> I am trying to write a simple database class to encapsulate all the
> database functions in one. Howerver I am having problems with
> while($row = mysql_fetch_array($IsResult,MYSQL_NUM)) line it never
> executes the loop. $IsResult is reeves no value.
>
> What am I doing wrong
>
> Thanks
>
> pat
>
> <?php
>
> class CDatabase
> {
> // Database
> var $misOpen = false;
> var $mResult;
>
> function getResults()
> {
> return $this->mResult;
> }
>
> function isOpen()
> {
> return $this->misOpen;
> }
>
> // constructer
> function CDatabase($username,$password,$host,$databasename)
> {
> $db_connection = mysql_connect($host,$username,$password);
> $this->misOpen = false;
>
> if($db_connection)
> {
> $this->misOpen = true;
> mysql_select_db($databasename);
> }
>
> echo mysql_error();
> }
>
> function __destruct()
> {
> $this->disconnect();
> }
>
> function disconnect()
> {
> if ($this->isOpen())
> {
> mysql_close();
> }
> }
>
> function sqlQuery($query)
> {
> $numberofResults = 0;
> $IsResult = mysql_query($query);
>
> echo "IsResult= '".$IsResult."' <br/>\n";
>
> while($row = mysql_fetch_array($IsResult,MYSQL_NUM))
> {
> $this->mResult[] = $row;
> $numberofResults++;
> echo $numberofResults++;
> echo "loop";
> }
>
> return $numberofResults;
> }
>
> };
>
> ?>
Contrary to other comments' suggestion your class does not work fine yet.
But you knew that, or you hadn't come here ;-)
There's a few things worth paying further attention to:
1. The reason why your query does not execute.
You open a connection and store the result (resource or FALSE) in a local
variable. As soon as the constructor exits it goes out of scope and the
resource is gone.
Fix: change $db_connection to $this->db_connection and declare it as a
class variable.
2. Error checking consistence:
You check for a succesful mysql_connect() but don't check the result of
the mysql_select_db()
Fix: do a second result check after the mysql_select_db and postpone the
$misOpen = true assignment till after the second check.
You could combine the testslike so:
function CDatabase ($h, $u, $p, $db) {
$this->misOpen =
(($this->db_connection = mysql_connect($h,$u,$p)) &&
mysql_select_db($db))
// mysql_select_db only executes if mysql_connect returns true
// to suppress mysql warnings/errors, prepend the functions with '@'
}
Looking at the use of keyword 'var' in your script I assume you use PHP4.x
If so, ignore purcaholic's remark about using __construct().
If not (and you _are_ using PHP5) you ought to declare your vars without
the var keyword and since they should be hidden from the class user,
declare them private.
In the sqlQuery() function, you collect the result and print it's value (I
assume this is debugging extra code) but don't test the value before
you start fetching rows from the db. Besides, you don't know if there are
any rows to be fetched.
You might rewrite:
if ($result = mysql_query($query))
{
if (($numrows = mysql_numrows($result)) > 0)
{
while ($this->mresult[] = mysql_fetch_array ($result));
// if you want to display the counter and 'loop', remove the ; from
// previous line, and write your code here between { and }
}
return $numrows;
}
The try/catch block suggestion is nice, but I'd leave that for a later
stage when you feel a little more comfortable around PHP, imho. Or, if
you've become curious about it, study it on a separate trail and only
implement it in practical code when you have a good grasp of how, when and
why to use it.
And you know (of course?!) there are ready-to-use db classes and
extensions available ? PDO comes standard with PHP I believe, and PEAR has
a good universal db extension.
Lastly, I probably made some typos here and there, hope you manage to use
the suggested snippets nonetheless.
Have a nice day, and good luck programming!
--
Schraalhans Keukenmeester - schraalhans@the.spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]
"strcmp('apples','oranges') is -1"
Navigation:
[Reply to this message]
|