#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2

    Define array with row = Mysql_fetch_array


    Hi all,

    It's been about 6 years since I've read a PHP book - so this may sound trivial to you all, but I'm fretting over this functionality:

    I'm trying to simply output 3 mysql results to 3 variables that I can then use inside of a marquee html tag. The mysql query is set to LIMIT 3, to keep the result concise. Here is what I have, but I know it's not right !::!


    PHP Code:
    $queryMan = "SELECT DISTINCT(manufacturer) FROM manufacturer ORDER BY RAND() LIMIT 3";
    $resultMan = @mysql_query($queryMan);
    $i=0;
    while ($rowMan = mysql_fetch_array($resultMan)) {
    $man = Array();
    $man[$i] = $rowMan[$i];
    $i++;
    }
    ?>
    <h5><marquee behavior="scroll" direction="left">Text...<?php echo $man1.', '.$man2.', and '.$man3?>...more text</marquee></h5>
    When i echo $man[$i] within the while loop to test it, I only get one row, while I needed three rows.

    Can somebody serve me with the right info here...
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Why would you want to create the $man array with every iteration of the loop?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,126
    Rep Power
    9398
    PHP Code:
    $man = Array(); 
    You keep resetting $man inside the loop. Also

    PHP Code:
    $man[$i] = $rowMan[$i];
    $i++; 
    $rowMan is a single row from the query. If you [$i] that you'll get a value from a column. Your query only has the one column so only [0] is valid - [1] and [2], which your loop tries to get to, will be null (because they don't exist). In fact $i isn't even necessary.

    PHP Code:
    $queryMan "SELECT DISTINCT(manufacturer) FROM manufacturer ORDER BY RAND() LIMIT 3"
    $resultMan = @mysql_query($queryMan); 
    $man = Array();
    while (
    $rowMan mysql_fetch_array($resultMan)) { 
    $man[] = $rowMan[0]; 

    Also, $man1 is a variable that doesn't exist. There is no "$man1=" in your code. Instead $man is an array. If you want the first value then use $man[0].
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2

    Smile


    This is what I wanted. I was trying every combination of, now apparently clear, wrong syntax.. Thanks requinix, now the site this goes to is more SEO optimized and great looking.

    I guess I should start brushing up on my php since I finally finished this 2 and 1/2 month project.

    Thank you again.

IMN logo majestic logo threadwatch logo seochat tools logo