www.webdeveloper.com
Results 1 to 4 of 4

Thread: Create Function For Array of Data from MySql

  1. #1
    Join Date
    Apr 2009
    Posts
    107

    Create Function For Array of Data from MySql

    I am writing a program where I repeat this code on numerous pages:
    PHP Code:
    $shipping_query=("SELECT * FROM `shipping` WHERE `mra_number`='$mra_number'");
    $shipping_result mysql_query($shipping_query);
        while(
    $ship=mysql_fetch_array($shipping_result)) {
            
    $c_fname=$ship['fname'];
            
    $c_lname=$ship['lname'];
            
    $c_company=$ship['company'];
            
    $c_distributor=$ship['distributor'];
            
    $c_email=$ship['email'];
            
    $c_phone=$ship['phone'];
            
    $c_address1=$ship['address1'];
            
    $c_address2=$ship['address2'];
            
    $c_city=$ship['city'];
            
    $c_state=$ship['state'];
            
    $c_zip=$ship['zip'];
            
    $c_country=$ship['country'];

    I would like to turn this into a function that I can just include. This is what I wrote and it doesn't seem to be working:
    PHP Code:
    function shipQuery($mra_num) {
    $shipping_query=mysql_query("SELECT * FROM shipping WHERE mra_number='$mra_num'");
        while(
    $ship=mysql_fetch_array($shipping_query)) {
            
    $ship['fname'] = $c_fname;
            
    $ship['lname'] = $c_lname;
            
    $ship['company'] = $c_company;
            
    $ship['distributor'] = $c_distributor;
            
    $ship['email'] = $c_email;
            
    $ship['phone'] = $c_phone;
            
    $ship['address1'] = $c_address1;
            
    $ship['address2'] = $c_address2;
            
    $ship['city'] = $c_city;
            
    $ship['state'] = $c_state;
            
    $ship['zip'] = $c_zip;
            
    $ship['country'] = $c_country;
        }
        
        return 
    $ship;

    If I try and echo $c_fname, the screen is blank. Any ideas?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,407
    Is mra_number the primary key or have a unique index (i.e. can you ever get more than one row)? If so, you could do something like:
    PHP Code:
    function shipQuery($mra_num)
    {
       
    $mra_num = (int)$mra_num// force integer to prevent SQL injection
       
    $shipping_query mysql_query("SELECT * FROM shipping WHERE mra_number='$mra_num'");
       if(
    mysql_num_rows($shipping_query))
       {
          return(
    mysql_fetch_assoc($shipping_query)); // use fetch_assoc
       
    }
       return 
    false;

    If you can get more than one row, then:
    PHP Code:
    function shipQuery($mra_num)
    {
       
    $mra_num = (int)$mra_num// force integer to prevent SQL injection
       
    $data = array();
       
    $shipping_query mysql_query("SELECT * FROM shipping WHERE mra_number='$mra_num'");
       while (
    $ship mysql_fetch_assoc($shipping_query)) {  // fetch_assoc
          
    $data[] = $ship;
       }
       return 
    $data;

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2009
    Posts
    107
    $mra_number is the unique key across all my tables. Only one row per $mra number. How do I display the data from:

    PHP Code:
    function shipQuery($mra_num

       
    $mra_num = (int)$mra_num// force integer to prevent SQL injection 
       
    $shipping_query mysql_query("SELECT * FROM shipping WHERE mra_number='$mra_num'"); 
       if(
    mysql_num_rows($shipping_query)) 
       { 
          return(
    mysql_fetch_assoc($shipping_query)); // use fetch_assoc 
       

       return 
    false


  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,407
    Just assign the return value of the function to a variable (which will be an array):
    PHP Code:
    $ship shipQuery($mra);
    if(!empty(
    $ship))
    {
       echo 
    "Hello, " $ship['fname'];
    }
    else
    {
       echo 
    "No matching data found.";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles