Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6551169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:18:11+00:00 2026-05-25T12:18:11+00:00

If I have a PHP function that generates a random number, is it possible

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:18:11+00:00Added an answer on May 25, 2026 at 12:18 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php function that generates HTML code like below function j_uf_SomeFunction($some_var) {
I have some jQuery code. I have called an Ajax function file, file.php, that
i need help with disk_total_space function.. i have this on my code <?php $sql=select
Using PHP, what are some ways to generate a random confirmation code that can
I have a php class that generates a map image depending on my db
I am trying to create a function that generates a random integer out of
I have a PHP script that returns a random password. How do I echo
I have a php code that generates a piece of html code like this.
Is there any way to have PHP automatically call a function, before a script
I have code with the following form: <?php function doSomething{ //Do stuff with MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.