stupid mysql question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrthumbs
    salad tossing sig guy
    • Apr 2002
    • 11702

    #1

    stupid mysql question

    im trying to do the most basic of basics and i fucking cant do it:

    select * from table where name = '$name'

    Now everything works fine as long as $name contains a value.

    But when the var $name is empty i want to grab ALL records
    but mysql says fuckyou and doesnt select a thing. Duh.

    Is there some sort of wildcard i can give to $name to select
    all records? I tried the obvious. * % %% _ ...

    Yes, i know "select * from table" does the trick but i want to use
    this syntax ;)


    Anyone? :help:
  • Chaldoray
    Confirmed User
    • Jul 2003
    • 2868

    #2
    For example, this is how you call everything in mysql, the host, db name, etc...

    $user="chaldoray"; // MySQL login name
    $pass="ray"; // MySQL password
    $hostname="localhost"; // MySQL server location
    $dbase='schaldoray_db'; // MySQL database name
    $connection = mysql_connect("$hostname" , "$user" , "$pass");
    $db = mysql_select_db($dbase , $connection);

    Comment

    • mrthumbs
      salad tossing sig guy
      • Apr 2002
      • 11702

      #3
      Originally posted by Chaldoray
      For example, this is how you call everything in mysql, the host, db name, etc...

      $user="chaldoray"; // MySQL login name
      $pass="ray"; // MySQL password
      $hostname="localhost"; // MySQL server location
      $dbase='schaldoray_db'; // MySQL database name
      $connection = mysql_connect("$hostname" , "$user" , "$pass");
      $db = mysql_select_db($dbase , $connection);

      Comment

      • Chaldoray
        Confirmed User
        • Jul 2003
        • 2868

        #4
        Originally posted by mrthumbs


        Confused? Hit me up on icq at 237842929

        Comment

        • broke
          Confirmed User
          • Aug 2003
          • 4501

          #5
          Originally posted by mrthumbs


          Perfect Gonzo

          Comment

          • Penthouse_mike
            Confirmed User
            • Jun 2003
            • 317

            #6
            mrthumbs,

            my php/mysql is a bit rusty, but i think you are looking for something like this.
            if $name has a value, a where clause is appended to the query

            PHP Code:
            
            $q = "SELECT * from table_name";
            
            if ($name) {
            $q .= "where name = '$name' ";
            }
            
            mysql_query($q); 
            

            Comment

            • myneid
              Confirmed User
              • Jan 2003
              • 736

              #7
              you are gonna have to do it in whatever programming language you are using.

              you can use a wildcard which happens to be % but you must use a different sql statement

              select * from table where name = '$name'

              select * from table where name like '%'

              you can always do

              select * from table where name = '$name%'

              which if you pass it a name it will grab everything starting with that name.

              but to really grab everything you should just do

              select * from table;
              Tanguy 0x7a69 inc. Programmer/President/CEO
              http://www.0x7a69.com
              A Leader in Programming since 1996
              PHP, Ruby on Rails, MySQL, PCI DSS, and any Technical Consulting

              Comment

              • mrthumbs
                salad tossing sig guy
                • Apr 2002
                • 11702

                #8
                Originally posted by Chaldoray


                Confused? Hit me up on icq at 237842929
                i know how to connect to a database. Thanks tho.

                I need the same results from:
                " select * from table where name = 'what do i put here?' "

                as i get from:
                " select * from table "

                There must be a wildcard character..

                Comment

                • jwerd
                  Confirmed User
                  • Jun 2003
                  • 1953

                  #9
                  Simple:
                  PHP Code:
                  if($whatver hahahaha "") {
                  $query = "select *from whatever";
                  $result = mysql_query($result);
                  } else if($query != "") {
                  $query = "select *from whatever where whatever = $whatever";
                  $result = mysql_query($query);
                  } 
                  
                  Basically what this is doing is seeing if the var $whatever doesn't contian anything... then if it doesn't it queries the database to have it show everything, but if it does it contain, then it searches for that...

                  If that doesn't work, hit me up on ICQ!
                  Yii Framework Guru - Seasoned PHP vet - Partner @ XXXCoupon.com

                  Comment

                  • mrthumbs
                    salad tossing sig guy
                    • Apr 2002
                    • 11702

                    #10
                    Originally posted by Penthouse_mike
                    mrthumbs,

                    my php/mysql is a bit rusty, but i think you are looking for something like this.
                    if $name has a value, a where clause is appended to the query

                    PHP Code:
                    
                    $q = "SELECT * from table_name";
                    
                    if ($name) {
                    $q .= "where name = '$name' ";
                    }
                    
                    mysql_query($q); 
                    
                    yup.. but then i would need to modify a shitload of code instead
                    of simply replacing a static name into a var. Appreciate it tho and if theres no other option ill just have to start cut and pasting..

                    Comment

                    • Penthouse_mike
                      Confirmed User
                      • Jun 2003
                      • 317

                      #11
                      np. hope it works out mrthumbs

                      Comment

                      • 4Pics
                        Confirmed User
                        • Dec 2001
                        • 7952

                        #12
                        That code posted should work fine.

                        I don't think there is another way to do it though.

                        Post your question here

                        http://forums.devshed.com/forumdisplay.php?forumid=5

                        Someone should answer within 20mins.

                        Comment

                        • JSA Matt
                          So Fucking Banned
                          • Aug 2003
                          • 5464

                          #13
                          Give this a shot...

                          PHP Code:
                          SELECT * FROM table WHERE name REGEXP "^$name"; 
                          
                          Off to bed

                          Comment

                          • mrthumbs
                            salad tossing sig guy
                            • Apr 2002
                            • 11702

                            #14
                            Originally posted by JSA Matt
                            Give this a shot...

                            SELECT * FROM table WHERE name REGEXP "^$name";

                            Off to bed
                            Dude.. it worked!!!! Thanks.. you saved me a lot of time!!!

                            Hit me up if you want something in return 160272407 .


                            Thanks everybody for helping, gnite!

                            Comment

                            • margarita
                              Confirmed User
                              • Jun 2003
                              • 917

                              #15
                              I've never tried to use regexp in database, but it must be damned slow, especially on big tables, isn't it ?
                              Check out my favourite kinky sponsor and must-have tool Nifty Stats

                              Comment

                              • Mr. Porno King
                                Confirmed User
                                • Aug 2003
                                • 146

                                #16
                                SELECT * FROM table WHERE name REGEXP "^$name";

                                Yeah.. that'll work but..

                                SELECT * FROM table WHERE name LIKE "$name%";

                                is faster.
                                My Favorite Game

                                Comment

                                • Samoan
                                  Confirmed User
                                  • Aug 2003
                                  • 203

                                  #17
                                  Would this work?

                                  SELECT * FROM db WHERE name like '%$name%'
                                  Samoan

                                  Comment

                                  • mrthumbs
                                    salad tossing sig guy
                                    • Apr 2002
                                    • 11702

                                    #18
                                    Originally posted by Samoan
                                    Would this work?

                                    SELECT * FROM db WHERE name like '%$name%'
                                    nope.. % and %% no worky.. only the REGEXP works for me


                                    getting some sleep!!

                                    Comment

                                    • Alky
                                      Confirmed User
                                      • Apr 2002
                                      • 5651

                                      #19
                                      if you need help, contact me... icq 9287771

                                      Comment

                                      • Rivux
                                        Confirmed User
                                        • May 2001
                                        • 1026

                                        #20
                                        You should do all of your logic statements in php and not in the queries. If you want a select all when there is no value, then test for the value BEFORE going into the select statement. A bit of pseudo code below as an exampel


                                        If value is empty
                                        myquery = "select * from table"
                                        else
                                        myquery = "select * from table where column = value
                                        end if


                                        You can then run myquery thru the database with no problems. Also, NEVER, NEVER use * when returning all columns, it slows querie down by up to 60%. List the columns you want to return don't use *. Yes it takes longer to type, but it will save you tonnes of cpu cycles and resources.
                                        TrafficHolder.com - Buy/Sell Adult Traffic

                                        Comment

                                        • Alky
                                          Confirmed User
                                          • Apr 2002
                                          • 5651

                                          #21
                                          Originally posted by Rivux
                                          You should do all of your logic statements in php and not in the queries. If you want a select all when there is no value, then test for the value BEFORE going into the select statement. A bit of pseudo code below as an exampel


                                          If value is empty
                                          myquery = "select * from table"
                                          else
                                          myquery = "select * from table where column = value
                                          end if


                                          You can then run myquery thru the database with no problems. Also, NEVER, NEVER use * when returning all columns, it slows querie down by up to 60%. List the columns you want to return don't use *. Yes it takes longer to type, but it will save you tonnes of cpu cycles and resources.
                                          1.) Nothing wrong with doing mysql_query's inside if's.. just personal prefrences really. might look neater your way, thats all.

                                          2.) using * shouldnt hurt as long as you set your indexes up correctly.

                                          Comment

                                          • Rivux
                                            Confirmed User
                                            • May 2001
                                            • 1026

                                            #22
                                            Originally posted by Alky


                                            1.) Nothing wrong with doing mysql_query's inside if's.. just personal prefrences really. might look neater your way, thats all.

                                            2.) using * shouldnt hurt as long as you set your indexes up correctly.

                                            1)The issue wasnt with doing IFs inside of mysql_query calls, the issue was with using LIKE and REGEXP just because you cant be bothered using an IF statement.

                                            2)Yes it does hurt, regardless of whether indexes are setup properly or not at all. By using * you are forcing the DB to do a lookup of the columns in the table before it can return them, this is especially painful on joins. By specifying the columns, you are bypassing this step and the DB can return your columns without doing the lookup.
                                            TrafficHolder.com - Buy/Sell Adult Traffic

                                            Comment

                                            • Rivux
                                              Confirmed User
                                              • May 2001
                                              • 1026

                                              #23
                                              One other reason not to use Select *, by not specifying the columns, you are returning columns that you do not need(unless you need all of them, which is actually rare), this means you are passing more data(bandwidth) back and forth between the DB Engine and the calling language.

                                              And probably most importantly, it's a lazy habit, and lazy habits have a way of biting you when you least expect it. Also makes the code look sloppy which increases programming/updating time. But it won't of course, if you are some 1337 hacker who enjoys writing cryptic, uneditable code.
                                              TrafficHolder.com - Buy/Sell Adult Traffic

                                              Comment

                                              • JSA Matt
                                                So Fucking Banned
                                                • Aug 2003
                                                • 5464

                                                #24
                                                You guys are insane. He was looking for a quick fix to a minor problem. This has nothing to do with indexes or using * instead of the column names. How do you know this is even his actual code and not just an example?

                                                As far as you guys saying the process will be too heavy or slow, a properly setup mysql database can handle more than any of you could ever throw at it and that's a fact.

                                                Comment

                                                • JDog
                                                  Confirmed User
                                                  • Feb 2003
                                                  • 7453

                                                  #25
                                                  Originally posted by mrthumbs


                                                  yup.. but then i would need to modify a shitload of code instead
                                                  of simply replacing a static name into a var. Appreciate it tho and if theres no other option ill just have to start cut and pasting..
                                                  here try this

                                                  PHP Code:
                                                  if(!$name) { $name = '%'; } 
                                                  
                                                  put that up top!

                                                  jDoG
                                                  NSCash now powering ReelProfits.com
                                                  ALSO FEATURING: NSCash.com :: SoloDollars.com :: ReelProfits.com :: BiminiBucks.com :: VOD
                                                  PROGRAMS COMING SOON: Greedy Bucks :: Vengeance Cash
                                                  NOW OFFERING OVER 60 SITES
                                                  CONTACT :: JAMES SMITH :: CHIEF TECHNOLOGY OFFICER :: ICQ (711385133)

                                                  Comment

                                                  • DrGuile
                                                    Confirmed User
                                                    • Jan 2002
                                                    • 2025

                                                    #26
                                                    Originally posted by JSA Matt
                                                    As far as you guys saying the process will be too heavy or slow, a properly setup mysql database can handle more than any of you could ever throw at it and that's a fact.

                                                    Ahahaha, you should try working on real projects.

                                                    You can bring any MySQL database to its knee.

                                                    And the mysql setup isnt the problem, its sloppy code.
                                                    LiveBucks / Privatefeeds - Giving you money since 1999
                                                    Up to 50% Commission!
                                                    25% Webmaster Referal
                                                    Powered by Gamma

                                                    Comment

                                                    • JSA Matt
                                                      So Fucking Banned
                                                      • Aug 2003
                                                      • 5464

                                                      #27
                                                      Originally posted by DrGuile
                                                      You can bring any MySQL database to its knee.
                                                      Wow.. really? ::we need a sarcastic smilie

                                                      Like I said before.. a PROPERLY SETUP DATABASE can handle more than any of you could ever throw at it. Well, if it's on a good server..

                                                      Comment

                                                      • JDog
                                                        Confirmed User
                                                        • Feb 2003
                                                        • 7453

                                                        #28
                                                        Originally posted by DrGuile



                                                        Ahahaha, you should try working on real projects.

                                                        You can bring any MySQL database to its knee.

                                                        And the mysql setup isnt the problem, its sloppy code.
                                                        This is very true! It is code & it is the setup! If their not done right - it's going further than it's knees!

                                                        jDoG
                                                        NSCash now powering ReelProfits.com
                                                        ALSO FEATURING: NSCash.com :: SoloDollars.com :: ReelProfits.com :: BiminiBucks.com :: VOD
                                                        PROGRAMS COMING SOON: Greedy Bucks :: Vengeance Cash
                                                        NOW OFFERING OVER 60 SITES
                                                        CONTACT :: JAMES SMITH :: CHIEF TECHNOLOGY OFFICER :: ICQ (711385133)

                                                        Comment

                                                        Working...