Announcement

Collapse
No announcement yet.

Just wanted to count...

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

  • Just wanted to count...

    How do you count how many rows are in a table, for example, I have a table of 5 rows, I want to write a script to grab one of the 5 rows randomly, but I want it to get 6 rows if I add one row later and without modiflying any PHP codes. And how do I generate a random number in PHP?
    Thank you!

  • #2
    Just let MySQL select the random row:

    PHP Code:
    SELECT <your columns>,MD5(RAND()) AS myRandom FROM <your tableORDER BY myRandom LIMIT 1 
    That will make sure you always get a random row. If you only do

    ORDER BY RAND()

    it happens that you get the same result when you reload the page a few times.

    Comment


    • #3
      Originally posted by Icheb
      Just let MySQL select the random row:

      PHP Code:
      SELECT <your columns>,MD5(RAND()) AS myRandom FROM <your tableORDER BY myRandom LIMIT 1 
      That will make sure you always get a random row. If you only do

      ORDER BY RAND()

      it happens that you get the same result when you reload the page a few times.
      If you only have 5 rows in the table you will get the same result after a few views, no matter what method you use. But using ORDER BY RAND() is extremely faster, the only problem is that it will not work on very old versions of MySQL but I wouldn't worry about that as there should be no hosts running such old versions.
      Chen Avinadav
      Better to remain silent and be thought a fool than to speak out and remove all doubt.

      גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

      Comment


      • #4
        Why do I have to MD5 RAND()?? And what is myRandom? Sorry I am a n00b for computing...

        Comment


        • #5
          Just use this:
          Code:
          SELECT * FROM tblName ORDER BY RAND() LIMIT 1;
          Chen Avinadav
          Better to remain silent and be thought a fool than to speak out and remove all doubt.

          גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

          Comment


          • #6
            I also wanted to know how to get a random number probably... I know that in Java you use maths.random*37 to get a number between 0 - 36, but what about PHP? I've seen alot of variations for PHP, mt_rand(); srand((double)microtime()*1000000); and RAND()???

            Comment


            • #7
              To get a random number:
              PHP Code:
              // First seed the randomizer; this has to be done ONCE in the script
              // Since PHP 4.2.0 this is not required
              list($usec$sec) = explode(' 'microtime());
              mt_srand((float) $sec + ((float) $usec 100000));
               
              // Get a random number between 0 and 36:
              $number mt_rand(036); 
              Chen Avinadav
              Better to remain silent and be thought a fool than to speak out and remove all doubt.

              גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

              Comment


              • #8
                What about the other questions? And what is myRandom and why did you MD5 the RAND() at first?? I am very curious about all this so please tell me! Thank you!

                Comment


                • #9
                  Now you told me mt_rand, so what is the difference between mt_rand and rand()
                  I have PHP 4.2.3 on my server BTW

                  Comment


                  • #10
                    I didn't post about MD5(RAND()), and I don't recommend using it, so I won't explain it either.

                    mt_rand() is supposed to be faster than rand(), I'm not sure of the technical information but the manual itself suggests that mt_rand() is better.
                    Chen Avinadav
                    Better to remain silent and be thought a fool than to speak out and remove all doubt.

                    גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

                    Comment


                    • #11
                      Oh ok thank you!

                      Comment


                      • #12
                        I am using MySQL 3.23.54 and the problems I talked about do happen when I only do ORDER BY RAND().
                        If I, however, use my approach, I get better random numbers and therefore better results. I don't know why it happens, but it does.

                        Comment


                        • #13
                          I have always used PHP 4.2 so what exactly do you have to do back in PHP3? What is a seed generator?

                          Comment

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