views:

112

answers:

4

I have this SQL by a programmer:

$sql = "  
   INSERT INTO  
    `{$database}`.`table`  
   (  
    `my_id`,  
    `xType`,  
    `subType`,  
    `recordID`,  
    `textarea`  
   )  
   VALUES  
   (  
    {$my_id},  
    ?xType,  
    ?subType,  
    {$recordID},  
    ?areaText  
   ) ";  

My question is why is he using ? before values? How do I see what values are coming in? I did echo and it shows ?xType as ?xType. No values. What does ? stand for in SQL?

+9  A: 

That looks like a named parameter to me.

John Weldon
+1 But that seems to be specific to the .NET provider. I don't know any use for that style of parameter in PHP.
Bill Karwin
The syntax `?<var name>` is MySQL specific, not .NET specific.
John Weldon
The SQL standard would use `:<var name>`, and the chronic lack of use of these things by PHP programmers has been a blight on the good name of programmers for a long time (c.f. SQL injection attacks).
Donal Fellows
A: 

Looks to be a place holder to be parsed by some sort of parser. Possibly they are under some framework.

Sarfraz
+1  A: 

John Weldon is right in this case but i have seen sql like the below where ? means it is a placeholder for a value. The sql is accompanied by parameters equal to the amount of ?'s in the sql. In that case it means pull the value from the parameter and escape it.

INSERT INTO  
    `{$database}`.`table`  
   (  
    `my_id`,  
    `xType`,  
    `subType`,  
    `recordID`,  
    `textarea`  
   )  
   VALUES  
   (  
    ?,  
    ?,  
    ?,  
    ?,  
    ? 
   ) ";  
acidzombie24
Those are positional parameters. With named parameters, you (in effect) supply a map from names to values which the query pulls the values out of. That can be a lot clearer, especially in maintenance.
Donal Fellows
I know, i was just stating the above in case a user sees positional parameters, expects named parameters and becomes confused.
acidzombie24
A: 

Here's another crazy idea -- you're maintaining someone else's code, right? Does it actually work/run? If not, did he put in the question mark because he wasn't sure of that parameter or the name of the parameter? Sometimes I put in stuff that doesn't compile because I know it will force me to go back and figure it out later (so I don't forget).

eeeeaaii
-1. John Weldon posted the right answer. 95% of the time when the poster doesnt ask for info like this it is not needed and is noise/useless. I should know, i have lots of experience asking questions and getting answers.
acidzombie24
acidzombie24, I was just trying to help, thanks for being a dick and downvoting me. Oh, and if you have so much experience asking questions, how come your comment is badly written and doesn't make any sense? Ask for info - what? Generally when I have a question I have no idea what the answer is and I'm actually looking for someone to help me.Anyway, now I'm sure you'll flag me or something. Go ahead, I deserve it because I'm such a terrible member of the community. Thanks a lot.
eeeeaaii