1

Well, very newbie question here, but I couldn't find the appropiate question to put it on google or here.

I have a very small table, with only 3 rows, every row with a defined id (1, 2, 3).

What I want to do is to print the row data of each one, but avoiding a loop function.

I.e.

The query would be:

$query = "SELECT field1, field2, field3 FROM table";
$var1 = mysql_query($query);
$var2 = mysql_fetch_assoc($var1);
$var3 = mysql_num_rows($var1);

The PHP:

<p><?php echo $var2['field1'];//how can i tell to the code "use the row with this id here -> id=1" ?></p>

<p><?php echo $var2['field1']; // "and use the row with the id "2" here" ?></p>

PS: Sorry about my poor english, argentine dude here.

Ok,

This is why I didn't want to do it as a loop:

I know I can do it this way:

<?php do { ?>

<p>bla bla <?php echo $var2[field1]; ?> </p> 

<?php } while ($var2 = mysql_fetch_assoc($var1)); ?>

But it can't be done like this, because the site design puts this data in different divs, with different positioning and CSS formatting.

I would like to do it the right way, avoiding the use o one query per row.

The rows will be forever 3, and the id of each one will always be the same (1, 2, 3).

2
  • There is no way not using a loop. Commented Aug 2, 2012 at 2:52
  • @user1570159 See my answer, I posted an example of how to do it simply with a loop. Commented Aug 2, 2012 at 2:57

5 Answers 5

1

you can use mysql_field_seek like this:

$query = "SELECT field1, field2, field3 FROM table";
$query_result = mysql_query($query);

$var1 = mysql_field_seek($query_result,0);
$var2 = mysql_field_seek($query_result,1);
$var3 = mysql_field_seek($query_result,2);

$resutl1 = mysql_fetch_field($var1);
$resutl2 = mysql_fetch_field($var2);
$resutl3 = mysql_fetch_field($var3);
Sign up to request clarification or add additional context in comments.

Comments

0

There is no way to do it without a loop. Why do you want to avoid a loop, though? It should be fairly simple, for example:

$query = 'SELECT field1, field2, field3 FROM table';
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
    echo "<p>{$row['field1']}</p>";
mysql_free_result($result);

Comments

0

Here's a simple function to get the fieldname and id. Then you can call it for any field/id combo you need.

function getField($fieldName, $id)
{ 
  $sql = sprintf("SELECT `%s` FROM `table` WHERE `id` = %d", $fieldName, $id);
  $result = mysql_query($sql);
  return mysql_num_rows($result) == 1 ? mysql_result($result, 0, 0) : null;
}

echo getField('field1', 1);
echo getField('field1', 2);

Comments

0

I can't think of a reason not to use a loop... In general the mysql_fetch_assoc function returns a row of data from the result resource and then automatically increments the internal row pointer in the result resource. This would be the loop way to do it:

while ($row = mysql_fetch_assoc($var1)) {
    echo $row["field1"];
    echo $row["field2"];
    echo $row["field3"];
}

mysql_free_result($var1);

However, you could do this since the internal row pointer is automatically incremented.

$row1 = mysql_fetch_assoc($var1);
$row2 = mysql_fetch_assoc($var1);
$row3 = mysql_fetch_assoc($var1);

echo $row1["field1"];
echo $row1["field2"];
echo $row1["field3"];

echo $row2["field1"];
...

and that will essentially get you the same thing without the loop. Happy coding!

Comments

0

How about this one? this might work for you.

$result = mysql_query("SELECT field1, field2, field3 FROM table");

$arr1 = (mysql_fetch_array($result));
$arr2 = (mysql_fetch_array($result));
$arr3 = (mysql_fetch_array($result));

switch ($x)
{
    case 1: echo $arr1[0] ." - ".$arr1[1] ." - ".$arr1[2]; break;
    case 2: echo $arr2[0] ." - ".$arr2[1] ." - ".$arr2[2]; break;
    case 3: echo $arr3[0] ." - ".$arr3[1] ." - ".$arr3[2]; break;
    default: echo "Unknown";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.