dounao5856 2014-05-06 14:10
浏览 23
已采纳

选择语句不正确检查num_rows

I am making a simple PHP and MySQL IP banning script and I am at the stage of checking to see if the IP address is already banned. I have created a test file called test.php and this is its contents:

<?php
require_once('../includes/db_connect.php');
session_start();

$ip = '1.1.1';
$stmt = $db->prepare("SELECT `ip`, `reason` FROM `banned_ips` WHERE `ip` = ?");
$stmt->bind_param('s', $ip);
$stmt->execute();

if ($stmt->num_rows > 0) {
    header('Location: banned.php');
}

$stmt->close();

 ?>

 <html>
 <h1>This is a test page</h1>
 </html>

As you can see I have set the $ip variable equal to 1.1.1 which is a test value that does exist in the database, therefore should be seen as banned, however it doesn't see it as being banned and therefore doesn't redirect to the banned page.

Am I doing something wrong when checking to see if the IP address already exists in the database?

My database structure is as follows:

Table: banned_ips column 1: id Column 2: ip column 3: reason

EDIT: Sorry for the typo, I do have an IP column and it is a varchar.

  • 写回答

1条回答 默认 最新

  • douju4594 2014-05-06 14:29
    关注

    This might get it to work: (if you have ip column as a varchar in the db)

    <?php
    require_once('../includes/db_connect.php');
    session_start();
    
    $ip = '1.1.1';
    $stmt = $db->prepare("SELECT `ip`, `reason` FROM `banned_ips` WHERE `ip` = ?");
    $stmt->bind_param('s', $ip);
    $stmt->execute();
    
    //Transfers a result set from the last query
    $stmt->store_result(); 
    
    if ($stmt->num_rows > 0) {
        header('Location: banned.php');
    }
    
    $stmt->close();
    
     ?>
    
     <html>
     <h1>This is a test page</h1>
     </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭