forumnz Posted January 27, 2008 Share Posted January 27, 2008 Is it possible to select multiple rows from a db. I have vars $1, $2....$5. Is it possible to SELECT something FROM dbname WHERE `id`='$1'..... but do it for all five? Please help, Thanks, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/ Share on other sites More sharing options...
Dada78 Posted January 27, 2008 Share Posted January 27, 2008 Yes you would do it just like that mysql_query("SELECT * FROM table WHERE id='1', id='2', id='3', id='4', id='5'"); Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450240 Share on other sites More sharing options...
cooldude832 Posted January 27, 2008 Share Posted January 27, 2008 Is it possible to select multiple rows from a db. I have vars $1, $2....$5. Is it possible to SELECT something FROM dbname WHERE `id`='$1'..... but do it for all five? Please help, Thanks, Sam. that is impossible for anyone to have php variables $1, $2,$3 because a variable can not start with a number in php Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450246 Share on other sites More sharing options...
toplay Posted January 27, 2008 Share Posted January 27, 2008 Yes you would do it just like that mysql_query("SELECT * FROM table WHERE id='1', id='2', id='3', id='4', id='5'"); That will produce MySQL syntax error. Use the 'IN' keyword. Example . . . WHERE id IN (1, 2, 3, 4, 5) or this method: . . . WHERE id = 1 OR id = 2 OR id = 3 OR id = 4 OR id = 5 See manual for syntax: http://dev.mysql.com/doc/refman/5.0/en/select.html cooldude832 is generally correct. However, there are ways to have a variable name with a number (but not recommended). The $1 syntax will not work, but this ${1} will. Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450248 Share on other sites More sharing options...
Dada78 Posted January 27, 2008 Share Posted January 27, 2008 It is possible if they have a column ID in the DB that is auto-increment and they want to select from those certain rows. Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450249 Share on other sites More sharing options...
toplay Posted January 27, 2008 Share Posted January 27, 2008 It is possible if they have a column ID in the DB that is auto-increment and they want to select from those certain rows. No. Not at least in MySQL syntax. Commas will not work like that in the "WHERE" clause. http://dev.mysql.com/doc/refman/5.0/en/select.html Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450260 Share on other sites More sharing options...
cooldude832 Posted January 27, 2008 Share Posted January 27, 2008 It is possible if they have a column ID in the DB that is auto-increment and they want to select from those certain rows. No. Not at least in MySQL syntax. Commas will not work like that in the "WHERE" clause. http://dev.mysql.com/doc/refman/5.0/en/select.html you both seem a bit off I will try and sort this. Mysql has databases in which tables live. The database is just like a open space iit can't hold any raw data, that data has to live inside of the tables. In the tables a single field is assigned the "primary key" that being the primary key gives a uniqueness value to that row of data to differentiate it from another row. This primary key can be an integer that is auto-incremented, this is very common. A database can not have a "primary key" because it can not have rows. So in short yes you can have a field in a table called some sort of "id" that is auto-incremented, but mysql can not magically tell you that value, you have to explain to it which one of those auto-incremented "ids" you wish to retrieve (or which ones) Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450263 Share on other sites More sharing options...
Dada78 Posted January 27, 2008 Share Posted January 27, 2008 I understand that perfectly, that is exactly what I already said. Yes you can use commas between values, I do it all the time with no problem. Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450266 Share on other sites More sharing options...
toplay Posted January 27, 2008 Share Posted January 27, 2008 you both seem a bit off I will try and sort this. cooldude832, some or rather most members on this forum tend to say DB (or database) and they really mean tables. But it's good you clarified it for the poster. I'm simply trying to correct the syntax. Read my previous posts. Thx. Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450268 Share on other sites More sharing options...
toplay Posted January 27, 2008 Share Posted January 27, 2008 Yes you can use commas between values, I do it all the time with no problem. No, you can't in MySQL. You may thing it works but I bet it's not. You're probably not checking if the query worked before executing subsequent commands (such as fetching). Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450270 Share on other sites More sharing options...
Dada78 Posted January 27, 2008 Share Posted January 27, 2008 You said "will produce MySQL syntax error" I don't get any syntax error and I have even seen it recommend many times and it works for me like a dream with no problems. Here is one example where I use them and I have no problems mysql_query("UPDATE users SET displayname = '$displayname', displaytype = '$displaytype', description = '$description', address = '$address', address2 = '$address2', city = '$city', state = '$state', postal = '$postal', country = '$country', website = '$website' WHERE email='$email'"); Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450277 Share on other sites More sharing options...
cooldude832 Posted January 27, 2008 Share Posted January 27, 2008 I don't see you producing an output of said mysql syntax error you can suppress errors from displaying proper debugging query structure is <?php $q = "Select this from `that` where this=that"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); ?> That will show a mysql error if the query is capable of producing the above stated error. Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450280 Share on other sites More sharing options...
toplay Posted January 27, 2008 Share Posted January 27, 2008 You said "will produce MySQL syntax error" I don't get any syntax error and I have even seen it recommend many times and it works for me like a dream with no problems. Here is one example where I use them and I have no problems mysql_query("UPDATE users SET displayname = '$displayname', displaytype = '$displaytype', description = '$description', address = '$address', address2 = '$address2', city = '$city', state = '$state', postal = '$postal', country = '$country', website = '$website' WHERE email='$email'"); That is an UPDATE and those commas are not in the "WHERE" clause. That is the correct syntax for the UPDATE. But we're not talking about UPDATE in this topic. As stated earlier, the SELECT does not allow commas in the "WHERE" clause like the way you show in your first post here. Glad that's sorted out now. Quote Link to comment https://forums.phpfreaks.com/topic/87999-solved-select-multiple-rows/#findComment-450283 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.