Jump to content

Basic Array Question


niche
 Share

Recommended Posts

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

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

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

$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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...