473,405 Members | 2,272 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,405 software developers and data experts.

Help with PHP Class connection

I keep getting a:

mysql_fetch_array(): supplied argument is not a valid MySQL...

this is my class connection

<?php

class cDatabase {

//class variables defined in constructor
var $host;
var $user;
var $password;
var $databaseName;

//constructor - needed for connection string
function cDatabase($hostName, $userName, $passwordName,
$databaseName){
$this->host = $hostName;
$this->user = $userName;
$this->password = $passwordName;
$this->database = $databaseName;
}
//execute a query
function ExecuteNonQuery($sql){
$conn = mysql_connect($this->host, $this->user, $this->password);
mysql_select_db ($this->database);

$rs = @mysql_query($sql);
settype($rs, "null");
mysql_close($conn);
}

}

?>
and this is the page

<?php

include "dbclass.php";

$cDB = new cDatabase(host, user, password, database);

// test select query
$rs = $cDB->ExecuteNonQuery("Select event from na_events");

while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
echo $row['event'];
}

// kill our objects
settype($rs, "null");
settype($cDB, "null");

?>

Thanks,

Jul 8 '07 #1
1 3630
vn****@hotmail.com wrote:
I keep getting a:

mysql_fetch_array(): supplied argument is not a valid MySQL...

this is my class connection

<?php

class cDatabase {

//class variables defined in constructor
var $host;
var $user;
var $password;
var $databaseName;

//constructor - needed for connection string
function cDatabase($hostName, $userName, $passwordName,
$databaseName){
$this->host = $hostName;
$this->user = $userName;
$this->password = $passwordName;
$this->database = $databaseName;
}
//execute a query
function ExecuteNonQuery($sql){
$conn = mysql_connect($this->host, $this->user, $this->password);
mysql_select_db ($this->database);

$rs = @mysql_query($sql);
settype($rs, "null");
mysql_close($conn);
}

}

?>
and this is the page

<?php

include "dbclass.php";

$cDB = new cDatabase(host, user, password, database);

// test select query
$rs = $cDB->ExecuteNonQuery("Select event from na_events");

while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
echo $row['event'];
}

// kill our objects
settype($rs, "null");
settype($cDB, "null");

?>

Thanks,
Let's see, where shall we start?

You're not checking to see if the connection is successful, you're not
checking if the database was actually selected, and you're not checking
to see if the query was successful.

Also, you're setting the type of the recordset returned by the query to
"null", so you've just destroyed the recordset itself. Anf you're not
returning $rs from ExecuteNonQuery().

And I'm not sure if it makes any difference or not - because I don't
close connections prematurely, but you're closing the connection before
you retrieve the results. It may not be a problem here - but if you
make another query in the same page, you'll have to open another
connection - a lot of overhead. You should only close a connection when
you're done with all of your MySQL work.

Try correcting these errors and adding error checking to print out error
messages when they occur.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 8 '07 #2

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

Similar topics

1
by: Naren | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote: >Naren wrote: >> MyServer class conatins a list of Mysock class. >> >> class Myserver >> { >> private: >> list<Mysock> L1; >> }; >>
4
by: Naren | last post by:
Hi, i am developing one application,that is completely object oriented application.the main purpose of this applicaiton to completely maintain the database operations for the project. i created...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
7
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
navanova
by: navanova | last post by:
Hi, I have two questions. pls help me!!! 1. I have created sql connection to sql server 2000 from a vb.net form. However, I just want to make the connection class and I also want to know how to...
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
6
by: S_K | last post by:
Hi all! I'm writing an ASP.NET web application that uses an Oracle database. I OPEN the Oracle connection by using the following code: if (this.ConnectionString != "") { this.Connection =...
2
by: dmstn | last post by:
Hey! I've got a little problem. I have to make a web site for a university essay. I curently have to create a search engine. Users can enter a hotel name in a search bar and results have to appear in...
6
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.