数据库简单的查询

Api.php

<?php

//数据库连接信息

$cfg_dbhost = 'localhost';

$cfg_dbname = 'ceshi';

$cfg_dbuser = 'root';

$cfg_dbpwd = '';

$cfg_dbprefix = 'dede_';

$cfg_db_language = 'utf8';



    $link = @mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd)or die('error');

     //选择test为操作库

    mysql_query("set names {$cfg_db_language}"); // //这就是指定数据库字符集,一般放在连接数据库后面就系了

    mysql_select_db($cfg_dbname,$link);

    $apiname = strip_tags($_POST['apiname']);

    switch($apiname){

        case 'seach':

            $condition = strip_tags($_POST['name']);

            $sql = "SELECT * FROM dede_addonarticle WHERE num = '".$condition."'";

            $result = mysql_query($sql,$link);

            $info = '';

            while($row=mysql_fetch_array($result)){

                $info[] = $row; 

            }

            // d($result);

            echo json_encode($info);

            break;

    }

    

    function d($data){

        echo '<pre>';

        var_dump($data);

        echo '</pre>';

    }

?>

 

html:

    

    <p>姓名:<input type="text" name="name"/><button id="btn_look">查询</button></p>

    <div style="color:red;font-size:13px;" id="nrl"></div>



<script>



$('#btn_look').on('click',function(){

    var name = $("input[name='name']").val();

    $.ajax({

        type: "POST",

        url: "./api.php",

        async:false,

        data:{

            apiname: 'seach',

            name: name,

        },

        success: function(data) {

            //alert(data);

            var str = eval('('+ data +')');

            for(var i in str){

                $('#nrl') .html(str[i].body)

                //alert(str[i].body);

                

            }

            //alert();

        }

    });

})





</script>

 

你可能感兴趣的:(数据库)