1

I assume this must be simple, but I'm just stuck on a solution here. How would I echo data from the id in the database? I cannot edit the css div where I display this data so need to find out a way to cut down the PHP, for example:

<?  $query = "SELECT * FROM example WHERE id='1'";

        $result_1 = mysql_query("SELECT * FROM example WHERE id=1");
        $result_2 = mysql_query("SELECT * FROM example WHERE id=2");
        $result_3 = mysql_query("SELECT * FROM example WHERE id=3");



        while($row_1 = mysql_fetch_array($result_1))
        while($row_2 = mysql_fetch_array($result_2))
        while($row_3 = mysql_fetch_array($result_3))


          { ?>

From here I echo the data in a div:

<? echo $row_1['name'] ?>

What I am trying to do is something like this: echo $row_1['name']['1']. I want to somewhat use the WHERE id=1 inside my echo. Sorry if this is not clear.

Thanks

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
User_coder
  • 477
  • 1
  • 7
  • 21

4 Answers4

2

You can easily do:

$result = mysql_query("SELECT * FROM example WHERE id IN (1, 2, 3)");
while ($row = mysql_fetch_assoc($result)) {
    echo ($row['id'] == 1) ? $row['id'] : ''; // print id only when id == 1
}

Notice: This extension (mysql_*) is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

Shoe
  • 74,840
  • 36
  • 166
  • 272
0

Try something like this:

<?  
$result = mysql_query("SELECT * FROM `example`"); // Select all columns and rows from the example table

while($row = mysql_fetch_assoc($result)) { // For each row returned
    print($row['id'] . " - " . $row['name'] . "<br />\n"); // Print the row's ID and name
}
?>
EM-Creations
  • 4,195
  • 4
  • 40
  • 56
0

You can try to do an if() just before the echo, like below:

<? if($row_1['id'] == 1) echo $row_1['name']; ?>

Or from what we know, your results in $row_1 will always have id == 1, since you specified it in the query.

Hope this helps, good luck :)

Wessel
  • 81
  • 4
0

Try this:

$result = mysql_query("SELECT * FROM example WHERE id IN (1, 2, 3)");
while ($row = mysql_fetch_assoc($result)) {
    echo ($row['id'] == 1) ? $row['id'] : ''; // print id only when id == 1
}
user2001117
  • 3,727
  • 1
  • 18
  • 18