PHP + MySQL 项目教程

PHP + MySQL 项目教程 扫二维码继续学习 二维码时效为半小时

PHP + MySQL 项目教程,PHP基础技术和MySQL项目开发基础

该课程属于 2017级软件技术2班
请加入后再学习

insert into student (xh,xm)values(20170533120231,'胡伟'); select * from student; insert into score(xh,kcid,cj)values(2070533120231,'kc001',100); select * from score; select a.xm,b.kcmc,c.cj frpm student a,course b,score c where c.xh = a.xh and c.kcid=b.kcid and c.xh = '20170533120231';

[展开全文]
<?php $id=mysql_connect("localhost","root","root"); if ($id){ echo "OK,数据库连接成功!
"; $ok=mysql_select_db("student",$id); if($ok){ echo "OK,选择数据库成功!"; }else{ echo "OH,选择数据库失败,请确认数据库是否存在。"; } }else{ echo "OH,数据库连接失败!请检查服务器地址、用户名和地址是否正确!
"; } ?>
[展开全文]

1、连接数据库服务器,选择数据库

<HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<TITLE>连接数据库服务器,选择数据库</TITLE>

</HEAD>

<BODY>

<?php

$id=mysql_connect("localhost","root","root");

if ($id){

echo "OK,数据库连接成功!<br>";

$ok=mysql_select_db("student",$id);

if($ok){

echo "OK,选择数据库成功!";

}else{

echo "OH,选择数据库失败,请确认数据库是否存在。";

}

}else{

echo "OH,数据库连接失败!请检查服务器地址、用户名和地址是否正确!

<br>";

}

?>

</BODY>

</HTML>

 

2、用PHP创建新数据库和表 <HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<TITLE>用PHP创建新数据库和表</TITLE>

</HEAD>

<BODY>

<?php

if (!$id=mysql_connect("localhost","root","root")){

echo "数据库服务器连接错误!";

exit; //如果数据库服务器连接不成功,退出程序执行

}

echo "数据库服务器连接成功!<br>";

if (!mysql_query("CREATE DATABASE newdata",$id)){

echo"数据库创建不成功,请检查账号权限和数据库是否已经存在!";

exit; //如果数据库创建不成功,退出程序执行

echo "数据库创建成功!<br>";

if (!mysql_select_db("newdata",$id)){

echo "数据库选择不成功!";

exit; //如果数据库选择不成功,退出程序执行

}

echo "数据库选择成功!<br>";

if (!mysql_query("CREATE TABLE testtable (name varchar(10), age int(4))",$id)){

echo "数据表创建不成功,请检查SQL语句是否正确!";

exit; //如果SQL执行不成功,退出程序执行

}

echo "数据表创建成功!<br>";

if (mysql_close($id)){

echo "数据服务器连接关闭成功!";

}

?>

</BODY>

</HTML>

 

 

3、用PHP向表中插入数据

<HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<TITLE>用PHP向表中插入数据</TITLE>

</HEAD>

<BODY>

<?php

$id=mysql_connect("localhost","root","root");

mysql_select_db("newdata",$id);

for($i=1;$i<6;$i++){

$nl=20+$i;

$xm="姓名".$i;

$sql="INSERT INTO testtable VALUES('".$xm."',".$nl.")"; 

$excu=mysql_query($sql,$id);

if($excu){

echo $sql;

echo "第".$i."条数据插入成功!<br>";

}else{

echo "数据插入失败,错误信息:<br>";

echo mysql_error(); //输出上一次MySQL执行的错误信息

}

}

mysql_close($id);

?>

</BODY>

</HTML>

 

 

4、用PHP从表中读取数据 <HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<TITLE>用PHP从表中读取数据</TITLE>

</HEAD>

<BODY>

<?php

$id=mysql_connect("localhost","root","root");

mysql_select_db("newdata",$id);

$query="SELECT * FROM testtable";

$result=mysql_query($query,$id);

$datanum=mysql_num_rows($result);

echo "表testtable中共有".$datanum."条数据<br>"; 

?>

<table width="228" height="34" border="1">

<?php while ($info=mysql_fetch_array($result,MYSQL_ASSOC)){ ?>

<tr>

<td width="99" height="28"><?php echo $info["name"]?> </td>

<td width="113"><?php echo $info["age"]?></td>

</tr>

<?php } ?>

</table>

<?php mysql_close($id);?>

</BODY>

</HTML>

 

 

5、用PHP实现数据分页

<HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<TITLE>用PHP实现数据分页</TITLE>

</HEAD>

<BODY>

<?php

$id=mysql_connect("localhost","root","root");

mysql_select_db("newdata",$id);

$query="SELECT * FROM testtable";

$result=mysql_query($query,$id);

$datanum=mysql_num_rows($result);

$page_id=$_GET["page_id"];

if ($page_id==""){

$page_id=1;

}

$page_size=2; //定义每页显示条数

$page_num=ceil($datanum/$page_size); 

?>

表testtable中共有<?php echo $datanum;?>条数据<br>

每页<?php echo $page_size;?>条,共<?php echo $page_num;?>页。<br>

<?php

for ($i=1;$i<=$page_num;$i++){

echo "[<a href=?page_id=".$i.">".$i."</a>]";

}

$start=($page_id-1)*$page_size;

$query2="SELECT * FROM testtable limit $start,$page_size";

$result2=mysql_query($query2,$id);

?>

<table width="228" height="34" border="1">

<?php while ($info = mysql_fetch_array($result2, MYSQL_ASSOC)) { ?>

<tr>

<td width="99" height="28"><?php echo $info["name"]?></td>

<td width="113"> <?php echo $info["age"]?></td>

</tr>

<?php }?>

</table>

<?php mysql_close($id);?>

</BODY>

</HTML>

[展开全文]

 

<HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<TITLE>用PHP实现数据分页</TITLE>

</HEAD>

<BODY>

<?php

$id=mysql_connect("localhost","root","root");

mysql_select_db("newdata",$id);

$query="SELECT * FROM testtable";

$result=mysql_query($query,$id);

$datanum=mysql_num_rows($result);

$page_id=$_GET["page_id"];

if ($page_id==""){

$page_id=1;

}

$page_size=2; //定义每页显示条数

$page_num=ceil($datanum/$page_size); 

?>

表testtable中共有<?php echo $datanum;?>条数据<br>

每页<?php echo $page_size;?>条,共<?php echo $page_num;?>页。<br>

<?php

for ($i=1;$i<=$page_num;$i++){

echo "[<a href=?page_id=".$i.">".$i."</a>]";

}

$start=($page_id-1)*$page_size;

$query2="SELECT * FROM testtable limit $start,$page_size";

$result2=mysql_query($query2,$id);

?>

<table width="228" height="34" border="1">

<?php while ($info = mysql_fetch_array($result2, MYSQL_ASSOC)) { ?>

<tr>

<td width="99" height="28"><?php echo $info["name"]?></td>

<td width="113"> <?php echo $info["age"]?></td>

</tr>

<?php }?>

</table>

<?php mysql_close($id);?>

</BODY>

</HTML>

[展开全文]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

</head>

 

<BODY>

<?php

 

$id=mysql_connect("localhost","root","root");

if ($id){

echo "OK,数据库连接成功!<br>";

$ok=mysql_select_db("student",$id);

if($ok){

echo "OK,选择数据库成功!";

}else{

echo "OH,选择数据库失败,请确认数据库是否存在。";

}

}else{

echo "OH,数据库连接失败!请检查服务器地址、用户名和地址是否正确! <br>";

}

 

?>

</BODY>

</HTML>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

</head>

 

<BODY>

<?php

 

if (!$id=mysql_connect("localhost","root","root")){

echo "数据库服务器连接错误!";

exit; //如果数据库服务器连接不成功,退出程序执行

}

echo "数据库服务器连接成功!<br>";

if (!mysql_query("CREATE DATABASE newdata",$id)){

echo"数据库创建不成功,请检查账号权限和数据库是否已经存在!";

exit; //如果数据库创建不成功,退出程序执行

echo "数据库创建成功!<br>";

if (!mysql_select_db("newdata",$id)){

echo "数据库选择不成功!";

exit; //如果数据库选择不成功,退出程序执行

}

echo "数据库选择成功!<br>";

if (!mysql_query("CREATE TABLE testtable (name varchar(10), age int(4))",$id)){

echo "数据表创建不成功,请检查SQL语句是否正确!";

exit; //如果SQL执行不成功,退出程序执行

}

echo "数据表创建成功!<br>";

if (mysql_close($id)){

echo "数据服务器连接关闭成功!";

}

 

?>

</BODY>

</HTML>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

</head>

 

<BODY>

<?php

 

$id=mysql_connect("localhost","root","root");

mysql_select_db("newdata",$id);

for($i=1;$i<6;$i++){

$nl=20+$i;

$xm="姓名".$i;

$sql="INSERT INTO testtable VALUES('".$xm."',".$nl.")"; 

$excu=mysql_query($sql,$id);

if($excu){

echo $sql;

echo "第".$i."条数据插入成功!<br>";

}else{

echo "数据插入失败,错误信息:<br>";

echo mysql_error(); //输出上一次MySQL执行的错误信息

}

}

mysql_close($id);

 

?>

</BODY>

</HTML>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<BODY>
<?php

$id=mysql_connect("localhost","root","root");

mysql_select_db("newdata",$id);

$query="SELECT * FROM testtable";

$result=mysql_query($query,$id);

$datanum=mysql_num_rows($result);

echo "表testtable中共有".$datanum."条数据<br>"; 

?>
<table width="228" height="34" border="1">
  <?php while ($info=mysql_fetch_array($result,MYSQL_ASSOC)){ ?>
  <tr>
    <td width="99" height="28"><?php echo $info["name"]?></td>
    <td width="113"><?php echo $info["age"]?></td>
  </tr>
  <?php } ?>
</table>
<?php mysql_close($id);?>
</BODY>
</HTML>

_array($result2, MYSQL_ASSOC)) { ?>
  <tr>
    <td width="99" height="28"><?php echo $info["name"]?></td>
    <td width="113"><?php echo $info["age"]?></td>
  </tr>
  <?php }?>
</table>
<?php mysql_close($id);?>
</BODY>
</HTML>

[展开全文]

<HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html;">

[展开全文]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

35Type" content="text/html; charset=utf-8"/>

 

<title>连接数据库服务器,选择数据库</title>

 

</head>

 

<body>

 

<?php

 

$id=mysql_connect("localhost","root","root");

if ($id){

echo "数据库连接成功!<br>";

$ok=mysql_select_db("student",$id);

if($ok){

echo "选择数据库成功!";

}else{

echo "选择数据库失败,请确认数据库是否存在";

}

else{

echo "数据库连接失败!请检查服务器地址、用户名和地址是否正确!<br>";

}

 

?>

 

</body>

 

</html>

[展开全文]

mysqli mysqli_connect([string $hostname [,string $username[,string password[,string $dbname]]]]);

[展开全文]

insert into student (xh,xm)valuse(20170533120247,'方志伟');
select * from student;
insert into score(xh,kcid,id)valuse(20170533120247,'KC001',100);
select * from score;
select a,xm,b,kcmc,c,.cj frpm student a,course b,score c where c.xh=a.xh and c.kcid=b.kcid and c.xh='20170533120247';

[展开全文]

<?php

return array (
    'default' => array (
        'hostname' => '127.0.0.1',
        'database' => 'phpcms',
        'username' => 'root',
        'password' => 'root',
        'tablepre' => 'v9_',
        'charset' => 'utf8',
        'type' => 'mysql',
        'debug' => true,
        'pconnect' => 0,
        'autoconnect' => 0
        ),
);

?>123456789101112131415161718

 

$mysql_user=include(PHPS_PATH.'/caches/configs/database.php');//得到数据库配置
        $username=$mysql_user['default']['username'];
        $password=$mysql_user['default']['password'];
        $tablepre=$mysql_user['default']['tablepre'];
        $database=$mysql_user['default']['database'];

        $con = mysqli_connect($mysql_user['default']['hostname'],$username,$password);//句柄

mysqli_select_db($con,$database);//选择数据库
$sql = ' SELECT * FROM '.$tablepre."pay_account where trade_sn='".$out_trade_no."'";
//file_put_contents('3.txt',$sql);
<div id="studentRead" class="reading" style="z-index:10" >
 <div class="class-table">
<div class="class-table-tit clearfix">
<h3 class="fl">班级课程表</h3>
<a class="fr" id ='studentEditKcb' attr="edit" onclick = "editKcb(this);" style="cursor:pointer;">编辑
 </a>
</div>
<table border="0" cellspacing="0" cellpadding="0" id = "myTable">
<tr>
<th width="5%"></th>
<th width="19%">周一</th>
<th width="19%">周二</th>
<th width="19%">周三</th>
<th width="19%">周四</th>
<th width="19%">周五</th>
</tr>
<tr id = "focustr">
<td rowspan="4" class="td-bg">上<br/>午</td>
<volist name = "dataListStu" id = "val" offset="0" length='1'>
<volist name = "val" id = "value">
<td>
<input  id = "focusId" readonly="true" maxlength='7' type="text"  value="{$value}" />
</td>
</volist>
</volist>
</tr>
<volist name = "dataListStu" id = "val" offset="1" length='3'>
<tr>
<volist name = "val" id = "value">
<td>
<input   readonly="true" maxlength='7' type="text"  value="{$value}" />
</td>
</volist>
</tr>
</volist>
 <tr>
<td rowspan="4" class="td-bg">课<br/>表</td>
<volist name = "dataListStu" id = "val" offset="4" length='1'>
<volist name = "val" id = "value">
<td>
<input   readonly="true" maxlength='7' type="text"  value="{$value}" />
</td>
</volist>
</volist>
</tr>
<volist name = "dataListStu" id = "val" offset="5" length='3'>
<tr>
<volist name = "val" id = "value">
<td>
<input   readonly="true" maxlength='7' type="text"  value="{$value}" />
</td>
</volist>
</tr>
</volist>
</table>
</div>           
</div>
$result2=mysqli_query($con,$sql);
$orderinfo=mysqli_fetch_array($result2);;
$uid=$orderinfo['userid'];
//更新数据库
$sql4 = ' update  '.$tablepre."pay_account set status= 'succ'  where userid=".$uid ." and trade_sn='".$out_trade_no."'";
        $result4=mysqli_query($con,$sql4);
                 mysqli_close($con);

[展开全文]