Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


SELECT single record

SELECT single record

SELECT single record

I'm using SQLite and I have the following to get a single record:


$query = 'SELECT * FROM talks WHERE id=:id;';
$stmt = $db->prepare($query);
$stmt->bindValue(':id', $id);
$result = $stmt->fetchAll();
foreach ( $result as $row ) {
  $address = $row['address'];
  $num_views = $row['num_views'];
I'm not sure this is right but I'm not sure what I should have. Can anyone please help?

I'm not a number, I'm a free man

RE: SELECT single record

Okay. Dimwit that I am, this seems to do the job:

CODE -->

$query = 'SELECT * FROM talks WHERE id=:id;';
$stmt  = $db->prepare($query);
$stmt->bindValue(':id', $id);
$result = $stmt->fetch();
$address = $result['address'];
$num_views = $result['num_views']; 

I'm not a number, I'm a free man

RE: SELECT single record

Your original code is best if you limited the query.



SELECT * FROM talks WHERE id=$id LIMIT 1; 

Your "answer" still has the potential to select a lot of extra data but you're making PHP only process the first part of it. It would be more efficient to query only the data you need and then run your original PHP.

RE: SELECT single record

Thanks for your reply. Is using fethcAll() and a foreach loop really more efficient than fetch() and no foreach loop? It sounds counter-intuitive.

I'm not a number, I'm a free man

RE: SELECT single record

My wording was not ideal. I think the "efficiency" I was thinking was your own...in not having to bother with writing alternative PHP to handle only one record.

Once you limit your query, you can process with either method in PHP. The performance difference between these two PHP options should be negligible with just one record.

RE: SELECT single record

Many thanks for explaining.

I'm not a number, I'm a free man

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close