zoukankan      html  css  js  c++  java
  • 基础--- 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扩展
  • 相关阅读:
    Shodan在渗透测试及漏洞挖掘中的一些用法
    QUdpSocket 简单用法
    用QT操作数据库(本周学的)
    Qt使用UDp通信、套接字socket的成员函数bind()的作用
    ppm的含义
    数字的补数
    两数之和
    C++中的最大整数最小整数
    如何使用dockerfile将jar包生成镜像
    python3解决 json.dumps中文乱码
  • 原文地址:https://www.cnblogs.com/suxiaolong/p/5949280.html
Copyright © 2011-2022 走看看