0

I have php code that uses general queries but I want to convert it to a prepared statement. I keep receiving errors when trying to create an array with the prepared queries.

Here is the array that was created with a general query that works perfectly.

public function tickets () {
    $this->db_connection = new mysqli('', '', '', '');
    $sql = "SELECT * FROM tickets WHERE member_id = '1'"; 
    $query = $this->db_connection->query($sql); 
    $me2 = array();

    while ($row = $query->fetch_object()) { 
        $me2[$row->ticket_id]['ticket_result'] = $row->ticket_result;
        $me2[$row->ticket_id]['member_id'] = $row->member_id;
    } 

    return $me2;
}

This is the new code I tried changing into a prepared statement that doesn't work. When I try echoing with a foreach loop a var_dump returns "NULL" values.

public function tickets() {
    $this->db_connection = new mysqli('', '', '', '');

    $sql = "SELECT * FROM tickets
            WHERE member_id = ? ";

    $stmt = $this->db_connection->prepare($sql);
    $id = "1";
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $stmt->store_result();   

    $me2 = array();

    while ($row = $stmt->fetch()) { 
        $me2[$row->ticket_id]['ticket_result'] = $row->ticket_result;
        $me2[$row->ticket_id]['member_id'] = $row->member_id;
    }  
    return $me2;
 }

Is it necessary to use prepared statements with a query like this since the query doesn't involve any user input?

1 Answer 1

1

To answer your last question

Is it necessary to use prepared statements with a query like this since the query doesn't involve any user input?

Not really. Prepared statements are designed to fight against SQL injection attacks, that can only happen from badly sanitized user input. If you are creating the query without user input, you would get away with just using your original implementation.

Regarding the NULL values within you while loop, this would suggest that your query is failing.

$stmt->bind_param('i', $id);

The above function is incorrect and is most likey why the query is not working. "1" is not an integer, which the i suggests from the PHP Manual. Use $i = 1 instead (notice no quotes wrapped around the value)

Sign up to request clarification or add additional context in comments.

8 Comments

could anything bad happen from me using the general query i posted in the question? (e.g. sql injection)
By general query, if you mean the first query you posted, I don't think so as it is just a SQL SELECT that isn't using any variables obtained from a user. However, for the purposes of future development, I would suggest that you use the prepared statement approach. It is good to get in the habit of always using prepared statements because eventually you will be doing a query based on user input.
I changed it to $i = 1 but still I receive the null. When I do a var_dump on the variable $me2 this is what I receive. array(1) { [""]=> array(2) { ["ticket_result"]=> NULL ["member_id"]=> NULL } }
Try this: printf("Error: %s.\n", $stmt->error); Make sure you add it AFTER $stmt->execute. It should give you a clearer understanding of the error.
I get "Error:." returned. when I changed it to $stmt->errno it returned "Error:0.".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.