doupai5450 2015-08-06 09:13
浏览 7
已采纳

简单的MySQL调用选择特定键的整数不起作用?

I have this PHP:

    <?php

    $client_ip = $_SERVER['REMOTE_ADDR'];
    $connection = new mysqli("localhost", "MyNotSoSecretUsername", "MySuperSecretPassword", "MyNotSoSecretDatabaseName");

    if ($connection->connect_error) {
         die("Connection failed: " . $Connection->connect_error);
    }

    $check_emails_sent_query = "SELECT `emails` FROM `email-ips` WHERE `ip`='11.111.111.111'";
    $check_emails_sent_result = $connection->query($check_emails_sent_query);

    echo $check_emails_sent_result;

    ?>

This is a small piece of a much larger function on my site. This snippet is simply intended to get the value of the "emails" column (Which is an int column if that makes a difference) of my table where the IP matches the client's IP.

I added a fake entry for 11.111.111.111 in my database, and used the exact same query on PHPmyAdmin's SQL console. I got a result on the PHPmyAdmin console, but nothing is echoed here.

I have also checked that the connection is good, as you can see in my code. Additionally, I pasted another query from another part of my function, which retrieved its data just fine.

AS stupid or obvious as it may be, I can't seem to figure out why this particular query out of almost twenty won't retrieve its data?

  • 写回答

2条回答 默认 最新

  • dongxiaoxing3058 2015-08-06 09:28
    关注

    Mysqli query() returns and object. Using the object:

    <?php
    
    $client_ip = $_SERVER['REMOTE_ADDR'];
    $connection = new mysqli("localhost", "MyNotSoSecretUsername", "MySuperSecretPassword", "MyNotSoSecretDatabaseName");
    
    if ($connection->connect_error) 
    {
        die("Connection failed: " . $Connection->connect_error);
    }
    
    $check_emails_sent_query = "SELECT `emails` FROM `email-ips` WHERE `ip`='11.111.111.111'";
    
    if ($check_emails_sent_result = $connection->query($check_emails_sent_query)) 
    { 
        while($obj = $check_emails_sent_result->fetch_object())
        { 
            $echo $obj->emails; 
        } 
    } 
    
    ?>
    

    You could use fetch_row() instead of fetch_object().

    Documentation for the object is here:

    http://php.net/manual/en/class.mysqli-result.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类