Jump to content

Echo Row by ID


verdrm

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...)?

 

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.