Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array item count puzzle 2

Status
Not open for further replies.

StuartBombay

Programmer
Feb 11, 2008
56
US
I can't figure out why the array count shows 1. If I copy the resulting SQL into the MySQL database I get 2 rows of results, but php is returning a count of 1. More puzzling is the while loop returns 2 names.
I'm obviously not understanding something here!

Code:
			$get_name_sql = "SELECT fullname, locnum from v_program_details where prognum = '".$_POST["sel_id"]."'";
			$get_name_rs = mysqli_query($conn, $get_name_sql)
					or die(mysqli_error($conn));
			print $get_name_sql; echo "<br/>";
			$prog_count = count($get_name_rs);
			print $prog_count;
			while ($name_info = mysqli_fetch_array($get_name_rs)) {;
				$display_name = stripslashes($name_info['fullname']);
				$locnum = stripslashes($name_info['locnum']);
				echo "<br/>"; print $display_name;
			}

The results are-

SELECT fullname, locnum from v_program_details where prognum = '2254'
1
Rose Heights
Rose Heights

I need to know when I'm getting more than one record returned! I can put a count in the while loop, that works - but I think the count() should work too!
Thanks --
 
Can it be because arrays start at 0, 1, 2, 3, ... where 1 actually means 2?

Just shooting from the hip

--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
LEts see you are using count which is basically a function to count elements in an array, on a handle. count assumes its an array so it just sees a single element and returns one.

If you want the number of rows returned by your query use:

mysqli_num_rows



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
What about sizeof(), will this not work as well?

--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Ah! Thanks vacunita and southbeach! mysqli_num_rows($get_name_rs) returns exactly what's needed.
 
sizeof() is the same thing as count(), and since the variable is not an array, but a handle there is nothing to count in it.

A handle as used by the mysqli functions is similar to a pointer if you will, it doesn't actually contain the returned data from the queries, it only points to it.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita,

Thanks for the information, I found it very interesting.



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top