new mysqli_ and 旧mysql

旧的php处理语法:

1.

<select name="s" onChange="redirec()">
<option selected>请选择</option>
<?php
$conn=mysql_connect("localhost","root",""); //连接MySQL服务器
mysql_select_db("PXSCJ",$conn); //选择PXSCJ数据库
mysql_query("SET NAMES gb2312"); //将字符集设为gb2312
$sql="select distinct 专业 from XSB";    // sql 内容
$result=mysql_query($sql);                 //定义接口
while($row=mysql_fetch_array($result))           //定义循环
{
$ZY=$row['专业'];                                         //打印结果    
echo "<option value='$ZY'>$ZY</option>";
}
?>

 

2.新的php 的处理语法

sample 1.

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
//print_r($_GET);
//exit;
$XH=$_GET['XH']; //取得XH的值
$KCM=$_GET['KCM']; //取得KCM的值
header('Content-Type:text/html;charset=uft8'); //发送header,将编码设为uft8
//$db=new PDO("mysql:host=localhost;dbname=PXSCJ","root","");
$conn= new mysqli('localhost','root','','PXSCJ');                              //连接MySQL服务器
if (mysqli_connect_errno($conn))                                         
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($conn, "utf8");                                                     //将字符集设为uft8
$sql="select 成绩 from CJB where 学号='$XH' and 课程号=(select 课程号 from KCB where 课程名='$KCM')";   // sql 内容
if ($result=mysqli_query($conn,$sql))                                                //定义接口
{
// Fetch one and one row
while ($row= $result->fetch_object())                                               //定义循环
{
printf($row->成绩);                                                                         //打印结果
}
// Free result set
mysqli_free_result($result);
}

mysqli_close($conn);
?>

 

 

sampe 2.

<?php
//1,连接
$conn=mysqli_connect("localhost","root","root");
if(mysqli_connect_errno($conn))
die("无法链接到服务器");
//2,选择数据库
mysqli_select_db($conn,"xuexi51")or die("error");
//3,SQL查询
$sql="SELECT * FROM liuyanban";//多了个,

$result=mysqli_query($conn,$sql);

$data=mysqli_fetch_row($result);

print_r($data); //print不能打印数组
mysqli_close($conn);
?>

sample 3:
http://www.blogjava.net/nkjava/archive/2015/01/20/422291.html


sampe 4:
http://www.w3cschool.cn/php/func-mysqli-fetch-row.html

 

posted @ 2017-04-28 15:29  feiyun8616  阅读(635)  评论(0编辑  收藏  举报