目前我所接触到的php中mysql数据库操作主要有以下三种, 分别为mysql, mysqli过程, mysqli对象
2.MySQL增强版扩展(过程)
3.MySQL增强版扩展(对象)
1.原生 MySQL API
#!/usr/bin/php
<?php
/**
* FileName: testDB.php
* Desc:
* Author: thomasjiao
* Email: thomasjiao@vip.qq.com
* HomePage:
* Version: 0.0.1
* Create: 2014-06-03 17:07:58
* History:
*/
//mysql function origin
function mysql_origin() {
$test = mysql_connect('localhost', 'user', 'user');
$db = mysql_select_db('test', $test);
$sql = "SELECT * FROM table_test";//SELECT, this could alse be INSERT/UPDATE/DELETE
$result = mysql_query($sql, $test);
$num = mysql_num_rows($result);//when querying INSERT/UPDATE/DELETE, using mysql_affected_rows() instead
for($i = 1; $i <= $num; $i++) {
$array[$i] = mysql_fetch_assoc($result);//fetch one on each [key]=>[value]
echo "\n";
//var_dump($array[$i]);
echo "\n";
}
mysql_close($test);
var_dump($array);
echo "\n";
unset($array);
}
mysql_origin();
2.MySQL增强版扩展(过程)
#!/usr/bin/php
<?php
/**
* FileName: testDB.php
* Desc:
* Author: thomasjiao
* Email: thomasjiao@vip.qq.com
* HomePage:
* Version: 0.0.1
* Create: 2014-06-03 17:07:58
* History:
*/
//mysqli function procedure-oriented
function mysqli_procedure() {
$test = mysqli_connect('localhost', 'user', 'user', 'test');
$sql = "SELECT * FROM t_crm_notify";//SELECT, this could alse be INSERT/UPDATE/DELETE
$result = mysqli_query($test, $sql);
$num = mysqli_num_rows($result);//when querying INSERT/UPDATE/DELETE, using mysqli_affected_rows() instead
for($i = 1; $i <= $num; $i++) {
$array[$i] = mysqli_fetch_assoc($result);//fetch one on each [key]=>[value]
echo "\n";
//var_dump($array[$i]);
echo "\n";
}
unset($num);
mysqli_free_result($result);
mysqli_close($test);
var_dump($array);
echo "\n";
unset($array);
}
mysqli_procedure();
3.MySQL增强版扩展(对象)
#!/usr/bin/php
<?php
/**
* FileName: testDB.php
* Desc:
* Author: thomasjiao
* Email: thomasjiao@vip.qq.com
* HomePage:
* Version: 0.0.1
* Create: 2014-06-03 17:07:58
* History:
*/
//mysqli function object-oriented
function mysqli_object() {
$test = new mysqli('localhost', 'user', 'user', 'test');
$sql = "SELECT * FROM t_crm_notify";//SELECT, this could alse be INSERT/UPDATE/DELETE
$test_result = $test -> query($sql);
$num = $test_result -> num_rows;
for($i = 1; $i <= $num; $i++) {
$array[$i] = $test_result -> fetch_assoc();//fetch one on each [key]=>[value]
echo "\n";
//var_dump($array[$i]);
echo "\n";
}
unset($num);
$test_result -> free();
$test -> close();
var_dump($array);
echo "\n";
unset($array);
}
mysqli_object();
以上是三种连接mysql的方案,以及查询的方案, 谨供参考
(使用时需要加上连接失败的处理, 否则肯定会出问题的...)