2 回答
TA贡献1995条经验 获得超2个赞
PHP查询MySQL并输出使用的是SELECT 语句。
语法:
1 | SELECT column_name(s) FROM table_name |
SELECT 语句用于从数据库中选取数据。
示例:
选取存储在 "Persons" 表中的所有数据(* 字符选取表中所有数据)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; }
mysql_close($con); ?> |
在 HTML 表格中显示结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>";
while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>";
mysql_close($con); ?> |
以上代码的输出:
TA贡献1845条经验 获得超8个赞
$sql=mysql_query("select name from use where id=123");
$row=mysql_fetch_array($sql);
echo $row["name"];
这样就行
- 2 回答
- 0 关注
- 2404 浏览
添加回答
举报