\' and query

hello,

I don’t understand the logic behind having to put the \ in this line.

<?php $reponse = $bdd->query('SELECT nom FROM jeux_video WHERE possesseur=\'Patrick\''); ?>

I don’t get why it’s needed. if someone could enlight me it would be great.

<?php $bdd = new PDO('mysql:host=localhost;dbname=test', 'root', ''); $reponse = $bdd->query('SELECT nom FROM jeux_video WHERE possesseur=\'Patrick\''); ?>

I got also a more conceptual question of something I don’t quiet understand. I get that there are variables, fonctions and array’s. But

$reponse = $bdd->query(‘SELECT nom FROM jeux_video’);
What is this ? I know what it does but is it a variable ? If so, I believe a variable store something in memory.

So there is the variable $response which equals the variable $bdd on which we apply the fonction query wich select everything in the table jeux_video? I’m kinda lost I think there is something I didn’t understand. especially that where i’m reading this the" ->" falls from nowhere without good explanation.

thanks for the help

In order:

PHP has something called string delimiters. There are four of them: HEREDOC (<<<EOF), which is rarely used. ". ‘. NOWDOC (<<<’), which is even rarer.

Each PHP string delimiter is closed by itself and itself only (this used to be false in older versions of PHP, where " would close '). To prevent such a character from closing an already-open set, you use the backslash operator (). The backslash literally tells “treat the next character literally” to PHP, which is the major way to get a single quote in a string, amongst other things.

Backslashes are not additive. \’ does not mean “treat ’ literally for 2 operations” - it means (“that second blackslash is a literal”).

In your case, you’re opening your string with '. As a result, you will need to escape every single ’ that comes along to avoid them from being interpreted by PHP as your string having ended. That is the logic.

For your second question:

$reponse = $bdd->query('SELECT nom FROM jeux_video'); 

is a variable assignment. You are assigning the return value of $bdd->query, which is a method of a class, to $reponse. This then allows you to fetch back the value returned later on - it stores it in memory. The -> string token is quite literally “in the class, dynamically”. In this case, you are going to use the function query present in the class instance $bdd (which is a PDO object and the function is available here: http://php.net/manual/fr/pdo.query.php. J’en ai profité pour changer l’addresse afin que tu aies la page en francais :smiley: ). That’s all.

The other one to watch out for is :: (“in the class, statically”). Read up on classes if you’re curious.

thanks a lot, that makes a lot more sens now.

Unfortunately I still don’t understand my second question. Well I understand the question but not the answer actually :D. I did read the manual you gave me but that makes no sens for me.

[php]<?php
$bdd = new PDO(‘mysql:host=localhost;dbname=test’, ‘root’, ‘’);

$reponse = $bdd->query(‘SELECT nom FROM jeux_video WHERE possesseur=‘Patrick’’);
?>[/php]

so you say that $response equals the value of the function applied to the other variable $bdd if I’m correct ?

Sorry if I’m a little slow but there seems to be something I’m missing. Note that I didn’t learn Php oriented object yet (I’ve just heard the name and it could read it on your link) that’s maybe why I’m lost.

No problem.

Unfortunately I still don't understand my second question. Well I understand the question but not the answer actually :D. I did read the manual you gave me but that makes no sens for me.

[php]<?php
$bdd = new PDO(‘mysql:host=localhost;dbname=test’, ‘root’, ‘’);

$reponse = $bdd->query(‘SELECT nom FROM jeux_video WHERE possesseur=‘Patrick’’);
?>[/php]

so you say that $response equals the value of the function applied to the other variable $bdd if I’m correct ?

Nope, I’m saying that $bdd is an object that contains methods - and query() is one of them. $reponse contains the result of $bdd->query(), where that function itself can tap into object-specific variables present within itself.

It is exactly equivalent to this:
[php]
class Foo {
function Bar() { return 42; }
}
$r = new Foo();
echo $r->Bar(); // will echo 42
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service