-3

i have a problem with the print of data from DB. I have a database called "Project" and a table: "Prenotazione".

I want print the elements from this table. so i use this code:

<html>
    <head>
        <title>Prenotazioni Richieste</title>
    </head>
    <body>
        <table> 
        <?php 
            $host="localhost"; 
            $uname="root"; 
            $psw="123456789"; 
            $nomedb="Project"; 
            mysql_connect($host,$uname,$psw);
            mysql_select_db($nomedb);
            $q=mysql_query("SELECT * FROM Prenotazione");
            $r=mysql_fetch_assoc($q);

            echo $r[nome];

        ?> 

        </table>
    </body>
</html>

But i have only a white page without any elements. How can i solve? And why this code doesn't show nothing? Thank you.

ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
Kepz23
  • 9
  • 6
  • usually, a blank page means that you have an error which is not displayed. have a look at your logs and/or enable display errors into your php config. for example, you have a syntax error on the last php line : the quotes are missing around `nome` : `echo $r['nome'];` – ᴄʀᴏᴢᴇᴛ Sep 14 '18 at 08:59
  • 3
    *"Warning: This extension [mysql] was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. "* http://php.net/en/mysql_connect – hellow Sep 14 '18 at 08:59
  • 1
    echo $r['nome'];turn on your error logging and you will see some errors – FatFreddy Sep 14 '18 at 08:59
  • in my error log there isn't nothing :/ – Kepz23 Sep 14 '18 at 09:02
  • 1
    Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – ᴄʀᴏᴢᴇᴛ Sep 14 '18 at 09:02
  • @ᴄʀᴏᴢᴇᴛ i don't think so – Kepz23 Sep 14 '18 at 09:04
  • Try to var_dump($r); but please don't use Mysql function, read this [http://php.net/manual/en/book.mysqli.php](http://php.net/manual/en/book.mysqli.php) – Sfili_81 Sep 14 '18 at 09:05
  • I do think so. Please learn how to solve (syntax) errors on your own. [Turn errors on](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display/41641608) and look at the errors that will appear. – hellow Sep 14 '18 at 09:05
  • @Kepz23 did you try to correct your echo as suggested by FatFreddy and me ? – ᴄʀᴏᴢᴇᴛ Sep 14 '18 at 09:08
  • @Sfili_81 I use var_dump($r) but nothing appears – Kepz23 Sep 14 '18 at 09:09
  • @ᴄʀᴏᴢᴇᴛ Yes! It's strange, because it is not such a complicated code – Kepz23 Sep 14 '18 at 09:11
  • @hellow I used also a connection.php that i use to put data into database and it works – Kepz23 Sep 14 '18 at 09:11
  • What error do you get? Try to turn errors on – Sfili_81 Sep 14 '18 at 09:16

1 Answers1

1

You have a syntax error in line:

echo $r[nome];

To access data of specific column, you should use single quotes, otherwise it will be defined as a constant.

It should be like this:

echo $r['nome'];

I also advice you to use mysqli as mysql is deprecated, here is a simple example for what you want to achieve using mysqli.

mysqli_connect function receiving 4 arguments:

$con = mysqli_connect("localhost","root","123456789","Project");
$sql = "SELECT * FROM Prenotazione";
$result = mysqli_query($con,$sql);

Here you can fetch $result into an associative array.

$row = mysqli_fetch_assoc($result);

Then you can access specific column in the database like this:

echo $row['nome'];