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
Share on other sites

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??

Link to comment
Share on other sites

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');

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.