Create an array from two coloumns

Discussion in 'Programming' started by narang.jasdeep, Sep 24, 2010.

  1. #1
    Hello Everyone,

    I have a MySQL database as follows:

    id , name , city
    1 , abc , new york
    2 , xyz , toronto
    3 , jas , london

    I would like to learn how can i create an array that associates id to name, something like:

    <?php
    echo $array[1]; //Outputs abc
    ?>
    PHP:
     
    narang.jasdeep, Sep 24, 2010 IP
  2. narang.jasdeep

    narang.jasdeep Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So far, i have this:


    <?php
    $var = abc;
    $query = "SELECT id,name FROM table WHERE name='$var'";
    $resource = mysql_query($query);
    $array[] = mysql_fetch_array($resource);

    //But $array here contains a multi dimensional array that i dont want..
    ?>
     
    narang.jasdeep, Sep 24, 2010 IP
  3. dakshhmehta

    dakshhmehta Active Member

    Messages:
    220
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    85
    #3
    Hello, for that type of tabulate array, you can use 2 dimensionality array which will increase reliability.

    For example,
    Consider your given table:
    id , name , city
    1 , abc , new york
    2 , xyz , toronto
    3 , jas , london

    i j
    $db[3][3] // 3 rows (i) and 3 column (i).

    Abd iff you want to access Id of 1st record,
    you can call up array $db[1][1] // Out will be 1

    Hope this will help you
     
    dakshhmehta, Sep 24, 2010 IP
  4. narang.jasdeep

    narang.jasdeep Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the reply Daksh,

    But the problem here is that i'm pulling the data from a MySQL database and it always spits out the data in form of a Multi Dimensional Array... Accessing the exact records become very tough for me when i have Multi dimensional arrays...

    Is'nt there a solution that help me create an array that associates the id column to name column or vice versa..?
     
    narang.jasdeep, Sep 24, 2010 IP
  5. narang.jasdeep

    narang.jasdeep Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello, I figured out the way how to do it... thanks Daksh, your solution worked!!!!
     
    narang.jasdeep, Sep 24, 2010 IP
  6. dakshhmehta

    dakshhmehta Active Member

    Messages:
    220
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    85
    #6
    Sure, if you have more problem. just Pm me. i would like to help you.

    thank you again.
     
    dakshhmehta, Sep 24, 2010 IP
  7. narang.jasdeep

    narang.jasdeep Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for your Help... i really appreciate it... actually i'm a student and i've been hired at a company to develop their Corporate Website... so, i might need a lot of help in due course.. :)
     
    narang.jasdeep, Sep 24, 2010 IP
  8. jocurileus

    jocurileus Peon

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    $var = abc;
    $query = "SELECT id,name FROM table WHERE name='$var'";
    $resource = mysql_query($query);
    while($row = mysql_fetch_row($resource)){
    $array[$row[0]] = $row[1];
    }
    
    Code (php):
    Now $array[1] should output 'abc';

    To sanitize your php input you should use mysql_real_escape_string
    This are some example to sanitize get and post variables.
    
    // Escape GET variable
    foreach ( $_GET as $key => $val ){
    	$val =  mysql_real_escape_string($val);
    	$_GET[ $key ] = $val;
    }
    
    // Escape POST variable
    foreach ( $_POST as $key => $val ){
    	$val =  mysql_real_escape_string($val);
    	$_POST[ $key ] = $val;
    }
    
    Code (php):
    DON'T use this code as it is, you should change it to suit your needs. (for exampleif you pass an array through a post variable you will get an error)
     
    jocurileus, Sep 24, 2010 IP
  9. dakshhmehta

    dakshhmehta Active Member

    Messages:
    220
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    85
    #9
    yes, i think you should use mysql_escape_string() function in your application.

    For example, in your given query you can use it like:

    <?php

    $temp2 = "SELECT id FROM shareholders_data WHERE shareholders_name='".mysql_escape_string($arrayShareholdersName[$y])."'";

    ?>

    thank you. more questions are welcomed.
     
    dakshhmehta, Sep 24, 2010 IP
  10. narang.jasdeep

    narang.jasdeep Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks i hope this will help me a lot in tightening the security of my application
     
    narang.jasdeep, Sep 24, 2010 IP
  11. narang.jasdeep

    narang.jasdeep Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks, this worked as well!
     
    narang.jasdeep, Sep 24, 2010 IP