Do what I do in MySQL but in PDO with PHP

0

I'm trying to learn PDO , I'm handling PHP, but with mysql_query I wanted to migrate to PDO , but it gives an error, I tried inserting but it does not work now, I want to try doing a query but I do not know someone would know why he will not let me consult.

The error that comes to me is:

  

Fatal error: Call to a member function query () on null in   C: \ xampp \ htdocs \ PDOJEFFERSON \ models \ addUser.php on line 5

Code:

function ConexionMySql(){
    $cadenaHost='mysql:Server=localhost;dbname=basebyjefferson';
    $nombre_user_db='root';
    $pass_user_db='';
    try {
        $conectar=new PDO($cadenaHost,$nombre_user_db,$pass_user_db);
        $conectar->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    } catch (Exception $e) {
        echo "ERROR: " . $e->getMessage();
    }
}
<?php
    include '../conexion.php';
    $cn=ConexionMySql();
    $sql= "SELECT * FROM tbluser";
    $query = $cn->query($sql);
    while($r=$query->fetch()){
        echo json_encode($r);
    }
?>
    
asked by Jefferson Vasconez 21.03.2017 в 22:59
source

4 answers

1

There are several errors to solve.

On the one hand, I think that the dsn mysql:Server= is wrong. It should be mysql:host= . With mysql:Server=localhost it gives a little bit because it is ignored by PDO and uses the default which is mysql:host=localhost , but if you want to connect to an external database using a specific ip, it would give you errors.

Second, the function must return the instance of the connection to continue working with it. If not, it returns a null value, and by logic, since the connection does not "exist", the script will launch the corresponding error.

<?php

function ConexionMySql()
{
    // $cadenaHost = 'mysql:Server=localhost;dbname=basebyjefferson';
    // cambiar por:
    $cadenaHost     = 'mysql:host=localhost;dbname=basebyjefferson';
    $nombre_user_db = 'root';
    $pass_user_db   = '';
    try {
        $conectar = new PDO( $cadenaHost, $nombre_user_db, $pass_user_db );
        $conectar->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    }
    catch( Exception $e ) {
        echo "ERROR: " . $e->getMessage();
    }

    return $conectar;
}


include dirname(__DIR__) . '/conexion.php';

$cn    = ConexionMySql();
$sql   = "SELECT * FROM tbluser";
$query = $cn->query( $sql );

while( $r = $query->fetch() ) {
    echo json_encode( $r );
}

PS: I have not tried it. If there are errors, a comment and we continue.

    
answered by 22.03.2017 / 12:35
source
1

Your problem is that your connection function is not returning an instance of the database.

What you need to add to your connection code is a return to get the instance to the database.

function ConexionMySql(){
   $cadenaHost='mysql:Server=localhost;dbname=basebyjefferson';
   $nombre_user_db='root';
   $pass_user_db='';
       try {
         $conectar=new PDO($cadenaHost,$nombre_user_db,$pass_user_db);
         $conectar->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
         return $conectar;
       } catch (Exception $e) {
          echo "ERROR: " . $e->getMessage();
       }
}
    
answered by 22.03.2017 в 01:20
0

Hello, your cycle is wrong. It should be like this:

foreach ($query as $querys)
{
    echo json_encode($r);    
}
    
answered by 21.03.2017 в 23:16
0

The problem is in your ConexionMySql function, it is not returning any value.

Try adding:

return $conectar;

Before finishing the function, it can be inside the try section.

On the other hand, in the code of agregarUser.php you should check that the value returned by the function ConexionMySql is valid before using it, for example:

if ($cn != null) {
    $query = $cn->query($sql);
} else {
    echo "¡Objeto PDO es nulo!";
}
    
answered by 22.03.2017 в 00:37