Typel
Typel

Reputation: 1139

stmt bind_result() doesn't bind all columns

I've got a simple query that I'm trying to bind to three variables in a fetch loop. Unfortunately, it picks up the first two columns and leaves the 3rd as an empty string... I'm at a loss... any ideas?

 $db = new mysqli("host.com", "username", "password", "db_name");

 $q = "SELECT user_id, meta_key, meta_value FROM wp_usermeta WHERE (meta_key='first_name' OR meta_key='last_name') AND meta_value != '';";
 $stmt = $db->prepare($q);
 $stmt->execute();
 $stmt->bind_result($col1,$col2,$col3);
 while ( $stmt->fetch() ) {
      echo $col1." - ".$col2." - ".$col3."<br />";
      }

Output:

 2 - first_name - 
 2 - last_name - 
 5 - last_name - 
 5 - first_name - 
 6 - first_name - 
 6 - last_name - 
 8 - last_name - 
 8 - first_name - 
 9 - first_name - 
 9 - last_name - 

Upvotes: 1

Views: 771

Answers (1)

Typel
Typel

Reputation: 1139

Finally figured this out. The meta_value field is a longtext data type. Apparently, if you try to bind a longtext field before using store_result, PHP returns an empty string without giving any sort of error or warning to that effect.

Here is the working code:

 $db = new mysqli("host.com", "username", "password", "db_name");

 $q = "SELECT user_id, meta_key, meta_value FROM wp_usermeta WHERE (meta_key='first_name' OR meta_key='last_name') AND meta_value != '';";
 $stmt = $db->prepare($q);
 $stmt->execute();
 $stmt->store_result();
 $stmt->bind_result($col1,$col2,$col3);
 while ( $stmt->fetch() ) {
      echo $col1." - ".$col2." - ".$col3."<br />";
      }

Upvotes: 2

Related Questions