Results 1 to 12 of 12

Thread: Selecting single entry from db

  1. #1

    Selecting single entry from db

    Im having a little problem selecting a single entry from a db><
    heres my code

    PHP Code:
      <?php 
    $name
    mysql_query(" SELECT 'team_name' FROM `team` WHERE team_id =1");
                 if (!
    $name){
                 die(
    "Database query failed: " mysql_error());
                } 
              
              
              echo 
    "$name";
              
              
              
    ?>
    i ran it through the sql console in phpmyadmin and it worked fine but when i jumped over into php and tried executing it echos out the following

    Resource id#5

    any ideas what im dooing wrong?

  2. #2
    is it the space before the SELECT statement?

  3. #3
    Nope

  4. #4
    Does this work?

    PHP Code:
    <?php 
    $name 
    mysql_query("SELECT team_name FROM team WHERE team_id =1");
    if (!
    $name)
    {
    die(
    "Database query failed: " mysql_error());

    echo 
    "$name";
    ?>

  5. #5
    Put a LIMIT 1 at the end of your MySQL qquery.
    Member #2 of the "I wont critique Timmytot's designs anymore" club.

  6. #6
    i added the LIMIT 1 to the end of the sql statment and i ran it through php my admin and it didnt give me any errors but, under the sql field it usualy shows u the row it pulls out this time it only gave me

    team_name
    ___________
    team_name

    this is my new query

    PHP Code:
     <?php 
             $name
    =mysql_query("SELECT 'team_name' FROM `team` WHERE team_id =1 LIMIT 1");
                 if (!
    $name){
                die(
    "Database query failed: " mysql_error());
                } 
              
              
              echo 
    ""$name";
              
              
              ?>

  7. #7
    Code:
    <?php
    $name=mysql_query("SELECT team_name FROM team WHERE team_id =1");
                 if (!$name){
                die("Database query failed: " . mysql_error());
                } else{
        $r = mysql_fetch_array($name);
         echo $r['team_name'];
    }
    ?>
    Try this...
    Web Site Design and Stuff....One day I will get it.

  8. #8
    oh man i am such an idiot....... the col namewas "name" not team name it works so far but it only displays the first item in the array. how would i tell it to echo say the 5th item in the array?

    i tried

    echo "$r[5]";

    and nothing gets echoed out

  9. #9
    You need to use mysql_fetch_array, like Bmoeb said. It converts resources returned from MySQL into PHP variables.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  10. #10
    Code:
    <?php
    $i=0;
    $name=mysql_query("SELECT name FROM team WHERE team_id =1");
    if (!$name){
      die("Database query failed: " . mysql_error());
      } else{
      //while will loop through entire table and show all team names.
       while($r = mysql_fetch_array($name)){
         if($i==4){
         //if $i is 5th row 0-4 echo name
        //if you want all rows remove if($i==4){ and } below..
         echo $r['name'];
        }
      $i++;
     //increment i
     }
    }
    ?>
    If team_id is only in the table once it will only show the one record that has that team_id. If you want it to show more change
    $name=mysql_query("SELECT name FROM team WHERE team_id =1");
    to
    $name=mysql_query("SELECT name FROM team");
    Web Site Design and Stuff....One day I will get it.

  11. #11

    Quote Originally Posted by MTsoul View Post
    You need to use mysql_fetch_array, like Bmoeb said. It converts resources returned from MySQL into PHP variables.
    mysql_fetch_assoc is easier to use, and mysql_fetch_object is easier again
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  12. #12
    thanks for the help guys apreciate it a lot!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012