views:

182

answers:

3

I have a mysql query that targets a single column in a single row

"SELECT some_col_name FROM table_name WHERE user=:user"

After I execute the statement $stmt->execute(); how do I get this single cell directly placed into a variable with no loops? In other words how to get

from $stmt->execute();

to $col_value = 100;

I tried the 2 below, but neither worked.. The column is number 4 in the original table, but I'm assuming since in my select statement I'm selecting it only, it should be 1 when I specify the parameter for fetchColumn.

$col_value = $stmt->fetchColumn();
$col_value = $stmt->fetchColumn(0);

As you can see, I'm trying to do it in as few lines as possible.

+1  A: 

Have you prepared the statement first? (Before $stmt->execute())

$stmt = $db->prepare("SELECT some_col_name FROM table_name WHERE user=:user");
True Soft
+2  A: 

Are you sure it's returning any rows? $stmt->fetchColumn() is correct way to fetch a single value, so either you probably didn't bind the :user parameter or it simply returned no rows.

reko_t
Thanks all, it turned out to be something with the database itself. I knew it should have been working
Chris
+2  A: 
$sql='SELECT some_col_name FROM table_name WHERE user=?';

$sth=$pdo_dbh->prepare($sql);
$data=array($user);

$sth->execute($data);

$result=$sth->fetchColumn();
Alex JL