show field of a php query with PDO

1

I am trying to print a user that is registered in my database as a test to implement it in some data later, I have a database with a user table where after filling out a form (user and password) they are sent to the same page php_self for which I use this conditional and print that user (entered in the form) and compared in the db, but I do not throw the result, and probe with return and with this way

if ($_SERVER['REQUEST_METHOD']=='POST') {

    $usuariodb = $_POST['usuario'];

    $conexion_usr = new PDO('mysql:host=localhost;dbname=ws_blog;charset=UTF8','root','');
    $sql = "SELECT * FROM user_blog WHERE name ='".strtolower($usuariodb)."'";
    $result = $conexion_usr->query($sql);
    $usr_name = $result->fetchAll();
    foreach ($usr_name as $names) {
        echo $names;
    }

}

and this one

if ($_SERVER['REQUEST_METHOD']=='POST') {

    $usuariodb = $_POST['usuario'];

    $conexion_usr = new PDO('mysql:host=localhost;dbname=ws_blog;charset=UTF8','root','');
    $sql = "SELECT * FROM user_blog WHERE name ='".strtolower($usuariodb)."'";
    $result = $conexion_usr->query($sql);
    return $result
    }

}
    
asked by matteo 07.11.2016 в 03:51
source

1 answer

0

This was the solution I found to my problem:

if ($_SERVER['REQUEST_METHOD']=='POST') {

    $usuariodb = $_POST['usuario'];

    $conexion_usr = new PDO('mysql:host=localhost;dbname=ws_blog;charset=UTF8','root','');
    $sql = "SELECT name FROM user_blog WHERE name ='".strtolower($usuariodb)."'";
    $result = $conexion_usr->query($sql);
    $usr_name = $result->fetchColumn();
    }
    
answered by 07.11.2016 в 04:30