How to debug a Mysql query?

Asked

Viewed 191 times

0

Is there any way to know what’s wrong with this QUERY?

When you perform a query, it displays it on the screen but nothing appears, the var_dump returns NULL, what steps a professional takes to know what’s wrong with a consultation? (I’m new to back end)

$query = $wpdb->get_results( "SELECT display_name, post_title, name

FROM $wpdb->terms 

INNER JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id 

INNER JOIN $wpdb->term_relationships ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_id

INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->posts.post_type = 'noo_resume' 

INNER JOIN $wpdb->users ON $wpdb->users.ID = $wpdb->posts.post_author 

INNER JOIN $wpdb->usermeta ON $wpdb->usermeta.user_id = $wpdb->users.ID AND $wpdb->usermeta.meta_key = '_jm_candidate_field_clocknow_user_btn' AND $wpdb->usermeta.meta_value = 'value_2';" );

foreach ($query as $vaga) {                 
  echo 'Nome: ' .$vaga->display_name. '<br>';
  echo 'Currículo: ' .$vaga->post_title. '<br>';
  echo 'Cargo: ' .$vaga->name. '<br>';
  echo '<hr>';
}

Relational Model of the Table

inserir a descrição da imagem aqui


  • 2

    Look at the last line, meta_key and meta_value pretencem to which table? You use phpMyAdmin or similar?

  • 1

    Oi Rodrigo, I would start investigating the Wordpress database itself, from the most internal to the most external table... See if there are entries in wp_usermeta where meta_key = '_jm_candidate_field_clocknow_user_btn' AND meta_value = 'value_2' (if that’s what you want). Then do the Join to users and see if you can return user ids... So do Join with posts, etc, until you have everything you need.

  • Thiago, I fixed the query... meta_key and meta_value belong to table wp_usermeta ($wpdb->usermeta). But you still have the same problem.

  • Anthony, yes you have entries in wp_usermeta and meta_key with these values. Look, I can even return all the data, but for the query to be right even there have to be these 2 conditions $wpdb->usermeta.meta_key = '_jm_candidate_field_clocknow_user_btn' AND $wpdb->usermeta.meta_value = 'value_2'. As simple as it seems I’m not managing to implement it in the query.

  • 1

    So the question is whether the joins are correct and whether there is data connecting all the tables. That’s why the plan is to test one Join at a time. Start with SELECT * FROM $wpdb->usermeta WHERE $wpdb->usermeta.meta_key = '_jm_candidate_field_clocknow_user_btn' AND $wpdb->usermeta.meta_value = 'value_2'; and build from there.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.