简体   繁体   中英

No database connection with no errors

I have Ubuntu 16.10 x86_64 x86_64. I installed LAMP to program in PHP and to create databases. In my php program I want to connect to my local database for creating a table ( in HTML ) with the data of any row of the table. The problem is that when I open the php file( localhost/file.php ) through firefox ,the browser doesn't charge anything. If thare were been an error during the connection with the database, It would have printed something in the browser. Here the code:

<!DOCTYPE html>
<html>

<head><title> SQL & PHP </title></head>

<body>

<?php


$db = mysql_connect("localhost", "root", "password")
        or die ("Non riesco a creare la connessione");


mysql_select_db("scuola")
         or die ("Non trovo il DB");

$sql = "SELECT id_utente, nome_utente, password_utente, conta_pres FROM  utenti WHERE conta_pres <> 0";

$ris = mysql_query($sql) or die ("Query fallita!");

echo "<TABLE><TR><TH>ID utente <TH> Nome utente <TH>Password<TH>Contatore visite</TR>";

while ($riga= mysql_fetch_array($ris))
{
        echo ("<TR>");
        echo "<TD>"  . $riga["id_utente"];
        echo "<TD>"  . $riga["nome_utente"];
        echo "<TD>"  . $riga["password_utente"];
        echo "<TD>"  . $riga["conta_pres"];

}

mysql_close();
?>
</body>

</html>   

I checked the syntax (using a website) of the code and thare aren't problems,even because I copied this one by a book. I read that mysql_connect has been deprecated, so I replaced it with new mysqli_connect but the error still remains: white page. I tried to put 2 echo, one before the connecting function and one after that. Only the first echo is printed on the screen. I tried to type in the terminal sudo apt-get install php5-mysql but there is an error: The "php5-mysql" packet has not run to install

Can someone help me, please?

First of all use mysqli instead of mysql .

I think I have found the problem. When you call mysqli_select_db , it expects 2 parameters and you only specified one. Even though you have set the $db database connection, you need to specify which database you want to select the database name from.

So mysqli_select_db($db, "scuola") should do the trick.

And at the bottom close the connection specifying which connection to close. In your case it is: mysqli_close($db);

<!DOCTYPE html>
<html>

    <head><title> SQL & PHP </title></head>

    <body>

        <?php


        $db = mysqli_connect("localhost", "root", "password")
            or die ("Non riesco a creare la connessione");


        mysqli_select_db($db, "scuola") // see this line
            or die ("Non trovo il DB");

        $sql = "SELECT id_utente, nome_utente, password_utente, conta_pres FROM  utenti WHERE conta_pres <> 0";

        $ris = mysql_query($sql) or die ("Query fallita!");

        echo "<TABLE><TR><TH>ID utente <TH> Nome utente <TH>Password<TH>Contatore visite</TR>";

        while ($riga= mysql_fetch_array($ris))
        {
            echo ("<TR>");
            echo "<TD>"  . $riga["id_utente"];
            echo "<TD>"  . $riga["nome_utente"];
            echo "<TD>"  . $riga["password_utente"];
            echo "<TD>"  . $riga["conta_pres"];

        }

        mysqli_close($db); // see this line
        ?>
    </body>

</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM