php查询数据库

php查询数据库,第1张

mysqli有两种数据库连接方式:

1、面向过程式连接:

mysqli_connect('localhost','xxx','xxx','xxx');

mysqli_query('');

后使用mysqli_fetch_assoc方法获取到数据。

2、面向对象式连接:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$result = $mysqli->query('');

后使用$result->fetch_assoc()获取数据。

至于num_rows是获取查询到的行数的方法。

<

$conn = @ mysql_connect("localhost", "root", "root") or die("数据库链接错误");

mysql_select_db("news", $conn);

mysql_query("set names 'GBK'"); //使用GBK中文编码;$sql="select info from message";

$query=mysql_query($sql);

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

>

echo $row[title];

>

php使用mysql查询数据库已经有多少条数据使用sql的count函数实现。

示例代码如下:

<php

//数据库连接

$conn=mysql_connect("localhost","root","root");

if(!$conn){

die("对不起,数据库连接失败! ")mysql_errno();

}

//选择数据库

mysql_select_db("testdb");

//sql语句

$sql="SELECT COUNT() AS count FROM user";

//执行sql

$query=mysql_query($sql,$conn);

//对结果进行判断

if(mysql_num_rows( $query)){

$rs=mysql_fetch_array($query);

//统计结果

$count=$rs[0];

你说的是数据库,还是数据表啊。

如果是数据表的话,可以用连接查询。

select aid,ausername,bid as bid btitle from 表A a left join 表B b on aid=bid

这样就可以了,如果是两个数据库,那就比较麻烦,要创建两个连接对象。再分别打开两个表。再 *** 作。

以上就是关于php查询数据库全部的内容,包括:php查询数据库、PHP和MYSQL数据库连接显示查询内容、PHP连接数据库查询条数的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存