help with php Parse error: syntax error, unexpected end of file in C: \ xampp \ htdocs \ log \ ajax \ conec.php on line 16

0
<?php
function connectDB(){
    $server = "localhost ";
    $user = "$usuario";
    $pass = "$passwoord";
    $bd = "nombre base de datos";
    global $link;
    if (!($link=mssqli_connect("local host","usuario","password"))){
        exit();
    }
    if (!mssqli_select_db("base de datos",$link)){
        exit();
    }
    return $link;
}
function ConnectBD(){;
?>

Hi, I have a problem with the php, it marks me an error and modifies the short tag but keeps bouncing the error help

    
asked by Mario 25.10.2018 в 18:00
source

2 answers

0

There is a syntax error here function ConnectBD(){; if you want to call the function just put ConnectBD();

    
answered by 25.10.2018 в 18:08
0

There are a few bad practices in your code and syntax errors.

We will try to correct them. I will comment on the code:

<?php
function connectDB(){
    $server = "localhost"; //sobraba un espacio
    $user = "usuario";     //tenía una $ dentro
    $pass = "passwoord";   //tenía una $ dentro y... ¿lleva dos oo?
    $bd = "nombre base de datos";  //poner datos reales en todo esto

    /*
       -no usaremos global, es una mala práctica, 
        ver al respecto: https://es.stackoverflow.com/q/29177/29967
       -la función se llama mysqli_connect, no mssqli_connect
       -usa las variables, que para algo las tienes
       -puedes seleccionar la bd de una vez en la conexión
    */


    if (!($link = mysqli_connect($server,$user,$pass,$bd))){
        echo "error de conexión: " . mysqli_connect_error() . PHP_EOL;
        exit();
    }

    return $link;
}

/*
    Dado que la función devuelve el objeto $link
    debes usar una asignación de variable cuando la llamas
*/
$link=ConnectBD();
//Usar $link para lo que quieras...
?>

TIP:

If you are starting with mysqli, I would recommend that you use the object-oriented style. It is clearer and more modern than the procedural style. For more details you can consult this question:

- Difference between new mysqli and mysqli_connect

    
answered by 25.10.2018 в 18:24