有可能做这样的事吗?我是新手,所以不知道!
//row 1 in mysql array
$var = "hello";
$id = 1;
//row 2 in mysql array
$var = "bye";
$id = 2;
//Closing while loop
//Is the following possible?
echo $var1; //outputs "hello"
echo $var2; //outputs "bye"
答案 0 :(得分:3)
不是你想要的,你可以使用数组:
$var[1] = "hello";
$var[2] = "bye";
echo $var[1];
echo $var[2];
答案 1 :(得分:0)
你提到了MySQL。所以你想要实现的更完整的例子是:
$var = array();
$id = array();
while ($row = mysql_fetch_assoc($result)) {
$var[] = $row['text']; // don't know how the column is named
$id[] = $row['id'];
}
echo $row[0]; // hello
echo $row[1]; // bye
使用循环变量i
:
$var = array();
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
$var[$i] = $row['text']; // don't know how the column is named
$i++;
}
echo $row[1]; // hello
echo $row[2]; // bye
答案 2 :(得分:0)
我会这样做
$id = 1;
$var[$id] = "hello";
$id = 2;
$var[$id] = "bye";
echo $var['1'];
echo $var['2'];
// or
echo $var[$id];
答案 3 :(得分:0)
// Define them both as arrays
$var = array();
$id = array();
// In your loop
$var[] = "hello";
$id[] = 1;
// Next iteration
$var[] = "bye";
$id[] = 2;
// Printing them out
echo $var[0]; // prints out "hello"
echo $var[1]; // prints out "bye"
使用数组^^
可替换地:
$rows = array();
// In your loop
$rows[] = array("id" => 1, "var" => "hello");
// Next iteration
$rows[] = array("id" => 2, "var" => "bye");
// Print them out
echo $rows[0]["var"]; // Prints hello
echo $rows[1]["var"]; // Prints bye