以下面代码查询为例
<?phpheader("Content-type: text/html; charset=utf-8"); $host="localhost";$user="root";$password="root";$dbase_name="liuyan";$table_name="contents";$conn=mysql_connect($host,$user,$password) or die("连接服务器失败。".mysql_error());echo "数据库服务器:$host 用户名:$user<br>";echo "...................................................<br>";mysql_select_db($dbase_name,$conn) or die("连接数据库失败".mysql_error());echo "数据库名:".$dbase_name.",数据表名:".$table_name."<br>";mysql_query("SET NAMES 'UTF8'");$mysql_command="select * from ".$table_name."where id=5";$result=mysql_query($mysql_command,$conn) or die("<br>数据表无记录<br>");$i=0;while ($record=mysql_fetch_row($result)){ $i+=1; echo "ID号:".$record[0]."<br>"; echo "姓名:".$record[1]."<br>"; echo "内容:".$record[2]."<br>"; echo "...................................................<br>";}echo "成功显示数据表".$table_name."的记录,记录数为:".$i;提示如下:

但是数据表中有数据,检查很久才发现拼接SQL语句时最常见的错误 忘了加空格,将代码改为:
$mysql_command="select * from ".$table_name." where id=5";则运行通过。