如何使用php mysql访问表中的数据? [英] How to access data from table using php mysql?
问题描述
实际上我的数据库已连接,但是当我尝试从中访问数据时,我没有得到任何结果$result=mysqli_query($conn,$sql)
这不起作用并给我错误消息 'Table 'test.user' does not exist'
但我已经创建一个名为 test
的表,您可以查看附加的屏幕截图.但是当我在控制台中编写相同的查询时,它给了我想要的输出.我的代码如下
Actually my database is connected but when I try to access data from it, I m not getting any result
$result=mysqli_query($conn,$sql)
this is not working and giving me error message 'Table 'test.user' doesn't exist'
but I have created a table named test
you can check in screenshot attached.
But when I write same query in console it is giving me desired output. My code is as follow
<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "test";
$conn = mysqli_connect($db_host,$db_user,$db_password,$db_name);
if(!$conn){ echo "failed"; }
echo "success";
$sql = 'SELECT * FROM user';
$result = mysqli_query($conn,$sql);
$a = var_dump($conn);
$b = var_dump($result);
echo $a;
echo $b;
if(mysqli_num_rows($result)>0){
echo "data fetched";
}
else{
echo "data access failed.";
}
?>
它给出了这个错误消息 screenshot
It is giving this error msg screenshot
这是控制台屏幕截图
推荐答案
我已经在我的设备上测试了你的代码,这里一切正常.
I've tested your code on my device, and everything is working correctly here.
然后我试着打错字而不是输入 $sql = "SELECT * FROM user";
我输入了 $sql = "SELECT *FROM userr";
和猜猜是什么,我遇到了和你一样的错误.
Then i tried to make a typo instead of typing $sql = "SELECT * FROM user";
i've typed $sql = "SELECT * FROM userr";
and guess what, i had the same error you have.
所以您可能也打错了,请尝试检查您的数据库表名.
So you probably made a typo too, please try to check out your database table name.
这篇关于如何使用php mysql访问表中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!