我想在mysql中查询多行作为变量。例如:
SELECT name, tid FROM term_data WHERE vid = 2
这是结果:
name | tid
-----|----
Jack | 55
Tony | 87
John | 32
然后我想用while:
while (...) {
print "My name is: $name and my ID is: $tid";
//name and tid should be printed from database.
}
我可以查询一行并将其放入while
,但这怎么可能?
答案 0 :(得分:1)
<?php
//Conection
...
$query= "SELECT name, tid FROM term_data WHERE vid = 2";
if (query_run=mysql_query($query)) {
while ($query_row=mysql_fetch_assoc($query_run)) {
$name=$query_row['name'];
$tid=$query_row['tid'];
echo 'My name is'.$name.'and my ID is'.$tid;
}
}
else {
echo mysql_error();
}
?>
唯一剩下的就是解决由您决定的连接。
答案 1 :(得分:1)
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>