1

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

I have next class:

<?php
    class DbHelper{
        private $databaseURL;
        private $databaseUName;
        private $databasePWord;
        private $databaseName;
        private $nameOfDbWithWorkers;
        private $connection;

        function __construct($dbURL, $dbUserName, $dbPword, $dbName, $nameOfDbWithWorkers){
            $this->databaseURL = $dbURL;
            $this->databaseUName = $dbUserName;
            $this->databasePWord = $dbPword;
            $this->databaseName = $dbName;
            $this->nameOfDbWithWorkers = $nameOfDbWithWorkers;
        }

        function setConnectionToDb(){
            $this->connection = mysql_connect($this->databaseURL,$this->databaseUName,$this->databasePWord) OR DIE("can't connect to DB"); 
            mysql_select_db($this->databaseName, $this->connection)or die ("Error while connecting to database");
        }

        function getUser($login, $pass){
            echo "$login, $pass";
            $query = "SELECT type FROM $this->nameOfDbWithWorkers WHERE login = '$login' and password = '$pass';"; 
            $queryResult = $this->getDataFromDbByQuery($query);
            if ((mysql_affected_rows($this->connection) == 1)){
                $meta = mysql_fetch_assoc($queryResult);
                if ($meta['type']=='admin'){
                    return 'admin';
                }
                if ($meta['type']=='user'){
                    return 'user';
                }
                else{
                    return 'nomatch';
                }
            }
            else{               
                 return 'nomatch';
            }           
        }

        function getDataFromDbByQuery($query){
            $this->setConnectionToDb();
            $result = mysql_query($query);
            mysql_close($this->connection);
            return $result;
        }
}
?>

and invoke this by

$dbHelp = new DbHelper($dbURL, $dbUName, $dbPword, $dbName, $nameOfDbWithWorkers);
$userType = $dbHelp->getUser($login, $pass);

And have a next error:

Warning: mysql_affected_rows(): 5 is not a valid MySQL-Link resource in Z:\home\ecl.ru\www\classes\dbhelper.php on line 27

What's wrong?

Community
  • 1
  • 1
Divers
  • 9,531
  • 7
  • 45
  • 88
  • 1
    You shouldn't open and close the database connection for each query to execute. You can execute `mysql_close()` at the end of your script instead. – Matthieu Napoli Jun 08 '11 at 09:44

4 Answers4

9

You shutdown the connection

    function getDataFromDbByQuery($query){
        $this->setConnectionToDb();
        $result = mysql_query($query);
        mysql_close($this->connection); // <----
        return $result;
    }

If you want to use it later in your script you shouldn't do this ;) However, because PHP closes all open itself at the scripts end you don't need to close it yourself at all.

Update (Just to avoid the next question ;))

You probably want to use mysql_num_rows() instead of mysql_affected_rows(). First one returns the number of rows returned by a SELECT-query, the other one returns the number of rows affected (;)) by a DELETE, INSERT, or UPDATE query. Oh, and REPLACE I forgot.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
2

mysql_affected_rows

Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE

and you are using SELECT..

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

hope that solves your problem. you do need to change bit of code to handle this.

Khurram Ijaz
  • 1,844
  • 5
  • 24
  • 43
1

first , use mysql_num_rows for row count , mysql_affected_rows returns number of changed rows , as is written in manual on php.net

Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier.

second - evry time you are using mysql function , put there link identifier or result identifier

SergeS
  • 11,533
  • 3
  • 29
  • 35
0

This is the signature for mysql_affected_rows():

int mysql_affected_rows ([ resource $link_identifier ] )

Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier.

link_identifier The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

So it requires an open link to the DB. However, your getDataFromDbByQuery() method closes the connection before you call the function:

    function getDataFromDbByQuery($query){
        $this->setConnectionToDb();
        $result = mysql_query($query);
        mysql_close($this->connection);
        return $result;
    }
Álvaro González
  • 142,137
  • 41
  • 261
  • 360