Results 1 to 3 of 3

Thread: Simple Query(1 column) to array to variables (not simple to me )

  1. #1
    Join Date
    Jun 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Query(1 column) to array to variables (not simple to me )

    Hello,

    I need just a little push and as i'm new at arrays it would really be helpfull.

    I need to get variables with data coming from a simple sqlquery , my sql query returns datas as
    product1
    product2
    product3
    ...
    i can't seem to get
    var1 = product1
    var2 = product2 ...

    all i can do is echo them in a loop but no usable variable.

    here is my "cleared" code so far:

    Code:
    <?php
    $query = "SELECT `produits`.`nom_prod` FROM `selectmem` Inner Join `produits` ON `selectmem`.`bon1` = `produits`.`id` OR `selectmem`.`bon2` = `produits`.`id` OR `selectmem`.`bon3` = `produits`.`id` "; 
    $result = mysql_query($query) or die(mysql_error());
    
    while($row = mysql_fetch_array($result))
    	{
    	echo $row['nom_prod'];
    	echo "<br />";
    	}
    	echo "THE_END";
    
    ?>
    my loop works and gets the datas but how to get datas to as many variables?
    $var1, $var2 etc..

    This or going directly from query to variables of course


    thanks for your help
    Last edited by noobster; 03-17-2009 at 11:07 AM. Reason: minor variation (no array is possible too)

  2. #2
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    As you iterate, you have to store each result in an array or work on the returned result within each iteration.

    PHP Code:

    $data 
    = array();

    while(
    $row mysql_fetch_array($result))
        {
        echo 
    $row['nom_prod'];
            
    array_push($data$row['nom_prod']);
        echo 
    "<br />";
        }

    echo 
    "<pre>".print_r($datatrue)."</pre>"

  3. #3
    Join Date
    Jun 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks a lot CrazyChop, i'll test that as soon as possible.
    I'll travel for work this week so i'm not sure when i'll give feedback but i'll try to understand and adapt wich is always better.

    Thanks for your time and tip

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
  •