Engr Atiq Engr Atiq - 3 months ago 9
PHP Question

Select query returns nothing for a random number

I am trying to fetch values from the database where a certain random number has a match. Here is my line of code

echo $rand_number = $_POST['rand'];
$select = mysqli_query($connection,"select * from temp_info where rand_number = '$rand_number'");


I am successfully receiving the random number which is also saved in the database. When I want to fetch from the database using the mysqli_fetch_array command, it returns nothing. However, when I write the random number itself instead of the variable $random_number. The query returns the desired result.

I have double checked that the $rand_number has the same random number that is saved in the database. What seems to be the problem?

Answer

In your case $rand_number is not treated as variable in query, in order to let PHP know that this is a variable put it in between two .. Try this :

$select = mysqli_query($connection,"select * from temp_info where rand_number ='".$rand_number."'");
Comments