作为程序员一定要保持良好的睡眠,才能好编程

php使用mysql连接数据库代码

发布时间:2016-10-28

$con=mysql_connect("localhost:3306","root","song");
    if(!$con){
        
        die("mysql can not to connect:".mysql_error());
        
    }
    
    mysql_select_db("newtts");
    
    $sql="select * from web_article order by id desc limit 10";
    
    $res=mysql_query($sql);
    if($res){
        $_data=array();
        /**while($row = mysql_fetch_object($res)){
            $_data[]=$row;
        }
        while($row=mysql_fetch_array($res,MYSQL_ASSOC)){
            $_data[]=$row;
        }
        **/
        while($row= mysql_fetch_lengths($res)){
            $_data[]=$row;
        }
        
    }else{
        die("mysql error:".mysql_error());
    }
    
    echo "<pre style='color:blue'>";
    print_r($_data);
    echo "</pre>";
    
    mysql_free_result($res);
    mysql_close($con);


543086206537443948.jpg