What I am trying to achieve:
Explanation:
I am constructing a simple process where my class 'Content' will convert an array into objects and print them out on my webpage as they are called.
Expected Outcome:
$stmt PDO will bind parameters using $stmt->bindParam(). The bind variables match the $args in the construct. Then pass the array to a static method which will convert the array into objects.
Outcome
Using print_r(), I can track my PDO results to the stage of converting to $object. I will note the result in comments at the end of the code.
- As I move through the results, print_r() returns the database column names rather than the bindPram() variable names.
- In the end, I have a blank array displayed on my webpage.
To me it seems my bindParams() isn't working in my PDO.The PDO itself is working because I am able to select and print from the database. I am not sure what I have done incorrectly.
My code
<?php
class Content {
// --- START OF ACTIVE RECORD CODE ---
public $n_id; // bind with column name 'nid' in database
public $n_text; //bind with column name 'ntext' in database
// instantiating a STATIC Database Connection for the class to use
static protected $database;
static public function set_database($database) {
self::$database = $database;
}
// constructing arguments
public function __construct($args=[]) {
$this->n_id = $args['n_id'] ?? '';
$this->n_text = $args['n_text'] ?? '';
}
// Multi use method to pass in sql that will execute the PDO only two parameters are bound nid and ntext.
static public function look_all_sql($sql) {
// -------------BEGIN PDO-----------
// checking to ensure PDO extensions are loaded
if (!extension_loaded ('PDO' )) {
echo 'PDO is NOT loaded!'."\n";
if (!extension_loaded('pdo_mysql')){
echo 'PDO mysql driver is NOT loaded.'."\n";
}
}
// preparing PDO by loading sql from method find_all(), calling db connection, and storing in variable $stmt
$stmt = self::$database->prepare($sql);
// Binding Parameters for sql
$stmt->bindParam(':nid', $n_id, PDO::PARAM_INT);
$stmt->bindParam(':ntext', $n_text, PDO::PARAM_STR);
// executing $stmt PDO and storing the result in $stmt
$stmt->execute();
// ------------END PDO ----------
// Checking to see if a result exist. If no result is stored in $stmt, then it will echo "Database query failed." If result is stored, then the method will continue
if(!$stmt) {
exit("Database query failed.");
}
// -------- BEGIN TURNING RESULTS INTO OBJECTS --------
$object_array = [];
// The result $stmt will be stored in the variable $record
while($record = $stmt->fetch(PDO::FETCH_ASSOC)) {
// passing $record to static method instantiate() to be converted to an object and returned as $object_array
$object_array[] = self::instantiate($record);
}
// clearing the PDO after information is stored in $record
$stmt->closeCursor();
return $object_array;
// ------------ END TURNING RESULTS INTO OBJECTS --------
}
// method to test passing $sql to method look_all_sql();
static public function find_all(){
$sql = "SELECT * FROM `ncontent`";
//passing $sql to static method
return self::look_all_sql($sql);
}
// --- BEGIN INSTANTIATING METHOD TO CREATE OBJECTS ---
static protected function instantiate($record) {
$object = new self;
// Auto assign values
foreach($record as $property => $value) {
if(property_exists($object, $property)){
$object->$property = $value;
}
}
}
// ----- END INSTANTIATING OF RECORD TO CREATE OBJECTS ---
// ---END OF ACTIVE RECORD CODE---
}
?>
What is populated on webpage with no print_r
Nothing page is empty.
Outcomes based on print_r:
On $return
$return = $stmt->fetch(PDO::FETCH_ASSOC);
return print_r($return);
- Displayed on page:
Array ( [0] => Array ( [nid] => 2 [ntext] => This is the first Line called the Top Main )
On $record in look_all_sql($sql)
while($record = $stmt->fetch(PDO::FETCH_ASSOC)) {
return print_r($record);
Displayed on page:
Array ( [nid] => 2 [ntext] => This is the first Line called the Top Main ) 1
On $record from static method instantiate():
static protected function instantiate($record) {
return print_r($record);
- Displayed on page:
Displays the full array of database information. No bound prams. Acts like a fetchAll() command:
Array ( [nid] => 2 [ntext] => This is the first Line called the Top Main ) Array ( [nid] => 3 [ntext] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor ) etc
On $object in the static instantiate($record) method. The number of objects match the number of objects from my database. However, it does not correlate with the information from the database.
$object = new self;
return print_r($object);
At the end of the foreach() loop yields the same result. Displayed on webpage.
Content Object ( [n_id] => [n_text] => ) Content Object ( [n_id] => [n_text] => ) Content Object ( [n_id] => [n_text] => ) Notice: Array to string conversion in H:\Website\xampp\htdocs\hinkleycentersandbox\public\includes\maincontenttopsection.php on line 24 Array
At this point $object is supposed to be returned to $object_array[].
print_r on $object_array:
Displayed on page:
Array ( [0] => [1] => )
Code on webpage:
<h2><?php
$ncontents = array(Content::find_all());
foreach($ncontents as $ncontent) {
echo $ncontent;
}
?></h2>