本文实例讲述了PHP5.5基于mysqli连接MySQL数据库和读取数据操作。分享给大家供大家参考,具体如下:

在学习1. 开启PHP的API支持

(1)首先修改您的php.ini的配置文件。

查找下面的语句:

;extension=php_mysqli.dll

将其修改为:

extension=php_mysqli.dll

(2)重新启动Apache/IIS,即可。

(3)说明:PHP需要单独的文件来支持这个扩展库,一般在PHP目录下的ext目录里能找到php_mysqli.dll文件(PHP <= 5.0.2 中是 libmysqli.dll),当然,在PHP的配置文件当中要有正确指向ext的信息(extension_dir)。假若您的PHP没有这个文件,您可以去下载PHP5的源码包。另外,这个API扩展,只能在PHP5以上版本使用。其它具体信息,请看下面。

2.PHP mysqli身份证

mysqli是“MySQL, Improved”的缩写,该扩展仅适用于PHP 5。它能用于MySQL 4.1.1和更高版本。该扩展完全支持MySQL 5.1中采用的鉴定协议,也支持预处理语句和多语句API。此外,该扩展还提供了先进的、面向对象的编程接口

<"Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
    exit;
  }
  /* Send a query to the server 向服务器发送查询请求*/
  if ($result = mysqli_query($link, 'SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5')) {
    //print("Very large cities are: ");
    /* Fetch the results of the query 返回查询的结果 */
    while( $row = mysqli_fetch_assoc($result) ){
      printf("%s (%s) ", $row['Name'], $row['Population']);
    }
    /* Destroy the result set and free the memory used for it 结束查询释放内存 */
    mysqli_free_result($result);
  }
  /* Close the connection 关闭连接*/
  mysqli_close($link);
"htmlcode">
<"localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
  // 输出数据
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
  }
} else {
  echo "0 结果";
}
$conn->close();
"htmlcode">
<"localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("连接失败: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
  // 输出数据
  while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
  }
} else {
  echo "0 结果";
}
mysqli_close($conn);
"htmlcode">
<"<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";
class TableRows extends RecursiveIteratorIterator {
  function __construct($it) {
    parent::__construct($it, self::LEAVES_ONLY);
  }
  function current() {
    return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
  }
  function beginChildren() {
    echo "<tr>";
  }
  function endChildren() {
    echo "</tr>" . "\n";
  }
}
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
  $stmt->execute();
  // 设置结果集为关联数组
  $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
    echo $v;
  }
}
catch(PDOException $e) {
  echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
"_blank" href="//www.jb51.net/Special/768.htm">php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

标签:
PHP5.5,mysqli,连接,MySQL数据库,读取数据

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com