无法将有效变量传递给查询

发布于 2024-12-12 01:06:34 字数 801 浏览 0 评论 0原文

我正在将变量传递到我的页面 page?title=fred 但如果它的值错误(不在数据库中),我想重定向到另一个页面,例如:fred


第一部分检查变量是否为空。这似乎有效。

//get variable from title
if(!isset($_GET['title']) || empty($_GET['title'])) {
    echo "no variable";
} 
else {
    $get_title = mysqli_real_escape_string($db, $_GET['title']);
    $title = str_replace("-"," ", $get_title);
} 


我无法检查我传递的变量是否在数据库中以及是否重定向到主页。

$query = mysqli_query($db, "SELECT title FROM persons WHERE title = '$title'");
//check that variable is in the database
if(!isset($query)){
  while($row = mysqli_fetch_assoc($query)){

    echo $row['title'];

  }
// mysqli_free_result($query);
} 
else {echo "variable does not exist";} ?>


有人可以告诉我我做错了什么以及如何解决它吗?谢谢你,

I'm passing variables to my page page?title=fred but I would like to redirect to another page if it's the wrong value(not in the database) ex: fredd

The first part checks if the variable is empty. Which seems to be working.

//get variable from title
if(!isset($_GET['title']) || empty($_GET['title'])) {
    echo "no variable";
} 
else {
    $get_title = mysqli_real_escape_string($db, $_GET['title']);
    $title = str_replace("-"," ", $get_title);
} 

I'm having trouble checking if the variable I pass is in the database and if it's not to redirect to main page.

$query = mysqli_query($db, "SELECT title FROM persons WHERE title = '$title'");
//check that variable is in the database
if(!isset($query)){
  while($row = mysqli_fetch_assoc($query)){

    echo $row['title'];

  }
// mysqli_free_result($query);
} 
else {echo "variable does not exist";} ?>

Can someone please show me what I am doing wrong and how to fix it? Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢好甜 2024-12-19 01:06:34
if(!isset($query)){

$query 始终被设置(您只需设置它),因此您想要的代码永远不会执行。

你想要这个:

if (!$query) {
  echo "Query returned error";
} else if (mysqli_num_rows($query) < 1) {
  echo "Variable does not exist";
} else {
  while ($row = mysqli_fetch_assoc($query)){
    echo $row['title'];
  }
}
if(!isset($query)){

$query is always set (you just set it) so the code you want will never execute.

You want this instead:

if (!$query) {
  echo "Query returned error";
} else if (mysqli_num_rows($query) < 1) {
  echo "Variable does not exist";
} else {
  while ($row = mysqli_fetch_assoc($query)){
    echo $row['title'];
  }
}
倚栏听风 2024-12-19 01:06:34

isset($query) 将始终为 true - mysqli_query() 将始终返回某些内容,即使它只是一个布尔假值。

$result = mysqli_query(..);
if ($result === false) {
    die(mysqli_error());
}
if (mysqli_num_rows($result) == 0) {
   echo "variable does not exist";
} else {
   while(...) {
      ...
   }
}

isset($query) will ALWAYS be true - mysqli_query() will always returning SOMETHING, even if it's just a boolean false value.

$result = mysqli_query(..);
if ($result === false) {
    die(mysqli_error());
}
if (mysqli_num_rows($result) == 0) {
   echo "variable does not exist";
} else {
   while(...) {
      ...
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文