-1

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>
  • Have you manually entered your query into mySQL? That way you could test to see if your query actually returns anything. Also, confirm that the account field is an integer type. – wclark Nov 14 '17 at 21:29
  • I have done so, it returned all the rows I wanted. The account field in mySQL is an int and can be -1, 0, 1, 2, or 3. – Jonathan Mongeau Nov 14 '17 at 21:32
  • I don't think you need () in this: $stmt = conn()->prepare – wclark Nov 14 '17 at 21:36
  • why is this tagged as mysqli? – Funk Forty Niner Nov 14 '17 at 21:55
  • you're also mixing different mysql apis, you can't do that. – Funk Forty Niner Nov 14 '17 at 21:56
  • Fred, please refer to the my question where I say: "NOTE: conn() is a global function in my config file that returns the mysqli object connected to my server." I am not mixing mysql apis. Please remove this as already answered. Additionally there are no warnings or error messages. So the link to "what does this error mean in PHP" does not help my situation. – Jonathan Mongeau Nov 16 '17 at 16:23

1 Answers1

1

You are using 2 different Variables $accNo and $account ...change one of them to match the other and it will probably start working.

Forbs
  • 1,256
  • 1
  • 7
  • 9