Maybe a dumb question, but I'm new to PHP and have found myself stuck. I'm trying to use the results from a mysql_fetch_array at another location in my script. I've tried to
do an array_pop on the results from mysql_fetch_array with no luck. I worked around it by creating an array and pushing the the results of the mysql_fetch_array to it. I was then able to use array_pop elsewhere in my sript on my newly created array to get the information. Why didn't array_pop not work with the results of mysql_fetch_array?

This is my sql query and how I got it to work...

$default = @mysql_query("SELECT DISTINCT name FROM images");  //get unique names
 while ($uname = @mysql_fetch_array($default))  {
 $uidx=$uname["name"];    
 array_push ($stuff, $uidx);
 }

In a separate loop I was able to get the results by doing this

$uid = array_pop ($stuff);

My goal is to get all the unique names from the name column of my table and set them as variables for obtaining specific information about each unique name. The problem is that I never know how many unique results I will get... nor do I have any idea what the results are going to be..... SO I have to pick the results off and handle them one at a time.

HELP! Thanks 🙂

    // Start an array for images:
    $images = array();
    
    // Build the query:
    $query = "SELECT DISTINCT name FROM images";
    
    // Run the query:
    $result = mysql_query($query);
    
    // Ascertain your result handle:
    if(!$result) {
    	echo "Query $query failed: " . mysql_error();
    } else {
    	// Verify the result handle actually has rows of data:
    	if(!mysql_num_rows($result)) {
    		echo "No records found for query $query.";
    	} else {
    		// Since you're only selecting a single column, just use fetch_row():
    		while($row = mysql_fetch_row($result)) {
    			// Stick element 0 from the fetch_row array into the images array:
    			$images[] = $row[0];
    		}
    
    	// Free up the resources associated with the result:
    	mysql_free_result($result);
    }
    }
    
    // Now you have an indexed array of images, called $images...
    // Count it, for example:
    $n = count($images);
    echo "There are $n images in the images array.";
    
    // Loop through it if you'd like:
    for($i = 0; $i < $n; $i++) {
    	echo $images[$i] . '<br />';
    }
    
    

    Not too tough, eh?

      Thanks,

      Makes more sence than the way I was doing it. Instead of
      counting the number of entries in the result array, I've been issuing a mysql_num_rows() on my query. Will this work fine or should I be counting my results instead?

      Chris

        Write a Reply...