简体   繁体   English

Mysql和PHP不会回显查询结果

[英]Mysql & PHP won't echo the results of the query

<?php

mysql_connect("localhost", "root", "usbw");
mysql_select_db("europese unie");

$id = $_GET['Land'];
$query = "SELECT `Land`, `Oppervlakte`, `Inwoners`, `Klimaat`, `Motto`, `UTC` FROM `algemene informatie` WHERE `Land` = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$land = $row[0];

echo "$land"; 



print("<h2>$id</h2>");
print("<br />");


die(mysql_error())

?>

when i tried to run this code i expected to echo the first land that came up with this query instead i got nothing. 当我尝试运行此代码时,我希望回显与此查询相关的第一条土地,但我一无所获。 i am not a very experienced PHP or Mysql user nor is english my first language so pleas do not hold annything against me. 我不是一个经验丰富的PHP或Mysql用户,英语不是我的第一语言,因此请不要对我不利。

Solved 解决了

Taken from comments: 摘自评论:

"well the adress is localhost:8080/details.php?Land=%20Belgie and $id = $_GET['Land']; is the id so Belgie should be $id but as var_dump($result) it keeps giving bool(false) " “那么地址是localhost:8080/details.php?Land=%20Belgie$id = $_GET['Land'];是id,因此Belgie应该是$id但作为var_dump($result)它一直在给bool(false)

  • Basically what you're asking SQL, is to find (space)Belgie and since your column doesn't contain the word Belgie with a space, it will not find it; 基本上,您要使用的SQL是查找(space)Belgie并且由于您的列中不包含带空格的Belgie单词,因此不会找到它。 least that's what I think is happening 至少那是我认为正在发生的事情

That %20 is a space btw. %20是一个空格。 That could be the problem. 那可能是问题所在。 You could make use of the trim() function. 您可以使用trim()函数。

Use: 采用:

$id = trim($_GET['Land']);

OP: OP:

thank you print_r($row); 谢谢print_r($ row); now gives Array ( [0] => Belgie [1] => 31 [2] => 11 [3] => gematigd zeeklimaat [4] => Eendracht maakt macht [5] => 1 ) i can finaly proceed 现在给出Array([0] => Belgie [1] => 31 [2] => 11 [3] => gematigd zeeklimaat [4] => Eendracht maakt macht [5] => 1),我可以结束


Sidenote: Your present code is open to SQL injection . 旁注:您当前的代码可以进行SQL注入 Use mysqli_* functions. 使用mysqli_*函数。 (which I recommend you use and with prepared statements , or PDO ) (我建议您将其与预处理语句PDO一起使用

mysql_* functions are deprecated and will be removed from future PHP releases. mysql_*函数已被弃用,并将从以后的PHP版本中删除。


Footnotes: 脚注:

mysql_* functions deprecation notice: mysql_*函数弃用通知:

http://www.php.net/manual/en/intro.mysql.php http://www.php.net/manual/zh/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. 自PHP 5.5.0起不推荐使用该扩展,不建议编写新代码,因为将来会删除该扩展。 Instead, either the mysqli or PDO_MySQL extension should be used. 相反,应使用mysqliPDO_MySQL扩展名。 See also the MySQL API Overview for further help while choosing a MySQL API. 另请参见MySQL API概述,以获取选择MySQL API时的更多帮助。

These functions allow you to access MySQL database servers. 这些功能使您可以访问MySQL数据库服务器。 More information about MySQL can be found at » http://www.mysql.com/ . 有关MySQL的更多信息,请参见» http://www.mysql.com/

Documentation for MySQL can be found at » http://dev.mysql.com/doc/ . 可以在» http://dev.mysql.com/doc/中找到MySQL的文档。

Do not use mysql_connect() this is going to become outdated, use mysqli_connect() . 不要使用mysql_connect()这将变得过时,请使用mysqli_connect()

Ie: 即:

$connection = new mysqli('host', 'username', 'password', 'db table', 'portnumber');

    if (mysqli_connect_errno()){
        printf("<b>Connection failed:</b> %s\n", mysqli_connect_error());
        exit;
    } 

run you query through mysqli object and remember to use injection prevention or you will get people hacking you site. 通过mysqli对象运行您的查询,并记住使用注入预防,否则您会被别人黑客入侵您的网站。

eg 例如

http://www.phphaven.com/article.php?id=65 http://www.phphaven.com/article.php?id=65

Hope that helps 希望能有所帮助

Quote out the variables. 引用变量。

echo $land;

print("<h2>" . $id . "</h2>");

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

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