开发者

mysql fetch array

开发者 https://www.devze.com 2023-02-27 01:56 出处:网络
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?

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开发者_如何学Go
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?


$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.


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];  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消