0

I'm working whit a simple site on a PHP server that print a series of data in a table but when i open it, instead of printing the data it prints {$row['tempLog']} etc. Where is the problem :\

connect.php

<?php

function Connection(){


    $connection = new mysqli("localhost", "root", "EasyMon", "test");

    if ($connection->connect_errno) 
    {
        echo "Failed to connect to MySQL: (" . $connection->connect_errno . ") " . $connection->connect_error;
    }
    echo $connection->host_info . "\n";

    return $connection;
    }
?>

Here the index.php site:

    <?php

    include("connect.php");     

    $link=Connection();

    $result=mysqli_query($link,"SELECT * FROM `Tabella1` ORDER BY `tempLog`");

?>
<html>
    <head>
        <title>Dati</title>
    </head>
    <body>
        <h1>Potenziometro e sensore</h1>

        <table border="1" cellspacing="1" cellpadding="1">
        <tr>
            <td>&nbsp;Timestamp&nbsp;</td>
            <td>&nbsp;Temperature&nbsp;</td>
            <td>&nbsp;Moisture&nbsp;</td>
        </tr>
        <?php
            if($result!==FALSE){
                while($row = mysqli_fetch_array($result)) { 
                    echo ("<tr><td>{$row['tempLog']}</td><td>{$row['potenza']}</td><td>{$row['sensore']}</td></tr>");
                }
                mysqli_free_result($result);
                mysqli_close($link);
            }
        ?>
        </table>
    </body>
</html>         
  • First silly question: is your file saved as .php? Second silly question: is your file hosted on a PHP web server? – Stefano Zanini Apr 27 '17 at 08:09
  • You mix `mysql_*` and `mysqli_*` (`mysql_fetch_array($result`) – Jens Apr 27 '17 at 08:10
  • ..and wrong order of arguments given in `mysqli_query()`, and we don't know what `new Connection()` does, nor whats inside connect.php. – Qirel Apr 27 '17 at 08:11
  • Converting `mysql_` to `mysqli_` is not as simple as adding an `i` to the function calls – RiggsFolly Apr 27 '17 at 08:12
  • 1
    [Read the Manual](http://php.net/manual/en/class.mysqli.php) Its written in many languages – RiggsFolly Apr 27 '17 at 08:13
  • I'm voting to close this question as off-topic because this is just a bad attempt to convert `mysql_` calls to `mysqli_` calls – RiggsFolly Apr 27 '17 at 08:15
  • Lol i miswrite everithing, i'm gonna edit – Alfredo Bressi Apr 27 '17 at 08:17
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Apr 27 '17 at 08:30
  • My server (virtual machine) use PHP 5.6.30 so it shuld still work, shouldn't it? – Alfredo Bressi Apr 27 '17 at 08:35
  • Yes, but the need to upgrade still applies, because it wont when your host want to upgrade you to PHP7 – RiggsFolly Apr 27 '17 at 08:40
  • I've just noticed that MySQLi commands are really similar to MySQL : | – Alfredo Bressi Apr 27 '17 at 08:59
  • But my actual problem now is that the site doesn't print the data, it just print the table labels and i don't know why – Alfredo Bressi Apr 27 '17 at 09:15
  • just test it without using string interpolation, in case that's the problem, e.g. `echo ("{".$row['tempLog']."}` etc. Obviously we don't know whether you've got the field names right either - bear in mind that they're case-sensitive, for instance. – ADyson Apr 27 '17 at 13:01
  • Field names are right, i think i can just use **pritnf**, something like **printf("  %s  %s   %s  ",$row["tempLog"], $row["potenza"], $row["sensore"]);** to incapsulate thi prints in the table – Alfredo Bressi Apr 28 '17 at 08:33

0 Answers0