简体   繁体   English

PHP / MYSQLI:mysqli_query在PHP中失败

[英]PHP/MYSQLI: mysqli_query fails in PHP

First off, I'm a total noob in mysql(i) language and know a little more than the basics of PHP. 首先,我对mysql(i)语言一无所知,并且比PHP的基础知识了解更多。

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. 但是,我可以访问phpMyAdmin页面。

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. 现在,我正在尝试制作一个链接到数据库的登录页面。

Now, behold the code: 现在,看一下代码:

$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! :-(";
}

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. 但是,当我在phpMyAdmin的“ SQL”选项卡上输入相同的查询时,得到了想要的结果。 How come the webpage gives me an error, while the phpMyAdmin gives me the results, and, how do I solve it? 网页为什么会给我一个错误,而phpMyAdmin却给了我结果,我该如何解决呢?

Use correct syntax: 使用正确的语法:

$result = mysqli_query($con, "SELECT * FROM my_tablename");

You forgot to link current mysqli connection. 您忘记了链接当前的mysqli连接。 First parameter is link - which mysqli connection you want to use (good for multiple conns) and then the second is your query. 第一个参数是link-您要使用哪个mysqli连接(适用于多个conn),第二个参数是您的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM