PHP and MySQL: Novice to Ninja 6th edition

Hello Everyone.

I just started learning PHP using the Sitepoint’s Novice to Ninja book by Tom and Kevin. Whilst following through the example code in Chapter 7 - Improving the Insert Function, I came about an error that has had me stumped for some days now. I have tried googling the error and re-run the code as seen in the book, but I still get the error.

Database error: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in C:\laragon\www\novice2ninja\project\includes\DatabaseFunctions.php : 31

I have gone back and forth, trying to find out where the error is coming from. I even copied and pasted the code from the book directly to my text editor, but the error persists.

Please I need help with an explanation of the error and how to solve it. Thank you

I dont have access to the book, so this may be a @TomB question.

it sounds like the database function you are calling expected you to bind some parameters into your query and that didnt happen. Are you certain you’ve copied all of the lines from the example?

Also, which specific example are you running that is generating this error? Again, don’t have the book, but i’m guessing there’s more than 1 example in Chapter 7.

Thanks @m_hutley for your reply.

This is a link to the online version of the book.

I’m positive I did copy the lines from the example, but there’s still room for errors, I guess.

The example code is in the chapter 7 sub-section which deals with modifying the insert function.

The error comes up after the insert function has been amended and I try to add new joke entries into the database.

Here’s the code from the book (Chapter 7):

function insertJoke($pdo, $fields) {
    $query = 'INSERT INTO `joke` (';

    foreach ($fields as $key => $value) {
        $query .= '`' . $key . '`,';

    $query = rtrim($query, ',');

    $query .= ') VALUES (';

    foreach ($fields as $key => $value) {
        $query .= ':' . $key . ',';

    $query = rtrim($query, ',');

    $query .= ')';

    foreach ($fields as $key => $value) {
        if ($value instanceof DateTime) {
            $fields[$key] = $value->format('Y-m-d');

    query($pdo, $query, $fields);

or the earlier version of the function:

function insertJoke($pdo, $joketext, $authorId) {
	$query = 'INSERT INTO `joke` (`joketext`, `jokedate`, `authorId`) 
			  VALUES (:joketext, CURDATE(), :authorId)';
	$parameters = [':joketext' => $joketext, ':authorId' => $authorId];
	query($pdo, $query, $parameters);

There are many samples so you’ll need the correct one.

Try comparing it to yours to see what’s wrong. The code is also available online here: which example is it you are on?


Thanks @TomB

I was actually on the previous version of the function before the CURDATE() was amended. That is the Structure2-ArrayFunctions-Error . I was expecting to get the error:

Database error: SQLSTATE[HY000]: General error: 1364 Field ‘jokedate’ doesn’t have a default value

Instead I was getting the error I complained about.

Going through the code you posted, I found out I left out the $fields parameter when calling the query() function.

I had this: query($pdo, $query), instead of query($pdo, $query, $fields)

I also noticed it also written the same way in the book.

Thank you for the assistance. I’ve enjoyed the book so far.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.