473,288 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

problems with mysql_fetch_array($IsResult,MYSQL_NUM)

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;
}

};

?>

May 28 '07 #1
3 3069
On 28 Mai, 09:55, Patrick <hornep...@gmail.comwrote:
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;
}

};

?>

Your class seems to work (but i haven't tested it).

I suppose the passed query is invalid. And an try{}catch{} block to
your sqlQuery function and throw an error, if mysql_query fails.

Tip:
You increment the $numberofResults counter twice ("$numberofResults++"
and "echo $numberofResults++"), once is enough.
Use __construct() instead of CDatabase().

purcaholic

May 28 '07 #2
Your class is working fine. Please check weather the query is
executing or not. Check it using
mysql_query($qry) or die(mysql_error()); this code

May 28 '07 #3
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 - sc*********@the.spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') is -1"

May 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jerome Lefebvre | last post by:
Hello, Hope this will interest a few. I been working with a friend on the problems given out during the "International Collegiate Programming Contest" (ICPC) http://icpc.baylor.edu/icpc/ ....
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
1
by: 3f | last post by:
Hello; We have made a web application that people can download from our web site and installed on: Windows XP Windows 2000 Professional Windows 2003 Server Windows 2000 Server
5
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) -...
2
by: Ellen Graves | last post by:
I am having a lot of problems with DB2 8.3.1 on RH Linux AS2.1. Installing and running stored procedures is problematic. Stored procedures I have used for years on V7 on WinNT are now failing...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
0
by: Sergistm | last post by:
Hello World, :D I have a problem that it is making me crazy, I hope you can help me. I'm trying to execute a .exe file with the Procces.Start, and there is no problem when the file is on my...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.