基础--- php 操作 mysql

$link = mysql_connect('127.0.0.1:3306', 'root', '1234abcd');
var_dump($link);

$result1 = mysql_query('use ecshop',$link);
$result2 = mysql_query('set names utf8');

$result = mysql_query('show databases', $link);
var_dump($result);
echo '<hr>';
$row = mysql_fetch_row($result);
var_dump($row);
echo '<br>';
$row = mysql_fetch_array($result);
var_dump($row);
echo '<br>';
$row = mysql_fetch_assoc($result);
var_dump($row);

//echo '<hr>';
//while($row = mysql_fetch_assoc($result)) {
//    echo $row['Database'];
//    echo '<br>';
//}
//echo '<hr>';
//var_dump($row);

mysql_free_result($result);
mysql_close($link);


/**
 * @param $table_name string 表名
 */
function fields($table_name) {
}

fields('ecs_goods');
array('goods_id', 'goods_name', 'shop_price', ....., 'pk'=>'goods_id');

 

$link = mysql_connect('127.0.0.1:3306', 'root', '1234abcd');
//var_dump($link);

if($result = mysql_query('use ecsho', $link)) {
    //
    echo '成功';
} else {
    //失败,错误报告
    echo mysql_errno($link);
    echo '<br>';
    echo mysql_error($link);
}

echo '<hr>';
$query = "show databases";
$query = "select * from ecshop.ecs_goods where goods_id<10";
$result = mysql_query($query);
echo mysql_num_rows($result);

echo '<hr>';

$query = "update ecshop.ecs_goods set shop_price = shop_price+100 where goods_id < 15";
$query = "update ecshop.ecs_goods set is_new = 1 where goods_id < 15";

mysql_query($query);
echo mysql_affected_rows($link);


echo '<hr>';

$query = "insert into ecshop.ecs_goods (goods_id, goods_name, shop_price, goods_desc) values (null, 'iphone5', 2345.56, 'shui')";
var_dump(mysql_query($query));

echo mysql_insert_id($link);

 

PDO

try{
    $pdo = new PDO("mysql:host=localhost;dbname=dh;","root","root",array(PDO::ATTR_PERSISTENT => true));
//    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);  //提示警告信息
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);  //抛异常进行提示
//    echo "链接成功";
}catch (PDOException $e){
    die("数据库链接失败".$e->getMessage());
}

echo '<pre>';
//$sql = "select * from tuser";
//$stmt = $pdo->query($sql);
//$list = $stmt->fetchAll();


$sql = "insert into stuu values(null,'xiaosu',1,1)";
//$res = $pdo->exec($sql);
//if($res){
//    echo 'success';
//}else{
//    echo $pdo->errorCode();
//    var_dump($pdo->errorInfo());   //打印错误信息
//}

try{
    $res = $pdo->exec($sql);
}catch (PDOException $e){
    echo $e->getMessage();
}



//query()  查询  exec()  执行增删改
//php_pdo_mysql.dll  mysql扩展
//php_pdo_oci.dll    oracle扩展
//php_pdo_mssql.dll  sql server扩展

 

posted @ 2016-10-11 14:57  伊人世界  阅读(185)  评论(0编辑  收藏  举报
Fork me on GitHub