verdrm Posted January 26, 2008 Share Posted January 26, 2008 How can I echo rows by column ID? For example, normally for all the rows it would be this: $result = mysql_query("SELECT column FROM table"); while($row = mysql_fetch_array($result)) { echo $row['column']; ...BUT I have two rows in the table with IDs of 1 & 2. How do I echo them individually, instead of together? Quote Link to comment Share on other sites More sharing options...
EchoFool Posted January 26, 2008 Share Posted January 26, 2008 $result = mysql_query("SELECT column1,column2 FROM table"); while($row = mysql_fetch_array($result)) { echo $row['column1']; echo $row['column2']; I think you mean like that? but unless i mis-read your post i aint quite sure i understood you. Quote Link to comment Share on other sites More sharing options...
verdrm Posted January 26, 2008 Author Share Posted January 26, 2008 No, I do not mean that. What I mean is if I have two ROWS in a table with IDs of 1 and 2, how do I echo both individually? So echo $row (somehow define ID 1) would output "This is row one". So echo $row (somehow define ID 2) would output "This is row two". Basically I want to supplement "WHERE id = 1" and "WHERE id = 2" in my select statement. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted January 26, 2008 Share Posted January 26, 2008 well you can do range of id's i think WHERE ID > 0 AND ID < 3 or as you put WHERE ID == 1 OR ID == 2 using less than and greater than. I'd recommend the first method cos then you can alter it to select an easy range of rows using variables in the future. then u while loop and it should only while loop 1 and 2 if they exist in the table Quote Link to comment Share on other sites More sharing options...
verdrm Posted January 26, 2008 Author Share Posted January 26, 2008 That still doesn't solve my problem. I want to echo the variables individually. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 26, 2008 Share Posted January 26, 2008 Do a print_r on the array and you can view how it's indexed. Quote Link to comment Share on other sites More sharing options...
verdrm Posted January 26, 2008 Author Share Posted January 26, 2008 Is there a way in PHP to echo $row[1] (1 being the ID of the column) and echo $row[2] (2 being the ID of the column)? Something like that? I want to split up the results of a SELECT statement by column ID. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 26, 2008 Share Posted January 26, 2008 You could try $row['1'] but it may be better if you either used a numerical value, so say Field 1 is the 5th column in the DB, just echo $row[4] Quote Link to comment Share on other sites More sharing options...
verdrm Posted January 26, 2008 Author Share Posted January 26, 2008 Let me try to put this differently...maybe I am explaining this the wrong way. Normally, to get two values one would do: $result1 = mysql_query("SELECT text FROM table WHERE id = 1"); // ID being the auto increment value $result2 = mysql_query("SELECT text FROM table WHERE id = 2"); while($row = mysql_fetch_array($result1)) { echo $row['text']; } while($row = mysql_fetch_array($result2)) { echo $row['text']; } I would like to just do this: $result = mysql_query("SELECT text FROM table); ...BUT still echo both rows individually in different parts of my script. How do I manipulate the WHILE function or ARRAY to echo $row[1] or $row[2] (that isn't the correct syntax, but for example purposes...)? Quote Link to comment Share on other sites More sharing options...
revraz Posted January 27, 2008 Share Posted January 27, 2008 You say $row[1] and $row[2] like they are the autoincrement value. mysql_fetch_array creates an array from the Fieldnames it reads in. So your table is like table id | name | job 1 | Fred | Janitor 2 | John | Manager So if you echo $row[1] you will get 1, echo $row[2] you get Fred. So if you want to do this for every record, you echo it during the while loop while($row = mysql_fetch_array($result1)) { echo $row['id']; } This will echo a line for Fred and a line for John, and display 1 2 Quote Link to comment Share on other sites More sharing options...
verdrm Posted January 27, 2008 Author Share Posted January 27, 2008 I'm using $row[1] as an example, although I know it will output '1'. I want to echo the column data corresponding to ID 1,2 not echo 1,2. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 27, 2008 Share Posted January 27, 2008 Show the table layout. Quote Link to comment 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.