Hello,

I know the problem that I am having is related to the change in PHP5 about strings and arrays, however I do not understand why my script is affected. It seems like I am not trying to add more variables to an array, I just want to look up the variable in an array and assign it to a simple string element. The two files below are related, one is a simple class to connect to the mysql database. the other file (script.php) simply calls a function and queries the database. It is possible that there are no records in the table hd_license.

script.php

$result = $this->db->queryResult("SELECT license_key FROM hd_license");
//On this next line below is where I keep receiving the error about using a string offset as an array
$this->key = $result[0][0];

dbInterface.php

<?php
class dbInterface {

var $_host;
var $_user;
var $_password;
var $_db;

var $_connection;
var $_result;
var $_items = array();



function dbInterface(){
	global $host, $user, $pass, $database;
	$this->_host = $host;
	$this->_user = $user;
	$this->_password = $pass;
	$this->_db = $database;
	$this-> _connection = $this->connectDB();
}

function connectDB(){
	if(mysql_connect($this->_host,$this->_user,$this->_password)){
		$this->_connection = mysql_connect($this->_host,$this->_user,$this->_password);
		mysql_select_db($this->_db,$this->_connection);
		return $this->_connection;
	}else{
		return mysql_error();
	}
}

function queryResult($sql){
	$this->_items = "";
	if($sql != ""){
		$this->_result = mysql_query($sql,$this->_connection);
		$item = array();
		while($item = mysql_fetch_array($this->_result)){
			$this->_items[] = $item;
		}
		return $this->_items;
		mysql_free_result($this->_result);
	}else{
		return mysql_error();
	}
}







}

?>

    I would say you have answered your own question: "It is possible that there are no records in the table hd_license."

    You do not check for query results in your queryResults function and your return mysql_error when the query string passed == "" and no query is executed is also wrong.

      Roger,

      Thank you for your advice. Could you possibly show me a better way to write that function? I really appreciate your help. Thank you!

      -Jason

        7 days later

        In the function, add

        if (mysql_num_rows($this->_result) > 0)
        {
        while ($item = ...
        }
        

        Also, before that statement, you might want to consider setting $this->items = array() if you want the function to always return an array (it will be empty if there are no results), or $this->items = FALSE (if you want to use something like -- if (!$DB->queryResult($query)) { print "There are no items" } ). In your current method, it will return an empty string if there are no results, which I can't imagine is a desired behavior.

          Write a Reply...