PDO Connection Database

Source: Internet
Author: User

<style>
table{
border:1px solid gray;
Border-collapse:collapse;
width:85%;
margin:0 Auto;
Text-align:center;
}
Table th, table td{
border:1px solid gray;
}
</style>
<?php
/**
* Created by Phpstorm.
* User:administrator
* DATE:2017/8/1
* Time : 17:18
*/
//Normal connection database
$pdo = new PDO (' mysql:host=localhost;port=3306;charset=utf8;dbname=pg137 ', ' root ', ' );
//password can be omitted
//$pdo = new PDO (' Mysql:host=localhost;port=3306;charset=utf8;dbname=pg37 ', ' root ');
User name can not omit
//$pdo = new PDO (' Mysql:host=localhost;port=3306;charset=utf8;dbname=pg37 ');
The address can be changed
//$pdo = new PDO (' Mysql:host=127.0.0.1;port=3306;charset=utf8;dbname=pg37 ', ' root ', ');
Allow local access only
//$pdo = new PDO (' Mysql:host=255.21.23.12;port=3306;charset=utf8;dbname=pg37 ', ' root ', ');
Var_dump ($PDO);
//Get object
$ps = $pdo->query (' select * from Xuesheng ');
Var_dump ($ps);

Get result set PDO::FETCH_ASSOC only contains column name label
Var_dump ($ps->fetchall (PDO::FETCH_ASSOC));
$arr = Array ();
if ($ps) {
foreach ($ps as $row) {
Array_push ($arr, $row);
}
}
Echo ' <table> ';
Echo ' <tr><th> serial number </th><th> name </th><th> gender </th><th> phone number </th> <th> Home Address </th><th> operations </th></tr> ';
foreach ($arr as $val) {
Echo ' <tr> ';
Echo ' <td> ' $val [0]. ' </td><td> '. $val [1]. ' </td><td> '. $val [2]. ' </td><td> '. $val [3]. ' </td><td> '. $val [4]. ' </td>
</td><td><a href= "updatestu.php?sid=". $val [0]. ' > Modify </a>--<a href= ' updatestu1.php?sid= '. $val [0]. ' > Add </a></td> ';
Echo ' </tr> ';
}
Echo ' </table> ';
Shut down
$pdo = null;

PDO Connection Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.