somebody could explain this code to me, I want to connect to a database, for a search engine in android studio, but it does not connect me

0
<?php

    $servername = "localhost";
    $username = "root";
    $password = "Engineringg96";
    $dbname = "tbl_fish";

    try 
    {
        $connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e)
    {
        die("OOPs something went wrong");
    }

?>


<?php

    $servername = "localhost";
    $username = "root";
    $password = "Engineringg96";
    $dbname = "tbl_fish";

    try 
    {
        $connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
    }
    catch(PDOException $e)
    {
        die("OOPs something went wrong");
    }

?>
    
asked by Francisco Miguel Eyu De Hernan 05.07.2017 в 21:36
source

2 answers

2

Since the code is not made by you, I recommend you to put the source in order to see if there is more documentation of that, on the other hand, I see that you have captured the exceptions but the error is not printed clearly:

 try 
{
    $connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    var_dump("Error: ".$e->getMessage());die;  // Muestra el error que se produce
}

So you can find out what error is obtained and correct it.

Here is the documentation for the class PDO

Luck.

    
answered by 05.07.2017 в 21:51
0

here an example

<?php
/**
 * Provee las constantes para conectarse a la base de datos
 * Mysql.
 */
define("HOSTNAME", "localhost");// Nombre del host
define("DATABASE", "tubd"); // Nombre de la base de datos
define("USERNAME", "root"); // Nombre del usuario
define("PASSWORD", "tupass"); // Nombre de la constraseña
$con = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATABASE) or die('Unable to Connect');
$tildes = $con->query("SET NAMES 'utf8'");
?>
    
answered by 05.07.2017 в 21:47