Below is my function listRecordsMatching($accNo) which connects to my mySQL server to match all records that have a specific account number. I know my problem lies in the way I write my SQL.
My table has the following structure: (int)id, (string)cheque-no, (double)amount, (date)cheque-date, (string)customer, (int)account.
NOTE: conn() is a global function in my config file that returns the mysqli object connected to my server.
EDIT: Since the conn() function is leading to some confusion:
EDIT2: I have found the answer. After doing $stmt->execute() I must bind the results to variables to use them. $stmt->bind_results($var1, $var2, ...)
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$GLOBALS['conn'] = $conn;
function conn(){
return $GLOBALS['conn'];
}
function listRecordsMatching($accNo){
//prepare statement
$stmt = conn()->prepare("SELECT * FROM `mfs-cheque-app`.`cheques` WHERE `account` = ?");
$stmt->bind_param('i', $accNo);
$stmt->execute();
$row=$stmt->fetch(PDO::FETCH_ASSOC);
var_dump($row) // outputs NULL
echo "<form id='records'>1";
while(true){
if (!$row) break;
$c = new Cheque($row['cheque-no'],$row['amount'],$row['cheque-date'],$row['customer'],$row['account']);
echo $c->list(); // outputs formatted html
}
echo "2</form>";
}
OUT>>> NULL<form id="records">12</form>