Saturday, June 2, 2018

PHP reporting that I'm feeding in a boolean..I'm not





I'm creating a simple blog system for myself -- it is done, but any time I view the posts page, PHP spits out an error about how I'm feeding in a boolean to mysqli_fetch_array. Last I checked, the query I'm checking spits out a number.



Any ideas?



Code:




$numPostsQry = $database->queryDB("SELECT COUNT(*) FROM mainSite_blogarticles WHERE articleCat='News'");
$numPosts = $database->fetchArray($numPostsQry);





fetchArray is just my classes alias for mysqli_fetch_array, and queryDB is the alias for mysqli_query.







My code for both functions, fetchArray and queryDB is:



query-



public function queryDB($query) {
try {

if ($this->connector == 'mysql') {
return mysql_query($query);
}


elseif ($this->connector == 'mysqli') {
return mysqli_query($this->connection,$query);
}

} catch (exception $e) {
return $e;
}
}



fetch-



public function fetchArray($toFetch) {
try {

if ($this->connector == 'mysql') {
return mysql_fetch_array($toFetch);
}


elseif ($this->connector == 'mysqli') {
return mysqli_fetch_array($toFetch);
}

} catch (exception $e) {
return $e;
}
}



EDIT (entire class if needed):




class database {

private $host;
private $user;
private $pass;
private $db;

private $connector;
public $connection;


function __construct($host, $user, $pass, $db, $connector='mysql') {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
$this->connector = strtolower($connector);

}


public function openConnection() {
try {

if ($this->connector == 'mysql') {
$this->connection = mysql_connect($this->host,$this->user,$this->pass);
if (mysql_errno()) {
printf('Failed to connect: %s', mysql_error());

} else {
mysql_select_db($this->db, $this->connection);
}
}

elseif ($this->connector == 'mysqli') {
$this->connection = mysqli_connect($this->host,$this->user,$this->pass);
if (mysqli_errno($this->connection)) {
printf('Failed to connect: %s', mysqli_error());
} else {

mysqli_select_db($this->connection, $this->db);
}
}

} catch (exception $e) {
return $e;
}
}



public function closeConnection() {
try {

if ($this->connector == 'mysql') {
mysql_close();
}

elseif ($this->connector == 'mysqli') {
mysqli_close($this->connection);
}


} catch (exception $e) {
return $e;
}
}

public function slashString($string) {
try {

return addslashes($string);


} catch (exception $e) {
return $e;
}
}

public function stripSlashString($string) {
try {

return stripslashes($string);


} catch (exception $e) {
return $e;
}
}

public function escapeString($string) {
try {

if ($this->connector == 'mysql') {

return mysql_real_escape_string($string);
}

elseif ($this->connector == 'mysqli') {
return mysqli_real_escape_string($this->connection,$string);
}

} catch (exception $e) {
return $e;
}

}


public function queryDB($query) {
try {

if ($this->connector == 'mysql') {
return mysql_query($query);
}


elseif ($this->connector == 'mysqli') {
return mysqli_query($this->connection,$query);
}

} catch (exception $e) {
return $e;
}
}

public function dbNumRows($query) {

try {

if ($this->connector == 'mysql') {
return mysql_num_rows($query);
}

elseif ($this->connector == 'mysqli') {
return mysqli_num_rows($query);
}


} catch (exception $e) {
return $e;
}
}

public function fetchAssoc($toFetch) {
try {

if($this->connector == 'mysql') {
return mysql_fetch_assoc($toFetch);

}

elseif($this->connector == 'mysqli') {
return mysqli_fetch_assoc($toFetch);
}
} catch (exception $e) {
return $e;
}
}


public function fetchArray($toFetch) {
try {

if ($this->connector == 'mysql') {
return mysql_fetch_array($toFetch);
}

elseif ($this->connector == 'mysqli') {
return mysqli_fetch_array($toFetch);
}


} catch (exception $e) {
return $e;
}
}
}

?>

Answer




Try using this code:



$numPostsQry = $database->queryDB("SELECT COUNT(*) FROM mainSite_blogarticles WHERE articleCat='News'");

if ($numPostsQry ===false) {
printf("Error: %s\n", mysqli_error($database));
exit();
}

$numPosts = $database->fetchArray($numPostsQry);



and you can see why you are getting an error


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...