0

I have a problem and I hope someone can help me solve it.

I had to upgrade my VPS and from CentOS 6.x I switched to CentOS 7.5 So now I find PHP 7.0.1 and MySQL 5.7

The script I write below, worked great on PHP 5.6 and MariaDB and now goes wrong.

What can be a valid solution for this script to work properly? Thank you all.

$connessione = mysql_connect("localhost", "myuser", "mypassword");
mysql_select_db("mydb", $connessione);
$risultato = mysql_query("SELECT * FROM mytable", $connessione);
$num_righe = mysql_num_rows($risultato);

if ($num_righe == 0) {
    echo "There are no new products";
} else {

    $query ="SELECT * FROM mytable";

    $nuovi_prodotti= mysql_query($query, $connessione) or die(mysql_error());
    $row_nuovi_prodotti = mysql_fetch_assoc($nuovi_prodotti);

    $intes ="Riferimento|ID|nome|plain_description|iva inclusa|id_fornitore|brand|tax|picture1|picture2|picture3|model_size|model_quantity|barcode";
    $fornitore ="1";
    $iva = "22";

    $righe.="".$intes."\n";
    do {
        $righe.= "".$row_nuovi_prodotti['reference']."|".$row_nuovi_prodotti['id_categoria']."|".$row_nuovi_prodotti['descrizione']."|".$row_nuovi_prodotti['descrizione']."|".$row_nuovi_prodotti['prezzoacquisto']."|".$row_nuovi_prodotti['prezzovendita']."|".$fornitore."|".$row_nuovi_prodotti['marca']."|".$row_nuovi_prodotti['dispo']."|".$row_nuovi_prodotti['EAN']."|".$iva."|https://www.mysite.it/cat/".$row_nuovi_prodotti['reference'].".jpg\n";    
    }
    while ($row_nuovi_prodotti_unica= mysql_fetch_assoc($nuovi_prodotti_unica));
        $filename = "nuovi-prodotti-tagliaunica.csv";
        file_put_contents($filename, $righe);
        echo "New products found: $num_righe - file CSV ok \n";
    }
    mysql_close($connessione);
AntoineB
  • 4,535
  • 5
  • 28
  • 61
Roberto
  • 9
  • 1
  • 1
    Turn on error reporting, then you will see the errors. Also, the `mysql_*` functions were deprecated in PHP 5.6, and completely removed in PHP7+. – Tobias F. Nov 21 '18 at 09:05
  • 1
    To build on Tobias F.'s comment, `mysql_*` was replaced with `mysqli_*` - bear in mind that it's not a simple find/replace job to upgrade from `mysql_`. You should look at the documentation on php.net for more info – Scoots Nov 21 '18 at 09:07
  • 3
    Possible duplicate of [How to change mysql to mysqli?](https://stackoverflow.com/questions/1390607/how-to-change-mysql-to-mysqli) – ADyson Nov 21 '18 at 09:09
  • I have already opted for mysqli, but without any outcome. the requested page goes into error – Roberto Nov 21 '18 at 10:36
  • you're not using mysqli, although you're attempting to use mysqli syntax with the added database connection parameter. your mysql_* needs to be mysqli_* – Martin Nov 21 '18 at 12:00

1 Answers1

1

The problem occurs because the mysql_* syntax is deprecated in PHP 7.0 and forward. You will need to change to mysqli_* or PDO.

You mention in the comments that you "already opted for mysqli", but the syntax provided in your question is a mixed syntax of mysql and mysqli. mysqli takes the database connection as a parameter, while mysql does not.

Therefore, change your mysql_* to mysqli_*.

Examples from your question:

mysql_query($query, $connessione)

to

mysqli_query($query, $connessione)

etc.

If you're in doubt about what the syntax is for the different mysqli_* functions, then I would suggest looking it up. This thread is a good place to start if you're changing from mysql to mysqli.

Martin
  • 2,326
  • 1
  • 12
  • 22