The MU forums have moved to WordPress.org

Array_key_exists always returns false (8 posts)

  1. pro_sephiroth
    Member
    Posted 15 years ago #

    Hi folks

    I've been making a plugin for wordpress however it required me to edit the main wordpress files, i have a simple query running and then a check to see if the array key exists, if it does then it needs to perform an update, otherwise write the new data to the custom table.

    $currentpub = $wpdb->get_results("SELECT blog_id FROM ".$pre."currentpub", ARRAY_A);
    if (array_key_exists('$id', $currentpub))

    That is the code i'm using, none of the people I work with can figure out why it's not finding the blog_id in our custom table, we've checked it's getting the right data and it is, $id in this case is the blog id, and we've tried it with and without quotes.

  2. tmoorewp
    Member
    Posted 15 years ago #

    Your problem is probably in your SQL statement.

    You are using FROM " . $pre . "currentpub".

    You probably want FROM " . $wpdb->prefix . "currentpub.

    Unless you have set $pre somewhere else in your code, the $wpdb->get_results isn't seeing the right table.

  3. pro_sephiroth
    Member
    Posted 15 years ago #

    Oops, should have included that, $pre is this

    $pre = str_replace($id."_", "", $wpdb->prefix);

    simply replaces the id in the prefix, since we're using MU it's finding wp_7_ as the prefix for example, $pre just contains a sanitised one that has "wp_"

  4. tmoorewp
    Member
    Posted 15 years ago #

    Is $id getting set to the correct value?

    Also, you use $id again in the array_key_exists. It's possible that $id isn't right in either the SQL or the function.

  5. pro_sephiroth
    Member
    Posted 15 years ago #

    Yeah, I var_dumped all values, the blog_id comes out as 2, and $id is 2, they're both alike

  6. pro_sephiroth
    Member
    Posted 15 years ago #

    Just tried using in_array, same problem

  7. pro_sephiroth
    Member
    Posted 15 years ago #

    I've got it sorted now, got rid of all in_array and array_key_exists stuff, just looping through now, if the id matches the blog id it increments a counter, if the counter is more than 0, it runs the update script rather than the add script.

  8. DeannaS
    Member
    Posted 15 years ago #

    wpdb->get_results is going to return an array of arrays. array_key_exists and in_array don't work recursively. If you loop through the results and do one of those functions, you should then get a result and you should be able to break out of the loop as soon as you find it.

About this Topic

  • Started 15 years ago by pro_sephiroth
  • Latest reply from DeannaS