PHP/MYSQLI:mysqli_query在PHP中失败 [英] PHP/MYSQLI: mysqli_query fails in PHP
问题描述
首先,我对mysql(i)语言一无所知,并且比PHP的基础知识了解更多.
First off, I'm a total noob in mysql(i) language and know a little more than the basics of PHP.
注意:我不管理或拥有/不能访问当前托管该网页的服务器.但是,我可以访问phpMyAdmin页面.
Note: I do not manage or own / have access to the server on which the webpage currently is hosted. I can however access the phpMyAdmin page.
也就是说,我有一个网页正在尝试一些东西.现在,我正在尝试制作一个链接到数据库的登录页面.
That said, I've got a webpage on which I am trying out some stuff. Right now I'm trying to make a log-in page linked to a database.
现在,看一下代码:
$mysql_host = "localhost";
$mysql_user = "my_user";
$mysql_database = "my_database";
$mysql_password = "my_password";
$table = "my_tablename";
// Create connection
$con=mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// sending query
$result = mysqli_query("SELECT * FROM my_tablename");
if (!$result) {
echo "Query to show fields from table failed! :-(";
}
现在,这是实际的问题了.启动页面后,它将立即提示我查询以显示表中的字段失败!"错误信息.但是,当我在phpMyAdmin的"SQL"选项卡上输入相同的查询时,得到了想要的结果.网页为什么会给我一个错误,而phpMyAdmin却给了我结果,我该如何解决呢?
Now, here comes the actual problem. As soon as I launch the page it will give me my "Query to show fields from table failed!" error message. But when I enter the same query on the phpMyAdmin 'SQL' tab, I get the wanted results. How come the webpage gives me an error, while the phpMyAdmin gives me the results, and, how do I solve it?
推荐答案
使用正确的语法:
$result = mysqli_query($con, "SELECT * FROM my_tablename");
您忘记了链接当前的mysqli连接.第一个参数是link-您要使用哪个mysqli连接(适用于多个conn),第二个参数是您的查询.
You forgot to link current mysqli connection. First parameter is link - which mysqli connection you want to use (good for multiple conns) and then the second is your query.
这篇关于PHP/MYSQLI:mysqli_query在PHP中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!