0

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?

Allenph
  • 1,875
  • 27
  • 46
  • 4
    `$check_emails_sent_result` is an object. What do you expect to be printed? – u_mulder Aug 06 '15 at 09:16
  • 1
    Mysqli->query() should return an object rather than a variable that you can echo out. From that object you can get the value of emails – Kickstart Aug 06 '15 at 09:16
  • A quick Google and a look through some of the PHP documentation didn't yield anything on mysqli objects, just how to use object-oriented mysqli. What do I request from the object in order to get my value? – Allenph Aug 06 '15 at 09:20

2 Answers2

1

mysqli_query()

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

You can get your email by using

if ($result = $connection->query($check_emails_sent_query)) {

    while ($row = $result->fetch_row()) {
        printf ("%s (%s)\n", $row[0]);
    }
}
Andrey Rudenko
  • 1,271
  • 20
  • 34
Saty
  • 22,443
  • 7
  • 33
  • 51
1

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

Andrey Rudenko
  • 1,271
  • 20
  • 34
Kickstart
  • 21,403
  • 2
  • 21
  • 33
  • Judging by the down-vote on my question, I should have stopped using deprecated functions and got on the MySQLi train a while ago. I found a "shorthand" version to get the single value I was looking for on another question...http://stackoverflow.com/questions/11456707/single-value-mysqli...I'm having hard time believing that the "improved" version of MySQL would make it such a pain to return single values. Am I mistaken? – Allenph Aug 06 '15 at 09:37
  • Personally I am not that keen on trying to turn it into shorthand as much as possible (beyond for amusement), so explicitly using extra lines of code would be fine with me. – Kickstart Aug 06 '15 at 09:51