1

I am trying to do a fetch array which pulls out 4 rows and each row has 2 columns. Is there anyway I can define each individual field in each row as a variable individually?

row 1 name, id
row 2 name, id
row 3 name, id
row 4 name, id
<?php echo $row1name;?>
<?php echo $row1id;?>
<?php echo $row2name;?>
<?php echo $row2id;?>
<?php echo $row3name;?>
<?php echo $row3id;?>
<?php echo $row4name;?>
<?php echo $row4id;?>

Does this make sense?

0

2 Answers 2

5
$r = array();
$query = mysql_query("select id,name from table");
while ($row = mysql_fetch_assoc($query)) {
$r[] = $row;
}

echo $r[1]['name'];
echo $r[3]['id'];

and so on.

You can do

echo '<pre>';
print_r($r);

if you want to see the content of your array.

Sign up to request clarification or add additional context in comments.

Comments

0

To access the fields in separate rows, you can do something like this:

mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo "ID: ".$row[0];
    echo "NAME: ".$row[1];  
}

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.