gpt4 book ai didi

php - 为什么这个 PHP mysql_query 没有从我的数据库中获取正确的信息?

转载 作者:搜寻专家 更新时间:2023-10-30 21:47:31 24 4
gpt4 key购买 nike

我对这个比较陌生,如果发错地方了,请见谅。我的数据库中有一个表,其中包含 4 个内容 - ID、输入的时间以及用户和消息(对于基本的聊天室样式)。我正在尝试使用 PHP 页面调用它们。但是当我在地址栏中输入一个参数时,我的页面只是返回空白。我正在尝试调用它,以便只显示用户列中特定用户的行。我的代码在下面 - 谁能看到我做错了什么?该文件的 URL 是 http://freedom-apps.co.uk/chat/messages.php .您会看到大量条目 - 在开始时,有 u m u m u m。 U代表用户输入,m是消息。所以我尝试了: http://freedom-apps.co.uk/chat/messages.php?user=u返回错误。有任何想法吗?我的代码是:

<?php
header( 'Content-type: text/xml' );
mysql_connect('localhost:/var/lib/mysql/mysql.sock',
'freedom_ASAPPS', '********');
mysql_select_db( 'freedom_chat_alpha' );
if ( $_REQUEST['user'] ) {
$user = mysql_escape_string($_REQUEST['user']);
$result = mysql_query('SELECT * FROM chatitems WHERE user = '$user'
ORDER BY added LIMIT 50');
} else {
$result = mysql_query('SELECT * FROM chatitems ORDER BY added LIMIT 50');
}
?>
<chat>
<?php
while ($row = mysql_fetch_assoc($result)) {
?>
<message added="<?php echo( $row['added'] ) ?>"
id="<?php echo( $row['id'] ) ?>">
<user><?php echo( htmlentities( $row['user'] ) ) ?></user>
<text><?php echo( htmlentities( $row['message'] ) ) ?></text>
</message>
<?php
}
var_dump($result);
mysql_free_result($result);
?>
</chat>

我认为第 6 行和第 7 行有问题,或者我用参数调用 php 页面的方式有问题(见上文)。我一直在引用我用来学习这些东西的书,但仍然无法弄清楚(Learning PHP, MySQL, and JavaScript: A Step-by-Step Guide to Creating Dynamic Websites)。

谢谢,山姆

最佳答案

$result = mysql_query('SELECT * FROM chatitems WHERE user = '$user' ORDER BY added LIMIT 50');

这是一个语法错误 - 您缺少字符串连接运算符:

$result = mysql_query('SELECT * FROM chatitems WHERE user = ' . $user . ' ORDER BY added LIMIT 50');
^^^------^^^^--- here

或者,使用双引号字符串:

$result = mysql_query("SELECT * FROM chatitems WHERE user = '$user' ORDER BY added LIMIT 50");

获得空白页表明您在 php.ini 中关闭了 display_errors。你应该在调试/开发时打开它。您只是搬起石头砸自己的脚,隐藏了本可以告诉您问题出在哪里的消息。

关于php - 为什么这个 PHP mysql_query 没有从我的数据库中获取正确的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11485743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com