niche Posted November 16, 2009 Share Posted November 16, 2009 I successfully scripted an fetch array while loop on a table called example. What's the name of the function that will return the name of the array that was created by the fetch array while loop?What's the name of the function that will turn the number of rows in an array?Thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 Can you show example code? I'm not sure what you mean. If you want to count the number of elements in an array you can use the count function. Link to comment Share on other sites More sharing options...
chibineku Posted November 16, 2009 Share Posted November 16, 2009 When you fetch the array, you assign it to a variable, for example:$info = mysqli_fetch_array($query);So you then refer to the array by the name $info.To get the number of items in an array, you can use count($info). Link to comment Share on other sites More sharing options...
niche Posted November 16, 2009 Author Share Posted November 16, 2009 Thanks for your help. Here's my script: query = "SELECT * FROM example"; $result = mysql_query($query) or die(mysql_error());while($row = mysql_fetch_array($result)) { echo $row['name']. " - ". $row['age']; echo "<br/>";} Does mysql_fetch_ array create an array or did an array already exist?If so how do I get the name of the array?Also, how do I get other general info about it, or any other array (i.e. number of rows and columns, and column names)? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 $row = mysql_fetch_array($result)$row contains the array that mysql_fetch_array returned. The array doesn't really have a "name", it's just stored in the $row variable. Also, how do I get other general info about it, or any other array (i.e. number of rows and columns, and column names)?Arrays don't really have rows or columns, they just have fields or elements. You can think of an array as a single row, but you can also have an array of arrays where each item in the parent array would be an array that represents a row, which in turn has columns. But basically an array is composed of a series of elements, where each element has a key and a value. If you make an array like this:$ar = array('one', 'two', 'three');Then it creates numbered keys, so the keys for the three elements are 0, 1, and 2, and the values are what you see in quotes. You can also specify the keys yourself:$ar = array('name' => 'Joe', 'age' => 20);So instead of having numbered keys, that array has keys called "name" and "age", with the values you see.You can use array_keys to return an array of the key names in another array. Also check the array function reference:http://www.php.net/manual/en/ref.array.php Link to comment Share on other sites More sharing options...
niche Posted November 16, 2009 Author Share Posted November 16, 2009 I tested $row with is_array(): $ray = is_array($row);echo $ray; The above script didn't return a "1" so I manually created a simple array to check that I probably understood the is_array() function.That led to believe that the the script, below, didn't produce an array in $row, but you're telling me it did. while($row = mysql_fetch_array($result)){... What do you think?Then I have some more questions that I'd like to continue in a new topic called: Creating An Array From A mysql Table.Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 If your is_array line is below the while loop, then by the time the while loop ends $row will be set to false, not an array. mysql_fetch_assoc returns false when there are no more rows. It's only an array inside the body of the loop. Link to comment Share on other sites More sharing options...
niche Posted November 16, 2009 Author Share Posted November 16, 2009 That explains a lot!So, please confirm, that the array only exists inside the while loop as $row (in this example)?If so, how can I make it exist outside the loop so I can create some local variables? Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 You just need to save it in another variable, Dad gave the example in another thread of packing all of the rows into another array. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.