Results 1 to 4 of 4

Thread: PHP - I want to retrieve records from specific field from a MySQL table and build array using PHP

  1. #1
    Join Date
    Jul 2010
    Posts
    2

    PHP - I want to retrieve records from specific field from a MySQL table and build array using PHP

    I want to retrieve records from specific field from a MySQL table and build array.

    Lets say I have field first_name in MySQL table and after using query
    "SELECT first_name from family", get records like
    Andy
    Bob
    Craig

    Now I want them to be in this format of array
    $names = array(1=>'Andy',
    2=>'Bob',
    3=>'Craig');

    Now the problem I am facing is when I run this query using PHP

    $query = "SELECT first_name FROM family ";
    $result = mysql_query($query);
    while($rows = mysql_fetch_array($result))
    {
    $names = $row['first_name'];
    }

    I don't want my variable (which stores the array) inside a while loop.....

  2. #2
    Join Date
    Dec 2007
    Posts
    1,599

    Re: PHP - I want to retrieve records from specific field from a MySQL table and build array using PHP

    If it is a problem with the db connection then I cant be too much helful but if it's the array, have a look at the PHP manual's entry on mysql_fetch_array() (http://www.php.net/mysql_fetch_array)

    <?php
    include 'config.php';
    include 'opendb.php';

    $query = "SELECT name, subject, message FROM contact";
    $result = mysql_query($query);

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    echo "Name :{$row['name']} <br>" .
    "Subject : {$row['subject']} <br>" .
    "Message : {$row['message']} <br><br>";
    }

    include 'closedb.php';
    ?>

  3. #3
    Join Date
    Jul 2010
    Posts
    2

    Re: PHP - I want to retrieve records from specific field from a MySQL table and build array using PHP

    I dont have problem retrieving records in mysql_fetch_array, I instead want the records to be in array format of

    $names = array(1=>'Andy',
    2=>'Bob',
    3=>'Craig');
    and the array should be accessible outside the while or any loop...

  4. #4
    Join Date
    Jan 2006
    Posts
    605

    Re: PHP - I want to retrieve records from specific field from a MySQL table and build array using PHP

    Can you just don't put the quotes in there.

    <form method="post" action="">
    <input type="text" name="userInfo[name]">
    <input type="text" name="userInfo[email]">
    <input type="text" name="userInfo[password]">
    <input type="submit">
    </form>
    <pre>
    <?php
    var_dump($_POST);
    ?>
    </pre>

    Submit a,b,c and get:

    array(1) {
    ["userInfo"]=>
    array(3) {
    ["name"]=>
    string(1) "a"
    ["email"]=>
    string(1) "b"
    ["password"]=>
    string(1) "c"
    }
    }

    Let us know back.

Similar Threads

  1. PHP mysql select unique records from one table
    By Captain Carrot in forum Software Development
    Replies: 6
    Last Post: 13-05-2010, 12:18 PM
  2. Select unique records from a table
    By Conraad in forum Software Development
    Replies: 5
    Last Post: 13-05-2010, 12:01 AM
  3. Retrieve an array using StringTokenizer
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 12:57 AM
  4. Replies: 3
    Last Post: 08-08-2009, 11:57 AM
  5. MySql to show records which is not a value
    By Grundy in forum Software Development
    Replies: 3
    Last Post: 23-07-2009, 07:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,051,224.06112 seconds with 17 queries