Heim >Backend-Entwicklung >PHP-Tutorial >Datenbankabfrage
<?php //连接数据库的参数 $host = "localhost"; $user = "root"; $pass = "zq19890319"; $db = "phpdev"; //创建一个mysql连接 $connection = mysql_connect($host, $user, $pass) or die("Unable to connect!"); //选择一个数据库 mysql_select_db($db) or die("Unable to select database!"); //开始查询 $query = "SELECT * FROM symbols"; //执行SQL语句 $result = mysql_query($query) or die("Error in query: $query. ".mysql_error()); //显示返回的记录集行数 if(mysql_num_rows($result)>0){ //如果返回的数据集行数大于0,则开始以表格的形式显示 echo "<table cellpadding=10 border=1>"; while($row=mysql_fetch_row($result)){ echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>".$row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "</tr>"; } echo "</table>"; } else{ echo "记录未找到!"; } //释放记录集所占用的内存 mysql_free_result($result); //关闭该数据库连接 mysql_close($connection); ?>