【问题标题】:PHP not printing result of MySQL queryPHP不打印MySQL查询的结果
【发布时间】:2013-04-03 18:04:37
【问题描述】:

我正在使用 PHP/MySql 来显示来自数据库的一些结果。

代码

$query = "select *  from users where (fname like '%".$searchTerm."%') OR (lname like '%".$searchTerm."%')";
$result = $db->query($query);

echo "yoooo";
$num_rows = $result->num_rows;

echo "<br/>".$num_rows." results st 2";
for ($i=0; $i<$num_rows ; $i++) {
    $row = $result->fetch_assoc();
    $fn = $row['fname'];
    $ln=$row['lname'];
    echo "<br/>".stripslahes($fn)." ".stripslashes($ln);
}

这表明:

yoooo
1 results st 2

但仅此而已……为什么?我确定我在关联数组中使用的名称是表中的列名...

【问题讨论】:

  • 您是否回复了您的查询?
  • 做一些基本的调试。 var_dump($row) 显示什么?然后去把你的 PHP 升级到稍微少一点石器时代并且不强制执行 magic_quotes 的东西。
  • 我敢打赌 stripslahes 函数不存在。请在行初始化后添加print_r($row, 1)(或var_dump)
  • 嗯 .. $fn = stripslashes($fn); $ln = 斜线($ln); echo "
    ".$fn." ".$ln;
  • 对于安全和效率问题,您最好了解Prepared Statements。一个很好的起点php.net/manual/en/mysqli.quickstart.prepared-statements.php

标签: php mysql web-applications


【解决方案1】:

我会尝试:

  • 开启error_reporting(E_ALL)
  • 回显查询+确保$searchTerm是安全的(避免sql注入)
  • var_dump($row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多