1.mysql

$dbc=mysql_connect('localhost',$user,$pass) or die('error!');
mysql_select_db("testdb", $dbc);
mysql_query("SET NAMES 'utf8'");
$query="select * from `testtable`";
$result=mysql_query($query,$dbc) or die('wrong');
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  print_r($row);
}
	

2.mysqli


3.pdo

try {
    $dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF8', $user, $pass);
    foreach($dbh->query('SELECT * from testtable') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
try {
    $dbh = new PDO('mysql:host=localhost;dbname=simplehtmldom;charset=UTF8', 'root', '');
    $text = 'testtext';
    $query="INSERT INTO testdb (text) VALUES ('$text')";
    $dbh->exec($query);
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

注意事项,插入数据时候除ID外所有字段都必须赋值,否则无法成功,或者可以预设默认值,或者设置字段可为空

try {
    $dbh = new PDO('mysql:host=localhost;dbname=simplehtmldom;charset=UTF8', 'root', '');
    $type = '预选赛';
    $query="UPDATE worldcup_match SET type='$type' WHERE id=1";
    $dbh->exec($query);
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
作者 铁血 汉子 2017年12月30日
2018/10/22/05:12:03am 2017/12/30/14:05:33
0 436