select query : multiple responses?

Discussion in 'PHP' started by Lordy, Oct 18, 2007.

  1. #1
    <?php
    require('config.php');
    
    $connection=mysql_connect($ddbhost,$ddbuser,$ddbpw) or die ('fail');
    mysql_select_db($ddb) or die ("fail");
    $test=mysql_query("SELECT uid FROM test WHERE uname='Test'");
    $test2=mysql_fetch_assoc($test);
    mysql_close($connection);
    
    print_r($test2);
    
    ?>
    PHP:
    is my code

    in my database i have
    CREATE TABLE `test` (
      `uid` int(12) NOT NULL auto_increment,
      `uname` varchar(255) NOT NULL default '',
      `upassword` varchar(255) NOT NULL default '',
      PRIMARY KEY  (`uid`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
    
    INSERT INTO `test` VALUES(1, 'Test', 'cbb3ad65658902b16e3eccd44a2adba9');
    INSERT INTO `test` VALUES(2, 'Test', 'cbb3ad65658902b16e3eccd44a2adba9');
    INSERT INTO `test` VALUES(3, 'Test', 'cbb3ad65658902b16e3eccd44a2adba9');
    INSERT INTO `test` VALUES(4, 'Test', 'cbb3ad65658902b16e3eccd44a2adba9');
    INSERT INTO `test` VALUES(5, 'Tes2t', 'cbb3ad65658902b16e3eccd44a2adba9');
    PHP:
    yet, when i run my code i get
    am i trying to select wrong?
     
    Lordy, Oct 18, 2007 IP
  2. kkrizka

    kkrizka Peon

    Messages:
    223
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hmm, I'm not sure what is wrong with your code, because I use a different approach. You might give it a try. Use this instead of mysql_fetch_assoc

    
    while($row=mysql_fetch_array($test)) {
       echo $row['uid'];
    }
    
    Code (php):
     
    kkrizka, Oct 18, 2007 IP
    Lordy likes this.
  3. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yup, that does work.

    hmm, is there a way to store it into an array though?
    edit: nevermind, got it, very rudimentary though
    <?php
    require('config.php');
    
    $connection=mysql_connect($ddbhost,$ddbuser,$ddbpw) or die ('fail');
    mysql_select_db($ddb) or die ("fail");
    $test=mysql_query("SELECT uid FROM test WHERE uname='Test'");
    $test2 = array();
    while($row=mysql_fetch_array($test)) {
       array_push($test2, $row['uid']);
    }
    
    print_r($test2);
    
    ?>
    PHP:
     
    Lordy, Oct 18, 2007 IP
  4. Shimonenator

    Shimonenator Peon

    Messages:
    28
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Then you'll need to put your result to query first.
    
    $array = '';
    while($row=mysql_fetch_array($test)) {
       $array[] = $row['uid'];
    }
    print_r($array);
    
    PHP:
     
    Shimonenator, Oct 18, 2007 IP
  5. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #5
    seems both our methods would work.

    do you know if one is better than the other in terms of resources?
     
    Lordy, Oct 18, 2007 IP
  6. Shimonenator

    Shimonenator Peon

    Messages:
    28
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I guess the my way is a bit faster, because of:
    From: http://us3.php.net/array_push
     
    Shimonenator, Oct 18, 2007 IP
  7. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks =) will use your way
     
    Lordy, Oct 18, 2007 IP
  8. Shimonenator

    Shimonenator Peon

    Messages:
    28
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You're always welcome! :)
     
    Shimonenator, Oct 18, 2007 IP
    Lordy likes this.