PHP入门学习的几个不错的实例代码
程序员文章站
2022-08-03 13:30:47
1,php连接数据库
1,php连接数据库
<?php
$dbhost = 'localhost';
$dbuser = 'root'; //你的mysql用户名
$dbpass = '123456'; //你的mysql密码
$dbname = 'data'; //你的mysql库名
//连接本地数据库
$globals["conn"] = mysql_connect($dbhost,$dbuser,$dbpass);
//打开数据库
mysql_select_db($dbname,$globals["conn"]);
?>
2.php读取数据库中,某一字段值
<?php
//读取一列数据
$sql="select * from ec_admin";
$result = mysql_query($sql,$globals["conn"]);
printf("用户名: %s<br>\n", mysql_result($result,3,"username"));
printf("密码: %s<br>\n", mysql_result($result,3,"userpass"));
?>
3,php插入某一条数据
<?php
$sql="insert into ec_admin(username,userpass) values('liugongxun2','jakemary2')";
$result=mysql_query($sql,$globals["conn"])or die(mysql_error());
?>
4,php while循环
<?php
$sql="select * from ec_admin";
$result = mysql_query($sql,$globals["conn"]);
while($myrow = mysql_fetch_row($result))
{
printf("用户名%s %s密码<br />",$myrow[1],$myrow[2]);
}
?>
5,php do while循环
<?php
$sql="select * from ec_admin";
$result = mysql_query($sql,$globals["conn"]);
if($myrow = mysql_fetch_array($result))
{
do
{
printf("用户名%s %s密码<br />",$myrow["username"],$myrow["userpass"]);
}while($myrow = mysql_fetch_array($result));
}
?>
6,php判断表单是否提交
<?php
if ($submit)
{}
?>
<?php
$dbhost = 'localhost';
$dbuser = 'root'; //你的mysql用户名
$dbpass = '123456'; //你的mysql密码
$dbname = 'data'; //你的mysql库名
//连接本地数据库
$globals["conn"] = mysql_connect($dbhost,$dbuser,$dbpass);
//打开数据库
mysql_select_db($dbname,$globals["conn"]);
?>
2.php读取数据库中,某一字段值
<?php
//读取一列数据
$sql="select * from ec_admin";
$result = mysql_query($sql,$globals["conn"]);
printf("用户名: %s<br>\n", mysql_result($result,3,"username"));
printf("密码: %s<br>\n", mysql_result($result,3,"userpass"));
?>
3,php插入某一条数据
<?php
$sql="insert into ec_admin(username,userpass) values('liugongxun2','jakemary2')";
$result=mysql_query($sql,$globals["conn"])or die(mysql_error());
?>
4,php while循环
<?php
$sql="select * from ec_admin";
$result = mysql_query($sql,$globals["conn"]);
while($myrow = mysql_fetch_row($result))
{
printf("用户名%s %s密码<br />",$myrow[1],$myrow[2]);
}
?>
5,php do while循环
<?php
$sql="select * from ec_admin";
$result = mysql_query($sql,$globals["conn"]);
if($myrow = mysql_fetch_array($result))
{
do
{
printf("用户名%s %s密码<br />",$myrow["username"],$myrow["userpass"]);
}while($myrow = mysql_fetch_array($result));
}
?>
6,php判断表单是否提交
<?php
if ($submit)
{}
?>
上一篇: 联想妹纸用Z6 Pro挑战瓶盖:画面捧腹
下一篇: PHP令牌 Token改进版