For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > November 2007 > While Loop Problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author While Loop Problem
Jesse Burns aka jburns131

2007-11-21, 4:02 am

I've got a coding problem here that I could use some help with.

Here's my db class (the config.php only has db connection info):

****************************************
****************************

<?php

// db_handler.inc

require_once("config.php");

class db {

var $mySQLresult;

function db() {

$this->dbConn = mysql_connect(DB_HOST, DB_USER, DB_PW);
mysql_select_db(DB_NAME, $this -> dbConn) or
die(mysql_error());
}

function db_Select($tables, $columns="'*'", $conditions="") {

$sql = "SELECT ".$columns." FROM ".$tables." ".$conditions;
$row = mysql_query($sql, $this -> dbConn) or
die(mysql_error());
$this->mySQLresult = $row;
}

function db_Fetch($type = MYSQL_BOTH) { //MYSQL_ASSOC
$row = mysql_fetch_array($this -> mySQLresult, $type) or
die(mysql_error());
if ($row) {
return $row;
} else {
return FALSE;
}
}
}

?>

****************************************
****************************

Here's the code I'm executing:

****************************************
****************************

<?php

require_once("includes/db_handler.inc");
require_once("includes/config.php");

echo "Hit 1<br />";

$sql = new db;
$result = $sql -> db_Select(DB_TABLE_ORG_INFO);
while ($row = $sql -> db_Fetch()) {

$organization_disclaimer =
$row['organization_disclaimer'];
}

echo "Hit 2<br />";

?>

****************************************
****************************

For some reason, the while loop is halting execution and won't display
"echo "Hit 2<br />";". The script just stops executing.

Any idea's why?

I'm using php5
Rik Wasmus

2007-11-21, 8:00 am

On Wed, 21 Nov 2007 06:28:04 +0100, Jesse Burns aka jburns131 =

<jburns131@gmail.com> wrote:
> I've got a coding problem here that I could use some help with.


> $row =3D mysql_fetch_array($this -> mySQLresult, $type) or =


> die(mysql_error());


Don't use 'or die()' here. If there are no further results (rows are =

finished), mysql_fetch_array() will return false, the die() will be =

executed, with nothing to display as there is no mysql_error(), it's jus=
t =

worked correctly.

And don't die() in production. Log the error, and degrade as gracefull a=
s =

you can.
-- =

Rik Wasmus
Jesse Burns aka jburns131

2007-11-24, 7:01 pm


Sorry for the delay in response, I've had trouble with my connection.

That was my problem. Thank you very much for the help and advise.

I hope you and everyone else had a great holiday.


"Rik Wasmus" <luiheidsgoeroe@hotmail.com> wrote in message
news:op.t14z8k1c5bnjuv@metallium.lan...
On Wed, 21 Nov 2007 06:28:04 +0100, Jesse Burns aka jburns131
<jburns131@gmail.com> wrote:
> I've got a coding problem here that I could use some help with.


> $row = mysql_fetch_array($this -> mySQLresult, $type) or
> die(mysql_error());


Don't use 'or die()' here. If there are no further results (rows are
finished), mysql_fetch_array() will return false, the die() will be
executed, with nothing to display as there is no mysql_error(), it's just
worked correctly.

And don't die() in production. Log the error, and degrade as gracefull as
you can.
--
Rik Wasmus


Rik Wasmus

2007-11-24, 10:04 pm

> "Rik Wasmus" <luiheidsgoeroe@hotmail.com> wrote in message
> news:op.t14z8k1c5bnjuv@metallium.lan...
> On Wed, 21 Nov 2007 06:28:04 +0100, Jesse Burns aka jburns131
> <jburns131@gmail.com> wrote:
>
>
> Don't use 'or die()' here. If there are no further results (rows are
> finished), mysql_fetch_array() will return false, the die() will be
> executed, with nothing to display as there is no mysql_error(), it's j=

ust
> worked correctly.
>
> And don't die() in production. Log the error, and degrade as gracefull=

as
> you can.

On Sat, 24 Nov 2007 20:32:13 +0100, Jesse Burns aka jburns131 =

<jburns131@jbwebware.com> wrote:
> Sorry for the delay in response, I've had trouble with my connection.
>
> That was my problem. Thank you very much for the help and advise.
>
> I hope you and everyone else had a great holiday.


You're welcome, there was no holiday in The Netherlands I'm aware off, =

however, I'll finally have 1 w of in 2 ws, and I'll make the most =
of =

it :)
-- =

Rik Wasmus
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com