Jump to content

Help with ARRAYS (mysql_fetch_array())


Wildhalf

Recommended Posts

I am using the following code to return all entries into my database with the letters begining with $letter which is taken from my URL

 

$sql_1 = mysql_query("SELECT * FROM tbl_offers WHERE business_name LIKE '$letter%'");
$data_1 = mysql_fetch_array($sql_1 );

 

It works but what i want todo is print all returned values for offer, example below prints one but i want to print all entries.

 

$offer = $data_1["offer"];
print "</BR>offer : $offer";

 

Anyone got any ideas???

 

Thanks in advance.

 

Kieron

Link to comment
https://forums.phpfreaks.com/topic/93380-help-with-arrays-mysql_fetch_array/
Share on other sites

Use a while loop

 

 

 

Oh yes...i kinda got confused...yes the while loop

 

add this to your code

 

//DELETE THIS LINE

$data_1 = mysql_fetch_array($sql_1 );

//-----------------------------------

 

Add this

-------

while ($data_1 = mysql_fetch_array($sql_1)) {

    print "$data_1[offer]<br>";

}

--------------

I believe im getting the letter fine using the following code.

 

// Create variables from URL.
$letter = $_REQUEST['letter'];
print "letter : $letter";

 

It prints the right letter anyways.

 

I'm returning and printing the first result of the field offer to screen. but want it to print all results.

 

How would i use a while loop with arrays??

Here's a general-purpose function to output query results

 

<?php
include 'db.php';    //connnection stuff

function table2table($query) {
    $result = mysql_query($query) or die (mysql_error());

    $str = "<TABLE border='1' cellpadding='4'>\n";

    // column headings
          $str .=  "<tr>\n";
          while ($fld = mysql_fetch_field ($result)) {
                   $str .= "<th>{$fld->name}</th>\n";
          }
          $str .=  "</tr>\n";


    // list data
          while ($row = mysql_fetch_row($result)) {
          $str .=  "<tr>\n";
          foreach ($row as $field) {
                   $str .=  "<td>$field</td>\n";
          }
          $str .=  "</tr>\n";
    }

    $str .=  "</TABLE>\n";
    
    return $str;
}

//
// call function
//

echo table2table('select * from tablename');

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.