convert mysql_connect to mysqli_connect

3

hello I need to know how to convert this code to mysqli_connect

php

$link = mysql_connect('localhost','root','');
if(!$link){
    echo 'No se puede establecer conexion con el servidor: '. mysql_error();
}else{
    $base = mysql_select_db('proveedores',$link);
    if(!$base){
        echo 'no se encontro la base de datos: '.mysql_error();
    }else{
        $sql = "SELECT * FROM proveedores";
        $ejecuta_sentencia = mysql_query($sql);
        if(!$ejecuta_sentencia){
            echo'hay un error en la sentencia de sql: '.$sql;
        }else{
            $lista_paises = mysql_fetch_array($ejecuta_sentencia);
        }
    }
}

?>
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>contenido pedidos</title>
    </head>

    <body>
        <h1>Pedidos</h1>
        <table>
            <tr>
                <th>fecha</th>
                <th>hora</th>
                <?php
                for($i=0; $i<$lista_paises; $i++){
                    echo"<tr>";
                    echo"<td>";
                    echo$lista_paises['fecha'];
                    echo"</td>";
                    echo"<td>";
                    echo$lista_paises['hora'];
                    echo"</td>";
                    echo"</tr>";
                  $lista_paises = mysql_fetch_array($ejecuta_sentencia);  
                }
                ?>
            </tr>
        </table>
    </body>

    </html>
    
asked by Julio César de León Sandoval 13.09.2018 в 19:46
source

2 answers

1

with this code mysqli you will be able to do what you need, I certainly recommend leaving msql since it is insecure, it is considered obsolete and deleted in version 7.1 of php.

the sintaxys is almost the same only some parameters change see this example

$link =new mysqli('localhost','root','','your_db');
    if(!$link){
        echo 'No se puede establecer conexion con el servidor: '. mysqli_error();

        }else{
            $sql = "SELECT * FROM proveedores";
            $ejecuta_sentencia = mysqli_query($link,$sql);
            if(!$ejecuta_sentencia){
                echo'hay un error en la sentencia de sql: '.$sql;
            }else{
                $lista_paises = mysqli_fetch_array($ejecuta_sentencia);
            }
        }
    }

As you can see the syntax is almost the same here I leave this article you want to motivate you and help you mysqli

    
answered by 13.09.2018 в 19:59
0

This works for me:

$host='localhost';
$user='root';
$password='root';
$db='db_name'
$con = new mysqli($host,$user,$password,$db);
    
answered by 13.09.2018 в 20:22