Store random number PHP
Solvedjee pee Posted messages 9437 Registration date Status Moderator Last intervention -
Hello everyone
I would like to generate a random number when the page loads and keep that same number for the rest of the code
So I did:
$number = rand(1,10);
The problem is that every time the variable $number is used in the rest of the program it generates a different number, whereas I want to keep the same one throughout
Does anyone have an idea on how to make that number stay the same throughout the code?
Thanks in advance.
1 answer
-
Hello
Using a variable will not regenerate its value...
If your random number is being regenerated every time, it's because you have coded it incorrectly or there's a bug in your code...
It has nothing to do with the single line of code you are showing us.
You will need to show us the complete code of the page related to this issue.
Of course, based on the description you provided, we assume that this variable is only being used within the same page that generated it....... Otherwise, if you want to use it in other scripts, you will indeed need to store it in a session variable, and to prevent it from regenerating every time the page is reloaded after a form submission, for example, you will first need to check whether this variable already exists or not before regenerating it...
.
Best regards,
Jordane-
Hello
To be more precise, as soon as I click on a button (which will send a POST request to the PHP page to trigger the code), the PHP should display a line at random from my database. So after declaring $number, I execute the prepared query:
SELECT * FROM table1 WHERE id=$number
But I get a different line displayed with each click, whereas theoretically, since $number remains fixed, the line should stay the same with each click.
I'm a beginner, so it’s probably a silly mistake, but I can't find it.
Thank you for your help.
-
-
-
-
Hello,
"
PHP should display a random line from my database, so after declaring $number, I execute the prepared statement:
<span>SELECT</span> <span>*</span> <span>FROM</span> table1 <span>WHERE</span> id<span>=</span>$number"Before any serious design and understanding problems with a database, I feel...
If your data model has an identifier, it serves to identify a piece of data in the database (and not elsewhere in general). With your model, you have to change the entire program if you need to add data to the table, and worse, your database will be useless if you delete a single piece of data from the table.
If I delete id = 1, then if the random number falls on 1, the program no longer works, and plus the database becomes unusable because the id no longer corresponds to anything... A piece of data is a value that makes sense for an information system; 1, 2, 3, 1312, etc., correspond to nothing. An id does not indicate any data. You can arbitrarily use an auto-incremented numeric identifier as a primary key if you want (although it's inferior to a natural identifier in many cases as a method), but using that value makes no sense since its only role is to ensure that every line of data is unique; there will be only one line in the table that has the value id=852244555. And well, a database is made to store and MODIFY the stored data; with your system, if you need to modify, you will have to do complex random value processing that makes no sense.
If you want to do this kind of thing, either you do it from a dataset using an array to store data (the natural form of a query result), where each set or line of data is ranked arbitrarily (from 0 to 'x', an integer indicating the number of corresponding values to the query), or you use a value that has meaning.
For example, an identifier named fruit_name = 'apple' or 'pear' or 'banana'... and there you do the random thing on these values. The fruit name corresponds to something real, whereas an auto-incremented numeric identifier does not guarantee any logical or mathematical sequence; therefore, you could have data with auto-incremented ids 0, 1, 2, a numeric integer up to infinity.
In short, a database must make sense; otherwise, it becomes unusable. Not only do programs become more complex to create and manage until it becomes impossible, but the database becomes unusable and worthless, and with that, you lose all the stored data.
The use of an auto-incremented numeric identifier is a trick (or even laziness on the part of the designer or a convenience when two values in a table need to be identifiable with or without a join), but clearly, its value corresponds to nothing other than a number that guarantees a unique value for each line. Using an arbitrary and changing value (adding a row increments the id value by 1, adding 1000 rows will each increment by 1) means that if you modify any value in your database, nothing works anymore since where you previously had 1, 2, 3,..., you could very well have 1, 457, 1687, 1333 (the order also indicates nothing except that a value is possibly inserted before another).
In conclusion, either you do your random draw on a value that has meaning (the natural identifier) or a set of arbitrary data at the moment you retrieve it, for example, with SELECT * FROM table1.
Ps: it's a bad idea to name your tables 'table1' which means absolutely nothing. A table name should indicate the role of the data grouping. Therefore, an outside person or you in a few months will not know at all what table1 corresponds to since it means nothing. Worse, imagine if everyone did this; everyone would have table1, table2, etc., and therefore, for one notation, table1 could correspond to table34 for someone else. I call that making a mess intentionally. While a name that means something and represents the stored entity not only helps everyone understand but also helps you clearly know what it concerns just by looking at the program and not having to do a complete analysis.
So tableClient is OK, but table1 is not OK at all.
tableFruits YES, table1457 NO.
-