Conversion to msqli

Hello, I have a PHP5 code (hosted with Free) that I need to convert to PHP8 because it no longer works. I’m starting with the database connection code: Original: <link />
$link = mysql_connect ($host,$user,$pass) or die (); mysql_select_db($db);
They changed it to:
$link = mysqli_connect($host, $user, $pass, $db); // Vérification de la connexion if (!$link) { die("Échec de la connexion : " . mysqli_connect_error()); }
I’m getting the error: Fatal error: Call to undefined function: mysqli_connect() Why ?? And how to fix it? - Thanks

15 answers

  • Votes
  • Date
  • Descending date
  1. Moderator

    Hello,

    Have you checked the active PHP version on your site?

    On one of mine I’m at PHP Version 5.6.40, not 8.5, so it depends on the servers. And also on the .htaccess where on Free you can select the version you chose.

    In the .htaccess I have an old parameter
    # Enable php5 on free.fr (php4 by default)
    php 5

    On a site without the .htaccess I’m at PHP Version 4.4.3-dev. And I tried to set php 8, I stay at 5.

    To display the version put a file like info.php containing <?php phpinfo(); ?> and run it manually.

    Migration might be in progress. Free has always been very poor at communication. I have 2 sites that are more accessible, neither via FTP nor with a browser.


    1. The PHP version of my site on Free 4.4.3.

      But I suppose the problem comes from Free. I tested my site on another hosting provider, it works!!!

      1. Moderator

        We return to the starting questions. Is the document physically uploaded on your site? If so, what is the SQL order error.


        1. No. The document is not saved on the site, and no error is raised!

          And now nothing else displays except the HTML. Even the links no longer work!

          1. I get the error:

            Fatal error: Cannot instantiate non-existent class: mysqli in /var/www/sdb/b/a/genfamille/test_bd.php on line 6

            Line 6 corresponds to

            $db_con = new mysqli("$host","$user","$pass","$db");

            If I understand correctly, it's that mysqli is not enabled!

            1. Moderator

              in PHP 4 it should not be mysqli, even the PDO access method only works with PHP 5

              here's a test done in PHP 4

              <?php // 1. Definition of connection variables $serveur = "sql.free.fr"; $utilisateur = "xxxxx"; // Identifier of your Free account (e.g.: myusername) $mot_de_passe = "yyyy"; // Your database password $base_de_donnees = "xxxx"; // At Free, the DB name is identical to the identifier // 2. Connection to the MySQL server $connexion = mysql_connect($serveur, $utilisateur, $mot_de_passe); // Check the connection if (!$connexion) { die("Echec de la connexion au serveur MySQL. Code erreur : " . mysql_errno() . " - Message : " . mysql_error()); } // 3. Selection of the database $selection_base = mysql_select_db($base_de_donnees, $connexion); // Check the database selection if (!$selection_base) { die("Impossible de selectionner la base de donnees. Code erreur : " . mysql_errno() . " - Message : " . mysql_error()); } echo "Connexion reussie a la base de donnees Free !"; // 4. Test of an SQL query $sql = "SELECT * FROM article"; $result = mysql_query($sql, $connexion); if (!$result) { // The query failed, display the MySQL error die('Erreur de requete SQL : ' . mysql_error()); } else { // Data processing if all goes well while ($row = mysql_fetch_assoc($result)) { echo $row['nom'] . '<br />'; } } // 5. Closing the connection (optional at end of script) mysql_close($connexion); ?> 

              we can generate an error, which in SQL is very clear, by replacing the table name article with a non-existent table.

          2. Thank you, JPP. I tested your code, and here is the feedback:

            MySQL server connection failed. Error code: - Message:

            1. Moderator

              I ran tests because your message does not contain an error label whereas it should.

              If I enter an incorrect password in PHP4 I get an error:
              Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mon_site'@'172.20.244.169' (using password: YES) in /var/www/sda/3/8/mon_site/accbddv4.php on line 9
              Failed to connect to MySQL server. Error code: 1045 - Message: Access denied for user 'mon_site'@'172.20.244.169' (using password: YES)

              Singularly, the other 3 parameters seem to be useless, it does not trigger an error and I connect; the site URL should be sufficient, with a correct password, because I connect with the following settings:

              $serveur = "ccm";
              $utilisateur = "ccm"; // Identifier of your Free account (e.g.: myusername)
              $mot_de_passe = "YYYYY"; // Your database password
              $base_de_donnees = "ccm"; // On Free, the database name is the same as the identifier

              Do you have a access issue to the BDD. Do you have access via phpMyAdmin on free.fr?

              There is now since June a new DB kernel on Free, MariaDB. I have not found where to request migration, but following your issues, have you not performed such a migration request for the DB?

              I also read that during server and DB maintenance, Free noticed that some users exceeded the allowed size quotas, which could lead to blockages?


              1. Thank you, pee! / Thank you, JP!

                Yes, I have access to my database via phpMyAdmin!

                And for the free accounts, Free doesn’t want to intervene..................

                1. It is not necessary to enable mysqli. It is enough to upgrade PHP from version 5.0 to version 8.0. The mysqli commands will work automatically.

                  1. Yes, but we still have to go through all the codes, one by one, line by line to modify them ..........

                    1. Moderator

                      Is your database still down on Free in PHP4?

                      Could you just enable PHP 8.5 in the htaccess and test the code of <9> to see if it’s better with mysqli.

                    2. @jee pee

                      Yes! Still in PHP4.

                      How to enable PHP8.5 in the htaccess and test the code of <9> to see if it’s better in mysqli.

                  2. Try this

                    $db_con = new mysqli("localhost","my_user","my_password","my_db"); // Check connection if ($db_con -> connect_errno) { echo "Failed: " . $db_con -> connect_error; exit(); }