3

I was looking in the php manual and found two functions that look very similar to me:

mysqli_result::$field_count or mysqli_num_fields  

and

mysqli::$field_count or mysqli_field_count()  

Do they differ in any way?

Deepend
  • 4,057
  • 17
  • 60
  • 101
PhpLou
  • 430
  • 3
  • 16
  • I had already read the documentation but couldn't find any differences – PhpLou Aug 17 '14 at 15:50
  • Apparently there's also a difference between [`mysqli::query`](http://php.net/manual/en/mysqli.query.php) and [`mysqli::real_query`](http://php.net/manual/en/mysqli.real-query.php), which may be relevant, but the manual is oddly silent on what is actually "real" about `real_query` – IMSoP Aug 17 '14 at 15:56
  • @IMSoP, take a look here: http://stackoverflow.com/a/13469713/1232526 – Noy Aug 17 '14 at 16:03

2 Answers2

2

Those two functions accept different parameters:

  • mysqli_num_fields() accepts a $con variable - the mysql connection variable - and returns the number of rows for the latest query preformed using this connection.
  • mysqli_field_count()' accepts a$result` variable - the result set of any previous query - and returns its number of returned row.

You could presumably use mysqli_field_count() for your latest query, but you could never use mysqli_num_fields() for any query but your most recent one.

Noy
  • 1,258
  • 2
  • 11
  • 28
  • But I guess the question is, why did the designers of mysqli feel the need to include `mysqli_num_fields`? What is its advantage over querying a particular result set? – IMSoP Aug 17 '14 at 16:11
  • 1
    I think that `mysqli_num_fields()` result is already in memory, cached per connection, so it should be faster compared to `mysqli_field_count()`. – noun Aug 17 '14 at 16:19
  • This seems like a valid hypothesis. – Noy Aug 17 '14 at 16:20
1

this may help:

Object oriented style
int mysqli->field_count ;

Procedural style
int mysqli_field_count(mysqli link);

Returns the number of columns for the most recent query on the connection represented by the link parameter. This function can be useful when using the mysqli_store_result function to determine if the query should have produced a non-empty result set or not without knowing the nature of the query.

for more detail visit here:http://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.field-count.html

Object oriented style
int mysqli_result->field_count ;

Procedural style
int mysqli_num_fields(mysqli_result result);
Returns the number of fields from specified result set.

for more detail visit here:http://dev.mysql.com/doc/apis-php/en/apis-php-mysqli-result.field-count.html

you can see in both the cases one receives a input but the other does not

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
  • thanks I would upvote your answer if I had enough reputation (It's my first question) I see that mysqli->field_count does not require having to know the type of the query – PhpLou Aug 17 '14 at 16:23