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!

2

2 Answers 2

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

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

6 Comments

It's also perfectly valid in non-XHTML markup to use single-quotes for attributes, and there's HEREDOC syntax.
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"]." ...
@MattiaRomagnoli are you sure you copied my examples properly and didn't forget a dot or a quote or something?
Yes i'm sure with the first it give the text without formatting
@MattiaRomagnoli that's strange. Does it output <div id="tfigura">something</div> like it should? And for the second one, it sounds like you're using double quotes (") around the echo statement
|
0

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

Comments

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.