Lyleyboy Posted March 7, 2013 Share Posted March 7, 2013 Hey all, I'm just starting to get on the PDO bandwagon with PHP. One thing I do a lot is to fetch the result of a query straight into a variable using list. Something like list($name) = mysql_fetch_row(mysql_query("SELECT name FROM names WHERE userID='1'")); Is there a similar thing I can do with PDO rather than having to loop through with a while or similar. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/275367-using-pdo/ Share on other sites More sharing options...
AyKay47 Posted March 7, 2013 Share Posted March 7, 2013 (edited) Look at example #1 on the bindColumn() PHP manual page. PDO::FetchBound mode is what you are after. Edited March 7, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275367-using-pdo/#findComment-1417207 Share on other sites More sharing options...
Lyleyboy Posted March 7, 2013 Author Share Posted March 7, 2013 May be me misreading but that seems to loop through a record set.What I'm looking for is a query that will only ever return one value which then dumps the value into a variable. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/275367-using-pdo/#findComment-1417208 Share on other sites More sharing options...
trq Posted March 7, 2013 Share Posted March 7, 2013 That is a terrible coding practice I'm sorry, and is something I would definitely try not to replicate again ever. What if mysql_query fails? Using your method you have absolutely no way of handling it at all properly. Quote Link to comment https://forums.phpfreaks.com/topic/275367-using-pdo/#findComment-1417209 Share on other sites More sharing options...
Lyleyboy Posted March 7, 2013 Author Share Posted March 7, 2013 Yeah I accept that and I always code round it, to ensure that the value is present and in the correct format. So what's the easiest way then? Do I still have to loop through, even though there will only be one record posted. I'm kind of hoping there will be an easier way. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/275367-using-pdo/#findComment-1417211 Share on other sites More sharing options...
AyKay47 Posted March 7, 2013 Share Posted March 7, 2013 You shouldn't accept it, that is horrible coding practice. Both the SQL statement and the mysql_query() return should be stored in variables and checked. But, to address you question, use that example without the looping logic if the query is to grab only 1 row. Quote Link to comment https://forums.phpfreaks.com/topic/275367-using-pdo/#findComment-1417215 Share on other sites More sharing options...
Lyleyboy Posted March 7, 2013 Author Share Posted March 7, 2013 Ok, thanks AyKay I'll give it a shot. Quote Link to comment https://forums.phpfreaks.com/topic/275367-using-pdo/#findComment-1417226 Share on other sites More sharing options...
DavidAM Posted March 7, 2013 Share Posted March 7, 2013 While I agree with @trq, let's just be clear about this looping. It is never required that you loop through a result set. If you know it returns a single row (or only want the first row), you can leave out any while or for processing and just fetch the first row. Well, that's the way it is/was in mysql, and I do it that way with mysqli. I have not played with PDO yet, but I see no reason this should not apply there as well. Quote Link to comment https://forums.phpfreaks.com/topic/275367-using-pdo/#findComment-1417243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.