0

If I have a PHP function that generates a random number, is it possible to pass that variable into the sql statement in the WHERE clause? I'm using CodeIgniter, so this is my code using its syntax.

$random = rand(1, 572);
$result = $this->db->query( ' SELECT part1, part2, _id FROM `questions` WHERE `_id` >= '$random' LIMIT 0,1 ');

Is this even possible to do?

EDIT: The reason I want the php to execute the random number is because I need to call it multiple times throughout my pages, and it needs to do another call to another database using a sql query

6
  • How do you know that random num exists? Commented Sep 13, 2011 at 3:22
  • 1
    Don't let people persuade you to use a different solution, you're headed in the right direction anyway. jan.kneschke.de/projects/mysql/order-by-rand ...otherwise, you just need to learn to concatenate strings (in PHP, use the period) Commented Sep 13, 2011 at 3:27
  • Because I know I have 572 rows right now. In the future that will probably grow, so what would be the best way to pass into that rand() function the max amount of rows in my table? Commented Sep 13, 2011 at 3:36
  • @TehShrike: If the solution is better than my current, then I personally will let others persuade me into using that :) Of course they'd have to let me understand first why their solution is better Commented Sep 13, 2011 at 3:46
  • @Rolando: your solution is correct, though your suggestion (ORDER BY RAND() LIMIT 1) is misinformed - you should check out that last post I linked! It's quite good. Commented Sep 13, 2011 at 6:57

3 Answers 3

1

Yes it is possible if you concatenate the variable with the string:

$query = "SELECT 
            part1, 
            part2, 
            _id 
          FROM
            questions
          WHERE _id >= " . $random . " LIMIT 0,1";
$result = $this->db->query($query);

But if what you want is to select a random row, then you might want this query

 SELECT part1, part2, _id FROM questions ORDER BY RAND() LIMIT 1 

EDIT

I understand that _id will be random, but you are specifying the min and max for rand(), right? So you'd have to change it whenever you insert a new row, or you'd have to use two queries if you want to make sure rand() does not return a value too high. By using ORDER BY RAND() you are free from both problems. You simply have to get the value of _id that was returned from the query.

Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, read my edit. I need to use that same number for a different call as well to a different database
@Bert why not use the id selected in the query for that?
This is exactly it. I forgot to concatenate it. I will accept your answer as best ASAP, stackoverflow is giving me a waiting period before I can select it as best.
But you'd have to harcode the maximum value of rand() every time you insert something to the database. If you simply want to store the ID, then get it from the result. You are returning _id from the query anyways.
Yes, but you execute the rand() before you execute the query. So if you want to use COUNT() you'd have to use 2 queries now. I understand that _id will be random but you are specifying the min and max for rand(), right? So you'd have to change it whenever you insert a new row, or you'd have to use two queries if you want to make sure rand() does not return a value too high. By using ORDER BY RAND() you are free from both problems. You simply have to get the value of _id that was returned from the query.
|
0

This might just be a mater of using double quotes instead of single quotes on the outside of your string.

$result = $this->db->query("SELECT part1, part2, _id FROM `questions` WHERE `_id` >= '$random' LIMIT 0,1 ");

Comments

0

Try this:

$result = $this->db->query("SELECT part1, part2, _id FROM questions WHERE _id >= '".$random."' LIMIT 0,1 ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.