Can DIE be done in mysql connection? To show if I connect or not

0

I want to make a testing website. Connection PHP I have the following code:

<?php
$conexion = @mysqli_connect("localhost", "usuario", "contrasenia", "database");
if (!$conexion)
{
    printf("Error de conexión.", mysqli_connect_error());
}
?>

I would like instead of print not more information, and only show:

Error de conexión

Is there any way I can change this so that it is not prinf if not die "Error de conexion"; ?

To be a bit more specific I need this file exito_conexion_text.php to show this:

<?PHP
INCLUDE 'testeo.PHP';
?>
<html>
<h2>
EXITO al conectar CON EL SERVIDOR
</h2>
</html>

If you connect ONLY show this when connecting but if not, say ERROR NO CONNECT. To be more clear is to test if there is a connection or not.

    
asked by Juan Carlos Villamizar Alvarez 24.12.2018 в 02:19
source

2 answers

1

As an alternative, you can always use a try ... catch. It would be something like this:

try {
    $conexion = @mysqli_connect($servidor, $usuario, $password, $bd);
    echo "Conexión efectuada.";
} catch (Exception $e) {
    echo "La conexión no se pudo efectuar.";
    exit();
}

The $e object, which contains the exception if it has occurred, you can use it during your tests to see what has failed, or simply leave it there, unused. Do not ask for bread.

Good. I guess with "I'm positive" you mean that you get the message "Connection made." Depending on the PHP version and configuration, an error in mysqli_connect can be interpreted as a simple warning, which, by itself, does not throw an exception. Although I have taken this code from a website I had stored, it is true that it was quite old.

You can do two things: 1) Below the connection line, force the exception if you have not connected, like this:

try {
    $conexion = @mysqli_connect($servidor, $usuario, $password, $bd);
    if (!$conexion) throw new Exception();
    echo "Conexión efectuada.";
} catch (Exception $e) {
    echo "La conexión no se pudo efectuar.";
    exit();
}

It's a fix, but it works to force the exception, even if only a warning is thrown.

The other alternative, which I like more, is to abandon the use of the mysqli extension and use PDO directly, like this:

try {
    $conexion = new PDO('mysql:host='.$servidor.';dbname='.$bd.';charset=UTF8', $user, $pw);
    echo "Conexión efectuada.";
} catch (Exception $e) {
    echo "La conexión no se pudo efectuar.";
    exit();
}

Working with PDO databases facilitates the performance of a DB and allows you to write a cleaner and maintainable code.

These last two options are tested with PHP 7.1.3 and running, with the standard configuration that mounts xampp by default. I hope you are useful.

    
answered by 24.12.2018 / 17:05
source
1

Sure! It would be like this:

$conexion = @mysqli_connect( $servidor, $usuario, $password, $bd ) 
                                     or die ("Error de conexion");

The connection is made or stops to run the page and will only show the error message of die .

Another way is this, but it does exactly the same thing:

<?php
$conexion = @mysqli_connect('localhost', 'usuario', 'password', 'bd'); //conexión a la bd.

if (!$conexion) {  //condición en caso de que no se conecte la bd.
    die('Error de conexión: ' . mysqli_connect_error()); //si no se conecta muestra "Error de conexión: " y el error en la conexión.
}
?>
    
answered by 24.12.2018 в 02:51