0

There are a lot of questions about this, but still I can't find a question which answers my question.

I got some code (obviously), which does a query to the database, and for some reason it is returning an error.

// ^ somewhere in the top session_start();
require("rw_conx.php");
try {
    // Get answers from database 
    $db = new PDO ("mysql:host=$host;dbname=$dbname", $username, $pass);
    $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $stmt = $db->prepare("SELECT antwoorden, commentaar
                        FROM antwoorden
                        WHERE vragenlijst = :vragenlijst
                        AND meting = :meting
                        AND behandeltraject = :behandeltraject
                        AND onderzoeksNR = :onderzoeksnummer");
    $vragenlijst = "1";
    $meting = "1";
    $onderzoeksNR = trim(preg_replace('#[^0-9]#', '', $_SESSION["onderzoeksnummer"]));
    $behandeltraject = trim(preg_replace('#[^0-9]#', '', $_SESSION["behandeltraject"]));        
    $stmt->execute(array('vragenlijst'=>$vragenlijst,'meting'=>$meting,'behandeltraject'=>$behandeltraject,'onderzoeksnummer'=>$onderzoeksNR));
    // Render all questions (check if 5's should be hidden), input disabled, no answer categories.          
    $num_rows = $stmt->rowCount();
    $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);

    // What this does is create a string like: 1=2,2=4,3=5|||COMMENTS|||1=blabla,2=blabla
    if ($num_rows > 0) {
        echo $row[0];
        echo "|||COMMENTS|||";
        echo $row[1];
    } else {
        echo "no data";
    }
} catch(PDOException $e) {
    echo "I'm sorry, I'm afraid I can't do that. (1)";
    file_put_contents('../debug.log', $e->getMessage(), FILE_APPEND);
    exit();
}   

$stmt->execute(array('vragenlijst'=>$vragenlijst,'meting'=>$meting,'behandeltraject'=>$behandeltraject,'onderzoeksnummer'=>$onderzoeksNR));

--- EDIT ---

Just to make this clear, I will explain.

1. I mentioned in my question above, that the sessions were no problem.

What I meant by this was the fact, that the error that occured (SQLSTATE[HY093]) was not thrown because the sessions were empty.

2. The problem still occurs.

Even though the session_start() fixed the query (It now returns the row it should), I am still getting the same error, which is (of course) not supposed to happen.

I hope this clears things up a bit, because everybody is getting mad at me. I did indeed mention the sessions were no problem, and they are no problem for the error that occurs. For some reason the catch function still gives me an error, even though the query succesfully runs.

So, if there are still people who would like to help me out, I would really appreciate it.

--- END EDIT ---

Does anyone see the (probably obvious) flaw?

30
  • Check your column names in your table and make sure the types are correct. Commented Dec 29, 2014 at 12:49
  • As a test, can you try using ? instead of :name for your parameters in the query, and use array($vragenlijst, $meting, ...) in the execute call? Commented Dec 29, 2014 at 12:50
  • 1
    Oh... wait a minute. You mention sessions. Did you start the session? Is session_start(); included? You mention in a comment below something about var_dump($_SESSION); Commented Dec 29, 2014 at 13:05
  • 1
    @Fred-ii- LOL turns out you're right, because I was sending a request to a page, instead of including it... You're da MAN! I thought I was including the page. Thank you so much :P Commented Dec 29, 2014 at 13:08
  • 1
    @Fred-ii- You can if you like, you earned it :) Commented Dec 29, 2014 at 13:09

3 Answers 3

2

The problem you are facing is due to a zero width space(​ in unicode). This might be automatically added in the editor that you are using. jQuery will also insert zero width space characters as you can read here.

But why is it giving me the SQLSTATE[HY093] error?

This is because $behandeltraject is not defined(there is a zero width space in front of it) so the parameters will not match.

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

3 Comments

Way to go Guy. I have seen that before (unicode), but inside jQuery scripts that would break the OP's code, but never like this. I have modified my answer slightly.
@Fred-ii- Yeah, it's a strange issue. I guess the OP copied the code from somewhere else(probably a Dutch PHP lesson). I am glad we have solved it together(session + zero width space).
Indeed strange Guy. First time I got wind of a unicode character in OP's code some time back, pasted the whole thing in strangely enough (Notepad, then MS Frontpage). Frontpage displayed the hidden character as ​ where I started Googling it, having found what it was. There are other hidden characters that find their way into code, as to how they get there is sometimes a mystery; type of editor used or done online(?). I now use a proper IDE, but those that I've mention still serve me well (to a certain extent). Cheers and glad this matter was resolved; finally.
2

Seeing that the mystery has been solved by GuyT <=(edit), am posting the following as per a comment I left under OP's question:

You mention sessions. Did you start the session?

Is session_start(); included?

You mentioned in a comment in another answer about var_dump($_SESSION);

session_start(); is required to be inside all files using sessions.


Original answer, added one above:

You forgot the $ sign for onderzoeksNR in

'onderzoeksnummer'=> onderzoeksNR
                    ^ right there

in regards to

$onderzoeksNR = "1";

change it to

'onderzoeksnummer'=>$onderzoeksNR

8 Comments

Good job, I was sure it had to be something obvious, but this? Cheers!
@JiFus Yeah, sometimes it's those little wee-things that makes it harder to figure out why something fails. I'm so glad that we were able to solve it :) Cheers and you're very much welcome.
Exactly what my guess was, first but OP said "noh I checked, it is correct, session not problem"
@DanFromGermany The // Normally this are sessions... in OP's code had me raise a brow from the start and I should have questioned it originally. This one was a tricky one.
@DanFromGermany Btw, is that your DV? If it is then it's uncalled for, don't be sore, you've no reason to attack me. Your answer wasn't the solution. I earned mine and asked the right questions.
|
0

My guess is, that either the error is coming from a different statement, or one of the values is an array or null in your code using the session variables

// Normally this are sessions, I checked them, and they contain a value.

You should be using the bindParam member,

$sth->bindParam(':calories', $calories, PDO::PARAM_INT);

...because

  1. it doesn't throw strange errors
  2. using execute(), all params are converted to string.

http://php.net/manual/en/pdostatement.execute.php

4 Comments

at your link (php.net/manual/en/pdostatement.execute.php) it says the following: "you must either: call PDOStatement::bindParam() to bind PHP variables..." OR "pass an array of input-only parameter values" So why then does the array part not work?
I think the error is somewhere else. For me, execute(array())-style works. I guess your original code is different from the one above.
OP should just accept yours then, if it'll make you feel any better. Btw, both methods work in PDO, so you're partly wrong.
@Fred-ii- You are correct that Dan is partly wrong, but his first thoughts were right. In my opinion this is a bad question. Certainly after the OP wrote Normally this are sessions, I checked them, and they contain a value. and at the end the values were empty.

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.