1

Hi i'm creating a website with database in mysql, php and html. The problem there is when i want to explain my datas of the database's table with php and my sql The code that i write is this:

<?php

$link = mysql_connect("localhost","user","psw");
mysql_select_db("my_db",$link);

$result = mysql_query("SELECT * FROM Table ORDER BY Data DESC LIMIT 20",$link);

while($riga = mysql_fetch_array($result))
{
    echo '  <ul id="contenitore">
                <li id="tfigura">$riga["Testo"]</li>
                <li id="efigura">$riga["Eta"]</li>
                <li id="sfigura">$riga["Sesso"]</li>
                <li id="dfigura">$riga["Data"]</li>
            </ul>
          ';

}
?>

But it return not the form (setted with css) but this:

$riga["Testo"]
$riga["Eta"]
$riga["Sesso"]
$riga["Data"]
$riga["Testo"]
$riga["Eta"]
$riga["Sesso"]
$riga["Data"]
...

not the datas of this position.. I try also with this code:

<?php

$link = mysql_connect("localhost","user","psw");
mysql_select_db("my_db",$link);

$result = mysql_query("SELECT * FROM Table ORDER BY Data DESC LIMIT 20",$link);

while($riga = mysql_fetch_array($result))
{
    echo "
            <html>
            <body>

            <div id="contenitore">
                <div id="tfigura">
                    ".$riga["Testo"]."
                </div>


                <div id="efigura">
                    ".$riga["Eta"]."
                </div>


                <div id="sfigura">
                    ".$riga["Sesso"}."
                </div>


                <div id="dfigura">
                    ".$riga["Data"]."
                </div>
            </div>

            </body>
            </html>
         ";
}
?>

but return me the error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /membri/figuralo/pagine/figure.php on line 14 (where is the line ).

How can i solve? thank you!

Mattia
  • 199
  • 1
  • 1
  • 12
  • 1
    [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Sep 14 '14 at 14:35
  • You have errors either way (either in approach or in syntax). [Please read on string interpolation](http://php.net/manual/en/language.types.string.php). – Jared Farrish Sep 14 '14 at 14:35

2 Answers2

1

When you use double quotes around a string, you need to escape double quotes inside the string like this: \". You can also just use single quotes around the string. And, as Jared Farrish said, it's also possible to use the Heredoc syntax

//escaping the double quotes
echo "
    <div id=\"tfigura\">
      " . $riga["Testo"] . "
    </div>
";

//using single quotes
echo '
    <div id="tfigura">
      ' . $riga["Testo"] . '
    </div>
';

//using the heredoc syntax
echo <<<HTML
    <div id="tfigura">
      {$riga["Testo"]}
    </div>
HTML;

I suggest you read up on how to properly use quotes: http://php.net/manual/en/language.types.string.php

Jonan
  • 2,485
  • 3
  • 24
  • 42
  • It's also perfectly valid in non-XHTML markup to use single-quotes for attributes, and there's HEREDOC syntax. – Jared Farrish Sep 14 '14 at 14:37
  • With the first solution not load the css format With the secondo not work:it give me this: ".$riga["Testo"]." ".$riga["Eta"]." ".$riga["Sesso"]." ".$riga["Data"]." ".$riga["Testo"]." ".$riga["Eta"]." ".$riga["Sesso"]." ".$riga["Data"]." ... – Mattia Sep 14 '14 at 14:37
  • @MattiaRomagnoli are you sure you copied my examples properly and didn't forget a dot or a quote or something? – Jonan Sep 14 '14 at 14:41
  • Yes i'm sure with the first it give the text without formatting – Mattia Sep 14 '14 at 14:42
  • @MattiaRomagnoli that's strange. Does it output `
    something
    ` like it should? And for the second one, it sounds like you're using double quotes (`"`) around the echo statement
    – Jonan Sep 14 '14 at 14:45
  • Ok i've solved i use your first method...but i forgeot to import the css style with (and use your metod also with this line in Now it work thank you very much....a question...what is the function of this "\"? – Mattia Sep 14 '14 at 14:46
0

In addition to escaping the quotes \", you wrote $riga[sesso}. It should be $riga[sesso]