I'd like some help understanding how to call the results of a function on separate pages within my simple web app. I'm quite new to php/pdo.
Most of the time I just want to create a simple SELECT function and display the data on another page.
I have a page containing all of my functions named class.crud.php, the contents are as follows (minified for display purposes);
class.crud.php
class crud {
private $db;
function __construct($DB_con) {
$this->db = $DB_con;
}
public function testing() {
$stmt = $this->db->prepare("SELECT * FROM users WHERE user_id=:userId");
$stmt->bindparam(":userId", $_SESSION['user_session']);
$stmt->execute();
$results = $stmt->fetch(PDO::FETCH_ASSOC);
//var_dump($results); this dumps the results successfully on the desired page
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
print($row['user_id']);
print($row['user_name']);
echo $row['user_id'];
echo $row['user_name'];
}
} else {
echo 'nothing here';
}
return true;
}
}
How do I display the results of this testing() function on any other page? I have tried the following but it doesn't work (no data displayed at all, although if I uncomment the var_dump($results) I can see an array of data displayed);
user-details.php
<div>
<p>User Details</p>
<?php $crud->testing();?>
</div>
A few questions;
- If I
echoorprintvariables within my functions - how do I access this data on another page? - Should I
return trueorreturnanything at all? - Should I format the data within my functions - i.e tabularise it. Or leave this to the page where I want it displayed?
My database connection is fine and I have many functions that work. However they are very simple functions that I have been using for testing.
Any help is appreciated in understanding the logic. I have read so many tutorials :/