-1

Hello

I'm running a php function to get some user information from mysql using pdo. The problem is that i don't get the results i want. This is my php code:

<?php 
require_once('db_connect.php');

    function mila(){
        global $dbh;
        // Set dummy variables
        $required_id=1;
        // Select table with query
        $sth = $dbh->prepare("SELECT username,firstname FROM users WHERE :id");

        // Bind variables to your statement
        $sth->bindParam(':id', $required_id);

        // Flip the switch
        $sth->execute();

        foreach ($sth as $row){         
                    $username = $row['username'];
                    $firstname = $row['firstname'];
                    echo '<br/>' .$username.' '.$firstname;
        }
    }

    mila();
?>

Below is the database table i access:
users

And i get this on my browser:

user1 name1
user2 name2
user3 name3
user4 name4

I should get only one record, because of my sql query.
If i use "SELECT username,firstname FROM users WHERE id=1" as my query, i get only on record.

Edited by romanromeo89: minor mistake

5
Contributors
6
Replies
10
Views
4 Years
Discussion Span
Last Post by diafol
0

Try -

$sth = mysql_query("SELECT COUNT(username,firstname) FROM users WHERE id='".$required_id."'");

return (mysql_result($sth, 0) == 1) ? true : false;

This should return record 1 because you declared "$required_id=1".

Edited by AndreRet

4

@romanromeo89

I should get only one record, because of my sql query.

-

If I use "SELECT username,firstname FROM users WHERE id=1" as my query, i get only on record.

This is your query from your code above:

SELECT username,firstname FROM users WHERE :id

It should be:

SELECT username,firstname FROM users WHERE id=$id

Change this:

$sth->bindParam(':id', $required_id);

To this:

$sth->bindParam('$id', $required_id);

Edited by LastMitch: grammer

0

You can't use execute to get results. You need to call fetch after execute. See the examples here.

@LastMitch: :id is correct for parameter binding.

Edited by pritaeas

4

@LastMitch: :id is correct for parameter binding.

OK

0

Thank you all for replies,
I changed my code to this:

<?php
    require_once('db_connect.php');
    function mila(){
    global $dbh;
    $required_id=3;
    $sth = $dbh->prepare("SELECT username,firstname FROM users WHERE id = ?");
    $sth->execute(array($required_id));
    $user_info = $sth->fetchAll();
    $user_info['username'];
    //print_r($user_info);
    }
    mila();
?>

The result is:
Notice: Undefined index: username in C:...\db_get_user_id.php on line 13
if i print_r($user_info) i get
Array ( [0] => Array ( [username] => user3 [0] => user3 [firstname] => name3 [1] => name3 ) )
and i cant't understand how to get the values i want from there.

Edited by romanromeo89: incorrect format

1
$sth = $dbh->prepare("SELECT username,firstname FROM users WHERE id = :id");
$sth->execute(array(':id'=>$required_id));

If using execute. If using bindParam:

$sth = $dbh->prepare('SELECT username,firstname FROM users WHERE id = ?');
$sth->bindParam(1, $required_id, PDO::PARAM_INT);
$sth->execute();

However, AFAIK you can match like this too:

$sth = $dbh->prepare('SELECT username,firstname FROM users WHERE id = :id');
$sth->bindParam(':id', $required_id, PDO::PARAM_INT);
$sth->execute();

Or like this:

$sth = $dbh->prepare('SELECT username,firstname FROM users WHERE id = ?');
$sth->execute(array($required_id));

WHich is what you seem to have already.

//EDIT

I think I see - due to the fetchAll, you're returning an array, not a single record, so to get the first record:

echo $user_info[0]['username'];

Edited by diafol

Votes + Comments
Nice !
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.