WHERE id = 3"...">

Mixing quotes in PHP

I have this script that gets my information from the DB

$result = mysqli_query($con,"SELECT * FROM <table> WHERE id = 3");

while($row = mysqli_fetch_array($result))
  {
  echo '<p>'.$row['name'].'</p>' . " " .'<p>' . $row['description'] . '</p>' . " " . '<p>' . $row['price'] . '</p>';
  echo "<br>";
  echo '<img src="http://www.example.com/images/'.$row['profile_pic'].'" alt="profilepic" />';
  echo '<img src="http://www.example.com/images/'.$row['pic1'].'" alt="pic1" />';
      ^^^                                                *********
      Here is the problem because I should have sigle quote at the beginning and at the end, but I still have it above ***.And I use double quotes for the src on the <img> so those are taken too.
  echo '<img src="http://www.example.com/images/'.$row['pic2'].'" alt="pic2" />';
  echo '<img src="http://www.example.com/images/'.$row['pic3'].'" alt="pic3" />';
  }

If I do it the way I expected, I need another set of numbers (possibly a triple:]]). How can I do it right?

+3
source share
4 answers

The source code that you have looks good. You can use the character \immediately before a single or double quote to escape, for example:

$variable = "my quote are \"like this\" ";

// output: my quote are "like this"

Similarly:

$variable = 'this is "how my quotes" don\'t look';

// output: this is "how my quotes" don't look

+3
source

You can use the syntax HEREDOCas follows. Variables can be used inline without any parentheses {}. Arrays must be wrapped in {}. Both types of variables can be embedded in a string.

while($row = mysqli_fetch_array($result)){

// "EOH" can be any string of your choosing, but you must close with it
$output = <<<EOH;
<p>{$row['name']}</p> <p>{$row['description']}</p> <p>{$row['price']}</p>
<br>
<img src="http://www.example.com/images/{$row['profile_pic']}" alt="profilepic" />
<img src="http://www.example.com/images/{$row['pic1']}" alt="pic1" />
<img src="http://www.example.com/images/{$row['pic2']}" alt="pic2" />
<img src="http://www.example.com/images/{$row['pic3']}" alt="pic3" />
EOH; // from above; MUST be aligned all the way left (don't intent or tab)

echo $output;
}
+2
source

PHP, $ - :

//Place any string in double quote
echo "<p> {$row['name']} </p> <p> {$row['description']} </p> <p> {$row['price']} </p>";

Rasmus

$ PHP

, $, , , .

, string, $ , , Perl .

+2

htmlspecialchars() $row, .. htmlspecialchars($row['profile_pic']).

mysqli_fetch_assoc() , mysqli_fetch_array().

0

All Articles