0

Why it is different

I know what the error means (which is one of the answer listed), but am unable to determine what the cause of the error is. It may be something simple I am overlooking, sometimes these things just require a fresh mind. To digress I have searched this site, and google for 2 days and have viewed several threads that do not provide a satisfactory answer to resolve the issue.

Image of SQL Table

Table that query is selecting

Purpose of Code

User purchases small items from points they acquire by obtaining certain goals. (Ex: Walk 5 miles and get a point). This is basically a table that displays the purchases that the use has made with said points..

Error:

Fatal error: Call to a member function bind_param() on a non-object in [DIR OF FILE] on line 137

Line 137

$Select_Points_Receipt_stmt->bind_param('ss', $User, $Type);

conn Variable:

It is pulled from a include file [configure.php]

//Connect to Database
$conn = new mysqli('SERVER', 'DB USERNAME', 'DB PASSWORD', 'DB TABLE');

Code in Question:

$User = $_POST['User'];
        $Type = 'Wellness';

        $Select_Points_Receipt_Query = "SELECT Purchase_Key, Purchase_Amount, Purchase_Item, Purchase_Date From Purchase WHERE Purchase_User = ? AND Purchase_Type = ?";
        $Select_Points_Receipt_stmt = $conn->prepare($Select_Points_Receipt_Query);
        $Select_Points_Receipt_stmt->bind_param('ss', $User, $Type);
        $Select_Points_Receipt_stmt->execute();

        $Select_Points_Receipt_stmt->bind_result($Purchase_Key, $Purchase_Amount, $Purchase_Item, $Purchase_Date);

        echo'<h3>Previous Purchases</h3>';
        echo'<table>
                <tr>
                    <th>Receipt</th>
                    <th>Amount</th>
                    <th>Item</th>
                    <th>Date</th>
                </tr>';
        while (mysqli_stmt_fetch($Select_Points_Receipt_stmt)) 
            {
                echo'<tr>';
                echo'<td>'.$Purchase_Key.'</td>';
                echo'<td>'.$Purchase_Amount.'</td>';
                echo'<td>'.$Purchase_Item.'</td>';
                echo'<td>'.$Purchase_Date.'</td>';
                echo'</tr>';
            }

        echo'</table>';
        echo'<br>';

Thanks for the help!

Jason
  • 63
  • 6
  • and `$Type` is defined where? all elements named also with no typos, lettercase issues? – Funk Forty Niner May 19 '15 at 13:40
  • $Type is defined as 'Wellness' (see Second line), and as far as I am aware every element is named. – Jason May 19 '15 at 13:42
  • my bad. well, non-object may probably mean that your form element isn't named or contains typos, or your query failed. check for errors on both PHP and DB side. – Funk Forty Niner May 19 '15 at 13:44
  • 1
    Add error reporting to the top of your file(s) right after your opening PHP tag for example `execute();` to `if(!$Select_Points_Receipt_stmt->execute()){trigger_error("there was an error....".$conn->error, E_USER_WARNING);}` – Funk Forty Niner May 19 '15 at 13:45
  • Thanks I will try that and get back with you. – Jason May 19 '15 at 13:46
  • I tried that and it gave me the exact same error. I also changed the the $User variable to a string value that I know exists, to rule out the previous form. – Jason May 19 '15 at 13:52
  • well, check for lettercase for your columns. Wellness and welness with a lowercase "w" may be two different animals. I don't know what else it could be. – Funk Forty Niner May 19 '15 at 13:59
  • 1
    MySQLi should be holding onto an error after the `prepare()` because it is `$Select_Points_Receipt_stmt` which is not an object, meaning prepare failed. Just before calling `bind_param()` simply check `echo $conn->error;` – Michael Berkowski May 19 '15 at 14:06
  • If anything, to really differentiate your question, rewrite it to ask the concrete question you should already be aware of: **Why does mysqli::prepare return false (and how to debug it)?** Strip everything else out. – deceze May 19 '15 at 14:08
  • Side note - for consistency I'd recommend sticking with the OO-style MySQLi entirely. So instead of using `mysqli_stmt_fetch()` in the while loop, you may use `while ($Select_Points_Receipt_stmt->fetch())` – Michael Berkowski May 19 '15 at 14:10
  • Thanks Michael, your suggestion helped me determine the error.. It appears their was a sql query above (one that pulled the amount of points a user has and displayed them) A quick add of $Select_Points_stmt->close(); and I was able to get it resolved.. Apparently, it is pert near impossible to run two queries, and either the extract from the database has to be stored instead of binded, or the query closed before the net one starts.. – Jason May 19 '15 at 14:15
  • @Jason Yes, the old "commands out of sync" error I presume? At least MySQLi's API has improved in recent years to make fetching all rows easier. – Michael Berkowski May 19 '15 at 14:40
  • Yes, you are correct, out of sync. Thanks again for the help – Jason May 19 '15 at 14:44

0 Answers0