Announcement

Collapse
No announcement yet.

Simple Select with PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Select with PHP

    Can some write a simple Select statement in PHP so I can see the basic code?

    I have only come across ones that seem to be bloated code for a simple query.

    For instance

    "SELECT username from users, wher USERID =1"

    I just want to see the needed PHP elements, not extra stuff. If anyone can do that I would appreciate it.

    Im reading this new book from BN (PHP in easy steps) it's ok but I think some areas are not to clear.
    Louis| Techieville.com

  • #2
    Originally posted by louisl
    Can some write a simple Select statement in PHP so I can see the basic code?

    I have only come across ones that seem to be bloated code for a simple query.

    For instance

    "SELECT username from users, wher USERID =1"

    I just want to see the needed PHP elements, not extra stuff. If anyone can do that I would appreciate it.

    Im reading this new book from BN (PHP in easy steps) it's ok but I think some areas are not to clear.
    Something like
    PHP Code:
     
    // initiate the connection to the database, if it fails, tell us why
    $connection mysql_connect("localhost", [b]"mysql username"[/b], [b]"mysql password"[/b]) OR die("MySQL connection failed: " mysql_error());
     
    // the query to select something from the database, in this instance it will select all administrators from vBulletin's database
    $query "SELECT * FROM `user` WHERE `usergroupid` = 6";
     
    // perform the query
    $getusers mysql_query($query$connection) OR die("MySQL query failed: " mysql_error());
     
    // for each row returned by the database, print all details of the user to the screen
    while($user mysql_fetch_array($getusers))
    {
    print_r($user);
    }
     
    // closes the database connection
    mysql_close($connection); 
    This enough? (using the php manual i pretty much ripped it straight out though)

    You could replace the while loop and the mysql_fetch_array with just a single call to the mysql_fetch_row function, however, this only works if your expecting one result (it will only return one result if you use this and MySQL returns many results.)

    If you're only expecting one result, you should limit the SQL query to one, by adding LIMIT 1 to the end of the statement.

    I always use mysql_fetch_array, or $DB_site->fetch_array if its in vBulletin.

    Comment


    • #3
      PHP Code:
      $resource mysql_query("SELECT username FROM users WHERE userid='1'");

      //to get the information from it you can do something like this

      while ($a_user mysql_fetch_array($resource))
          echo 
      $a_user['username']; 

      Comment


      • #4
        Thanks

        Yeah this is where I got confused
        PHP Code:
         [font=Courier New][color=#ff9900]// the query to select something from the database, in this instance it will select all administrators from vBulletin's database
        [/color][color=#0000cc]$query [/color][color=#006600]= [/color][color=#cc0000]"SELECT * FROM `user` WHERE `usergroupid` = 6"[/color][/font][font=Courier New][color=#006600];
        [/color][/font][font=Courier New][color=#ff9900]// perform the query
        [/color][color=#0000cc]$getusers [/color][color=#006600]= [/color][color=#0000cc]mysql_query[/color][color=#006600]([/color][color=#0000cc]$query[/color][color=#006600], [/color][color=#0000cc]$connection[/color][color=#006600]) OR die([/color][color=#cc0000]"MySQL query failed: " [/color][color=#006600]. [/color][color=#0000cc]mysql_error[/color][/font][font=Courier New][color=#006600]());

        [/color][/font][font=Courier New][color=#ff9900]// for each row returned by the database, print all details of the user to the screen
        [/color][color=#006600]while([/color][color=#0000cc]$user [/color][color=#006600]= [/color][color=#0000cc]mysql_fetch_array[/color][color=#006600]([/color][color=#0000cc]$getusers[/color][/font][font=Courier New][color=#006600]))
        {
        [/
        color][color=#0000cc]print_r[/color][color=#006600]([/color][color=#0000cc]$user[/color][/font][font=Courier New][color=#006600]);
        }
        [/
        color][/font
        Why the while / array? For instance If Im stating that I want to have the record with id"10" and this is what I want to sho on the page, do I still have to use this method? Also print_r ($user) should disply the results on the php page right?

        Cause My examples are not showing anything. But I do appreciate your reply as it looks much different than the book.
        Louis| Techieville.com

        Comment


        • #5
          Originally posted by louisl
          Yeah this is where I got confused
          PHP Code:
           // the query to select something from the database, in this instance it will select all administrators from vBulletin's database
          $query "SELECT * FROM `user` WHERE `usergroupid` = 6";
          // perform the query
          $getusers mysql_query($query$connection) OR die("MySQL query failed: " mysql_error());
           
          // for each row returned by the database, print all details of the user to the screen
          while($user mysql_fetch_array($getusers))
          {
          print_r($user);

          Why the while / array? For instance If Im stating that I want to have the record with id"10" and this is what I want to sho on the page, do I still have to use this method? Also print_r ($user) should disply the results on the php page right?

          Cause My examples are not showing anything. But I do appreciate your reply as it looks much different than the book.
          If you only expect one result for this query, you can change the while part to look like

          PHP Code:
          $user mysql_fetch_row($getusers);
          print_r($user); 
          Using the while loop will allow for you to use the query for getting more than one result. But if you are only looking for one there isnt much point. Just make sure the query returns the result you are looking for!

          print_r will print a formatted array to the output.

          Comment


          • #6
            Ok so taking what you have and what I had this is my query:

            PHP Code:
            # Connecting the the databse
            $conn = @mysql_connect("localhost""mysql username""mysql password") or die ("err:conn");

            #Selecting the databse
            $result = @mysql_select_db("dbname" $conn)

            #query
            $sql "select id, username from users where id=2";

            #dispaly on php page OUTPUT
            $result mysql_fetch_row($sql);
            print_r($sql); 
            Now I did not try as Im at work but The way I see it this will display the one result Im looking for. The goal is to query the DB but place items (text, images) etc in certain places. Thanks for your help merk. All the quireies I see have an array (while) But I got a little confused. I'll have to see if this works when I get in.
            Louis| Techieville.com

            Comment


            • #7
              Originally posted by louisl
              Ok so taking what you have and what I had this is my query:

              PHP Code:
              # Connecting the the databse
              $conn = @mysql_connect("localhost""mysql username""mysql password") or die ("err:conn");
               
              #Selecting the databse
              $result = @mysql_select_db("dbname" $conn)
               
              #query
              $sql "select id, username from users where id=2";
               
              #dispaly on php page OUTPUT
              $result mysql_fetch_row($sql);
              print_r($sql); 
              Now I did not try as Im at work but The way I see it this will display the one result Im looking for. The goal is to query the DB but place items (text, images) etc in certain places. Thanks for your help merk. All the quireies I see have an array (while) But I got a little confused. I'll have to see if this works when I get in.
              The array will return every column in the row you selected. So there will only be id and username.

              As for your errors, it seems you have supressed the mysql_query errors, and you are missing a comma. That might be the problem. (each row you could possibly return is an array, you would access it via $result[id] and $result[username].

              Comment


              • #8
                merk

                Thank you for all your help, but my crap seems to outa whack. I cant seem to produce the correct result. I get something like Arry o -->--> ). I will get back to this After I complete the datainsert into the DB. Maybe Im just trying to hard right now.
                Louis| Techieville.com

                Comment


                • #9
                  Try adding echo "<pre>"; before the code.

                  It might format it better.

                  Comment


                  • #10
                    Basically your mysql query should look like this:

                    PHP Code:
                    mysql_query("SELECT username from users WHERE USERID = '1' "); 
                    to learn more about it you should also check php.net because they have lots of explanations of how to use the queries as well.

                    Comment

                    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                    Working...
                    X