Help with PHP Class connection

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I keep getting a:

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

this is my class connection


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,
    $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");



and this is the page


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");



Re: Help with PHP Class connection wrote:
Quoted text here. Click to load it

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.

Site Timeline