1

I've created a table to use within my Wordpress site, with columns using date, int and varchar as the type.

However, when I use var_dump to get the values of particular rows and results, it displays all the columns as string.

I haven't seen this actually affect the operation of the DB calls and functions, but I'm just wondering why they're all returning as strings, where I've seen core Wordpress functions in var_dump returning int values sometimes.

enter image description here

$bv_set = $wpdb->get_row('SELECT * FROM wp_before_after WHERE ID = '.$bv_id );

var_dump($bv_set);

C:\wamp64\www\bellavou\wp-content\plugins\bv-before-afters\views\edit.php:8:
object(stdClass)[6915]
  public 'ID' => string '1' (length=1)
  public 'created' => string '0000-00-00' (length=10)
  public 'before_date' => string '2015-04-08' (length=10)
  public 'after_date' => string '2015-04-21' (length=10)
  public 'patientID' => string '2137' (length=4)
  public 'procedureID' => string '238' (length=3)
  public 'patient_display' => string '1' (length=1)
  public 'procedure_display' => string '1' (length=1)
  public 'gallery_display' => string '1' (length=1)
  public 'before_img' => string '2200' (length=4)
  public 'after_img' => string '2199' (length=4)
  public 'period_taken' => string '1week' (length=5)
  public 'notes' => string '' (length=0)
6
  • 2
    it would be helpful to see the code how you retreive the values. Commented Oct 18, 2016 at 8:23
  • Yes, of course. Added the call to the DB, and the result of the var_dump, sorry. Commented Oct 18, 2016 at 8:26
  • whats in get_row? Assuming this is a wrapper method for executing the query/getting the result? Commented Oct 18, 2016 at 8:27
  • 1
    maybe this might answer some questions Commented Oct 18, 2016 at 8:29
  • You can see the result of the get_row call in the var_dump Commented Oct 18, 2016 at 8:29

2 Answers 2

2

Reading through the code of $wpdb->get_row, you can see that it eventually calls $wpdb->query, which in turn calls mysqli_fetch_object (line 1822 at time of writing):

while ( $row = mysqli_fetch_object( $this->result ) ) {
    $this->last_result[$num_rows] = $row;
    $num_rows++;
}

Now, taking a look at the PHP documentation for mysqli_fetch_object:

Returns an object with string properties that corresponds to the fetched row or NULL if there are no more rows in resultset.

Which provides an answer to the question.

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

3 Comments

Is there a particular reason for doing this? Given that it doesn't seem to affect the operation of working with the DB row, is there a upside of returning everything as string?
@Lee that's a question for the developers of the PHP language itself.
You don't need to ask developers of PHP language anything because the decision is clear to the one who asks the right question. MySQL uses a text protocol to send data back to PHP. PHP doesn't know how to represent the data so it does the safest thing - considers everything a string. There's a library (that I mentioned in another comment) called mysqlnd which "fixes" this behavior. All it takes is a bit of research before coming to wrong conclusions.
0

Highly depends on the mysql connector and it's type castings based on the DB field options like "null" values. Might also be related to some notorious php type castings (see https://www.sitepoint.com/3-strange-php-facts-you-may-not-know/ )

2 Comments

if you code PHP badly, then yes those things on sitepoint might happen.
Answer was before the author of the question added screenshot + var_dump example :) - now its clear that he uses the wpdb class to query the data

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.