-1

I'm getting the following error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/pantyho1/public_html/affiliates/classes/XpDb.php on line 185

Here's my code:

class XpDatabase
{
var $mDbhost;
var $mDbuser;
var $mDbpwd;
var $mDbname;

/**
* Connects to database
*/
function connect()
{
    $link = mysql_connect($this->mDbhost, $this->mDbuser, $this->mDbpwd);
    if (!$link)
    {
        $error = 'Could not connect: '.mysql_error();
        $this->printError($error);
        die('Could not connect: ' . mysql_error());
    }

    if (!mysql_select_db($this->mDbname))
    {
        $error = 'Can\'t use database : ' . mysql_error();
        $this->printError($error);
        die ('Can\'t use database : ' . mysql_error());
    }       
}

/**
* Close connection to database
*
* @param $aConnection connection
*
* return bool
*/
function close($aConnection)
{
    return mysql_close($aConnection);
}

/**
* Executes sql query
*
* @param str $aSql sql query
*
* @return bool
*/
function query($aSql)
{
    return mysql_query($aSql);
}

/**
* Returns row of elements
*
* @param str $aSql sql query
*
* @return arr
*/
function getRow($aSql)
{
    $res = $this->query($aSql);
    $out = mysql_fetch_assoc($res);

    return $out;
}

/**
* Returns array of rows
*
* @param str $aSql sql query
*
* @return arr
*/
function getAll($aSql)
{
    $out = Array();

    $res = $this->query($aSql);
    while($temp = mysql_fetch_assoc($res))
    {
        $out[] = $temp;
    }

    return $out;
}

/**
* Returns recordset as associative array where the key is the first field
*
* @param str $aSql sql query
*
* @return arr
*/
function &getAssoc($aSql)
{
    $out = Array ();
    $res =& $this->query($aSql);

    while ($temp = mysql_fetch_assoc($res))
    {
        $key = array_shift($temp);
        $out[$key][] = $temp;
    }
    return $out;
}

/**
* Returns one element
*
* @param str $aSql sql query
*
* @return int
*/
function getOne($aSql)
{
    $res = $this->query($aSql);
    $row = mysql_fetch_row($res);

    $out = ($row ) ? $row[0] : '';

    return $out;
}

/**
* Returns array of tables
*
* @return arr
*/
function getTables()
{
    $out = Array();

    $sql = "SHOW TABLES FROM {$this->mDbname}";
    $res = $this->query($sql);

    while ($row = mysql_fetch_row($res))
    {
        $out[] = $row[0];
    }

    return $out;
}

/**
* Prints out block with error
*/
function printError($aError)
{
    echo $aError; 
}

/**
* Returns recordset as associative array where the key is the first field
*
* @param str $aSql sql query
*
* @return arr
*/
function getKeyValue($aSql)
{
    $out = Array ();
    $res = $this->query($aSql);

line 185    while($row = mysql_fetch_array($result))
     {
        $out[$temp[0]] = $temp[1];
     }
    return $out;
 }

}
Asaph
  • 154,284
  • 25
  • 189
  • 193
Beezer
  • 7
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**pink box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). (**and you're in luck by using a wrapper, so switching should be easy!**) – h2ooooooo Nov 16 '14 at 14:22
  • Not the same question. It may be a similar error message, but it's not the same cause. The problem here is a variable name typo. The problem in the "duplicate" is a failed query. – Asaph Nov 16 '14 at 14:50

1 Answers1

0

There are a few issues that I can see with the code you posted.

  1. You have a variable name mismatch:

    In your getKeyValue() function change:

    $res = $this->query($aSql);
    

    to

    $result = $this->query($aSql);
    
  2. Remove the string "line 185" which is almost certainly a typo or cut/paste error.

  3. Also a good idea: At the end of your connect() function, add this line if you'll ever want to use the connection resource created by your connect() function (such as in the case of opening 2 db connections to different databases at the same time):

    return $link;
    
Asaph
  • 154,284
  • 25
  • 189
  • 193
  • Now I'm Getting the following error: Parse error: syntax error, unexpected 'return' (T_RETURN), expecting ';' or ' I'm sorry but the line 185 code is as follows while ($temp = mysql_fetch_row($res)) – Beezer Nov 16 '14 at 14:32