1.
复制代码 代码如下:
$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器
mysql_select_db("test",$connection);
$query="insert into users(user_name)"; //在test数据库里插入一条数据
$query.="values('tuxiaohui')";
$result=mysql_query($query);
if(!$query)
echo "insert data failed!
";
else{
$query="select * from users"; //查询数据
$result=mysql_query($query,$connection);
for($rows_count=0;$rows_count{
echo "用户ID:".mysql_result($result,$rows_count,"user_id")."
";
echo "用户名:".mysql_result($result,$rows_count,"user_name")."
";
}
}
?>
复制代码 代码如下:
$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_row($result))
{
echo "用户ID:".$row[0]."
";
echo "用户名:".$row[1]."
";
}
?>
复制代码 代码如下:
$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_array($result))
{
echo "用户ID:".$row[0]."
"; //也可以写做$row["user_id"]
echo "用户名:".$row[1]."
"; //也可以写做$row["user_name"]
}
?>
复制代码 代码如下:
$connection=mysql_connect("localhost","root","root"); //连接并选择数据库服务器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_object($result))
{
echo "用户ID:".$row->user_id."
"; //通过对象运算符->获得改行数据在其属性上的值。
echo "用户名:".$row->user_name."
";
}
?>