Some code I'm working with has the line:
Code: Select all
if ($result->GetNumRows() > 0) {
The code errors at this point.
Is this a blatant mistake, or is there something else that "->it can signify?
Moderator: General Moderators
Code: Select all
if ($result->GetNumRows() > 0) {
Nor does it necessarily return an object. It can call an object method that may or may not return a value (which could be of any data type), or read a property (which can also be of any data type) of that instantiated object... look for the presence or absence of braces, used when calling an object method but absent when accessing an object property.swraman wrote:I know it means to get an object from a certain class, but can it have another meaning?
Isht $result the class here? and GetNumRows() is the function in the class result that it is calling?jaoudestudios wrote:The class does not have to be called result, as $result is the object not the class (instance of the class I think it is called).
No, $result is the variable name that you have given to the instance of a class that was passed back as a return value by your call to mysql_query()swraman wrote:Isht $result the class here?
Code: Select all
$result = mysql_query('SELECT * FROM table');That is correctswraman wrote:and GetNumRows() is the function in the class result that it is calling?
Correct, so what code are you using to set $result?swraman wrote:Fatal error: Call to a member function GetNumRows() on a non-object in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\project.php on line 398
doesnt that mean that $result is not an instance of an object
Code: Select all
global $database;
$result = $database->Query("SELECT status FROM tr_projects WHERE id = '{0}'", $id);Code: Select all
require_once('pgsql_database.php');
$database = new PGDatabase;Code: Select all
class PGDatabase implements Database {
/**
* @access private
* @var resource resource handle for the database connection
*/
private $link;
/**
* @access private
* @var int the current transation level
*/
private $intrans;
public function __construct() {
$this->link = false;
$this->intrans = false;
}
....(a bunch of other functions)...
public function Query() {
$args = func_get_args();
$num = func_num_args();
$query = array_shift($args);
if (isset($args[0]) && is_array($args[0])) {
// The rest of the parameters are passed in as an array
$args = $args[0];
}
for ($i = 0; $i < $num - 1; ++$i) {
$query = str_replace("{{$i}}", $this->EscapeString($args[$i]),
$query);
}
$result = pg_query($this->link, $query);
TR_POSTCONDITION($result, 'Query Failure:', $query,
pg_result_error($result));
if (!is_bool($result)) {
return new PGQueryResult($result, $this->link);
}
}
...(and more functions)....
}Code: Select all
class PGQueryResult implements QueryResult {
/**
* @access private
* @var resource resource handle to the MySQL query
*/
private $result;
/**
* @access private
* @var resource the link identifier for this database
*/
private $link;
public function __construct($result, $link) {
TR_PRECONDITION($result, 'bad result');
$this->result = $result;
$this->link = $link;
}
public function __destruct() {
$this->FreeResult();
}
...(more functions)...
public function GetNumRows() {
return pg_num_rows($this->result);
}
(...more functions...)
}
Code: Select all
if (!is_bool($result)) {
return new PGQueryResult($result, $this->link);
}So I have to make the function Query return a value for the cases when $result is a boolean?pytrin wrote:The problem is with the instancing logic:
if $result here is false (ie query failed or returned no results), then no object would be returned, and you get the error you described earlier.Code: Select all
if (!is_bool($result)) { return new PGQueryResult($result, $this->link); }
Code: Select all
Warning: pg_query() [function.pg-query]: Query failed: ERROR: current transaction is aborted, commands ignored until end of transaction block in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 187
Warning: pg_num_rows(): supplied argument is not a valid PostgreSQL result resource in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 83
Warning: pg_free_result(): supplied argument is not a valid PostgreSQL result resource in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 79
Code: Select all
Warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near ")" LINE 1: INSERT INTO tr_projects() VALUES() ^ in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 187
Warning: pg_query() [function.pg-query]: Query failed: ERROR: current transaction is aborted, commands ignored until end of transaction block in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 187
Notice: Undefined variable: data in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 199
Warning: pg_query() [function.pg-query]: Query failed: ERROR: current transaction is aborted, commands ignored until end of transaction block in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 187
Fatal error: Call to a member function GetNumRows() on a non-object in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\project.php on line 398