rubyarat2010 Posted July 7, 2012 Share Posted July 7, 2012 How would i get a specific collums and row of an sql table. For example lets say i want the fifth row of author so author Jim Frank Drake Tim Chris So it would get chris Then i would like to assighn a variable that value. How would this be done? Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/ Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2012 Share Posted July 7, 2012 "Fifth row" has no real significance without some criteria that would make it fifth. In other words, fifth when ordered by what? Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359968 Share on other sites More sharing options...
xyph Posted July 7, 2012 Share Posted July 7, 2012 Check out the LIMIT clause Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359969 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 Well if you got the last six like this the fifth one of that SELECT title, imagelink FROM posts1 ORDER BY id DESC LIMIT 6 Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359980 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2012 Share Posted July 8, 2012 You could use a derived table, I suppose. SELECT title, imagelinks FROM (SELECT id, title, imagelinks FROM posts1 ORDER BY id DESC LIMIT 5) AS t ORDER BY id ASC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359982 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2012 Share Posted July 8, 2012 You could also run a regular select query, with order by id desc and limit 5, dump the results in an array in php, and just take the end element of the array, or assign the value of array_pop to another variable. I don't know for certain which way is more efficient, but I suspect the SQL-only way would be. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359985 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 How would you "dump" the values into an array. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359987 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 Woudlint there be a simple way like row1 collum['author']??? Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359989 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2012 Share Posted July 8, 2012 Honestly, in my opinion if you want the simplest way, use the derived table query I posted above. Then you have exactly one record returned, and it's the one you need. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359990 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 Ok how could i assighn a variable that value? Wait so thats getting author 5 correct? Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359993 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 Because i need to assighn $p1 imagelink 1 then $p2 imagelink 2 and so on for 6. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359995 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2012 Share Posted July 8, 2012 What are all of the field names that you need to retrieve? Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359996 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 title and imagelink so $n1 = title 1 $im = imagelink1 and so on to 6 Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1359998 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2012 Share Posted July 8, 2012 Are you storing all 6 links in the same field: `imagelink`? Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360000 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 In the same collum(using mysql ad i said) or if you ment in the same variable no theres a new one for each $img1 $img2 ans so on. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360002 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 ? Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360045 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2012 Share Posted July 8, 2012 What I mean is, are there 6 different links stored in each `imagelink` field? In other words, does the record look like this: -------------------------------------------------------------------------------------- | id | imagelink | -------------------------------------------------------------------------------------- | 56 | link1, link2, link3, link4, link5, link6 | -------------------------------------------------------------------------------------- Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360050 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 fifth by id as you so one of the things i was getting was id this is an incrementing number so it would be if the first one was 1 then it would be id 5. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360051 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 sorry about the post above the quote go screwed. Well im storing it like this Imagelink http http http http http http because its a collum name. And i want to get the fifth ordered by id. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360052 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2012 Share Posted July 8, 2012 Well, that puts us right back to using the query I already provided. It returns the fifth record, ordered by id, descending. The value you need will be in it. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360065 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 Can i get multiple because i need to get 1-6, fifth was just a example Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360071 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 And how would i assighn a varible that value. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360072 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2012 Share Posted July 8, 2012 Everything you said indicated you wanted to retrieve one record. Now you say that was just an example and you really wanted to do was retrieve 6 records? How about you specify exactly what you want to do, provide a sample of the actual data, the structure of the table, and the results you hope to get from the data. Examples don't work so well with writing code, because code does exactly what it is written to do. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360073 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 Well I want to get the last 6 records of the two(or how many there is) collums of that table. Then get each of those values title and imagelink for the six and assighn a varible $im1-6 and $n1-6 Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360113 Share on other sites More sharing options...
rubyarat2010 Posted July 8, 2012 Author Share Posted July 8, 2012 Please I have been trying at this for several weeks. Quote Link to comment https://forums.phpfreaks.com/topic/265369-get-sql-specific-collum-and-row/#findComment-1360122 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.