0

I could use some assistance with an sql query. I have a small 3 column table, id, ip, and birthday. id auto increments.

I'm trying to select all birthdays that are associated with a specific ip, but I'm not sure if the SQL statement I wrote is correct. The var_dump returns an empty array. If someone could check this for me it would be appreciated.

global $wpdb;

$query = "SELECT birthday FROM $table WHERE ip=$ip";
$results = $wpdb->get_results($query, ARRAY_A);

var_dump($results);
4
  • 1
    Do an echo $query; and add that to your question Commented Jun 19, 2015 at 18:49
  • @Machavity I just tested echo $query. The correct table name and a valid ip found in the table was echo'd. Commented Jun 19, 2015 at 18:52
  • Is it as simple as missing single quotes around the IP? i.e. WHERE ip='$ip' Commented Jun 19, 2015 at 19:41
  • @nickyb Just humor me here. I'm not asking just for idle curiosity. Commented Jun 19, 2015 at 20:08

1 Answer 1

1

wrap ip address passed to mysql with single quotes, use actual table name. if your resulting array is blank, then you are missing that ip from your table.

Sign up to request clarification or add additional context in comments.

23 Comments

I'm using wordpress so I just called the global inside the function I'm writing making the call. global $wpdb; I've used this before without any problems, but I've always used get_row to fetch a single row. The function is written in functions.php.
Thanks, but your suggestion gets parsed exactly the same as what I already have. I just embedded the variables in double quotes instead of concatenating.
can you paste here what your final string looks like value-wise, and describe what is happening or not. also explain your error handling
I don't want to paste my table name or ip, but that's what gets parsed. So the correct table name and my ip which can be found in the table is passed through as SQL. So $results should be an array of all the birthdays selected in the table associated with my IP. $results = $wpdb->get_results($query); The var_dump should show all the birthdays I have in the table with my IP as ip, which at the moment is 8 in total. But instead of dumping an array with 8 dates, $results is empty.
the whole concept is funny. i identify the user by ipaddress and get their birthday !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.