0

I want to initialize a variable from an entry in an sql table; this entry is one I've queried for.

//Grab user id from users.sql
$quser = $db->query("
    SELECT *
        FROM users //in this table is a column named userident
        WHERE username = '$user' //this where conditional filters to a lone row         
");*/

I tried $var = $quser["ident"]; since this is how I would display table entries in a foreach() loop, but this doesn't seem to work.

There is only one ident entry per user, so when I filter the query with where, it results in one row. Why can't I grab the entry and place in a variable?

5
  • Disregard the end-comment at the end; I commented this segment of code out so I could work on other things in the meantime. Commented Jan 2, 2013 at 8:17
  • You say in this table is a column named userident but you use $quser["ident"] Commented Jan 2, 2013 at 8:18
  • Sorry, I'm renaming things as I go along. I'm trying to get the entry under ident in the lone row. Commented Jan 2, 2013 at 8:20
  • Is this pure PHP or CodeIgniter? Commented Jan 2, 2013 at 8:21
  • Pure PHP; I'm using PDO in conjunction, however. Commented Jan 2, 2013 at 8:23

1 Answer 1

2

This is what you need to do

  1. Execute the query
  2. Pick up the first row (as there will be only one row this'll work otherwise do it in a loop)
  3. Get the column from the row produced

Here's an example

    $quser = $db->query("SELECT * FROM users WHERE username = '$user'");
    $first_row=$quser->row();

    $reqdData=$first_row->ident; // Or whateber column you need
Sign up to request clarification or add additional context in comments.

2 Comments

i think above solution is perfect but one patch i would like to add $reqdData=($first_row->num_rows() > 0)?$first_row->ident :0;
With this I get a Call to undefined method PDOStatement::row() error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.