0

There are many questions on stackoverflow regarding an error printing "Array to string conversion" but none seem to help me.

I'm trying to execute a sql select statement with php. Here is a bit of what im dealing with:

public static function getNumCGS(){
    require 'includes/vars.php';
    $paramArray = array(":table" => "campground_data");
    $query = Utilities::dbSelect("SELECT count(*) FROM :table","campgrounddata", $paramArray);
    $r = $query->fetch();
    return $r["count(*)"];
}

In Utilities.php:

public static function dbSelect($selectStmt, $dbName, $params){
    require_once "config/config.php";
    $dbh = Config::connect($dbName);
    $query = $dbh->prepare($selectStmt);
    $query->setFetchMode(PDO::FETCH_ASSOC);
    $query->execute($params) or die("DB Error: " . $query->errorInfo());
    return $query;
}

In Config.php:

public static function connect($dbname){
    $dbserver = "localhost";
    $dbuser = "sys";//just a local server
    $dbpass = "test1";//just a local server
    try {
        $dbh = new PDO("mysql:host=$dbserver;dbname=$dbname", $dbuser, $dbpass, NULL);
        return $dbh;
    }
    catch (PDOException $e) {
        printf("ERROR: %s", $e->getMessage());
        exit();
    }
}

If I call getNumCGS() It tells me that I have an Array to string conversion problem at the line that executes the query in the dbSelect function.

I've been following an example from a text book, not sure what the deal is. The example I'm seeing passes in an array into the execute call.

Khaines0625
  • 387
  • 1
  • 5
  • 17

0 Answers0