php mysql怎么查询数据库

php mysql怎么查询数据库,第1张

    //1.连接数据库

    $link = @mysql_connect('localhost','root','123456')

    //2.判断是否连接成功

    if(!$link) exit('数据库连接失败')

    //3.选择数据库

    mysql_select_db('mydatabase')

    //4.设置字符集  utf8

    mysql_set_charset('utf8')

    //5.准备一个SQL语句

    $sql = 'select * from user'

    //6.发送SQL语句

    $result = mysql_query($sql)

    //7.判断并处理返回结果

    if($result){

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

            $list[] = $row

        }

        echo "<pre>"

            print_r($list)

        echo "</pre>"

    }

    //8.释放资源

    mysql_free_result($result)   //查询 *** 作才需要释放结果集

    mysql_close()

可以添加一个并且条件,例如myid是xxx,那么SQL语句如下:

$sql = "select * from ... where (现在的所有条件在这里并在其外添加括号) AND myid!=xxx"

<?php

    $key=isset($_POST['key'])?$_POST['key']:''

    if ($key!=''){

        mysql_connect('127.0.0.1','root','123456')//本行需要自行修改

        $sql="select * from ddb.user where name like '%$key%'"

        $res=mysql_query($sql)

        echo "<table><tr><th>id<th>name<th>age<th>sex"

        while ($row=mysql_fetch_array($res))

            echo "<tr><td>$row[id]<td>$row[name]<td>$row[age]<td>$row[sex]"

        echo "</table>"

        mysql_free_result($res)

        mysql_close()

    }

    echo <<<END

<form method=post>

姓名:<input type=text name=key value='$key'> <input type=submit value='搜索'>

</form>

END

?>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9680181.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存