mysql - using class in php and displaying results -
i want display results retrieving mysql db in php.
i have files call db_functions.php following :
<?php class db_functions { private $con; // constructor function __construct() { require_once __dir__.'/db_connect.php'; // connecting database $db = new db_connect(); $this->con = $db->getdbconnection(); } public function selectuser($id) { try { $stmt = $this->con->prepare('select * user id = :id'); $params = array(':id' => $id); $stmt->execute($params); return $stmt; } catch(pdoexception $e) { echo 'error: ' . $e->getmessage(); } } public function othersqlfunction($parameter) { // other sql code } }
i call function :
<?php require_once __dir__.'/db_functions.php'; $db = new db_functions(); $result = $db->selectuser($id); ?>
how use function , display results , lets want display "affid "
you call selectuser
function , either return $stmt
or echo 'error: ' . $e->getmessage();
.
if returns $stmt
variable stored in $result
variable.
you can use $result
you'd (echo $result
, foreach $result $item
, etc,).
Comments
Post a Comment