The SQL code that I have works in HeidiSQL when run over and over again, but when I clone it into PHP and run mysqli_query($db,$sql), it doesn't work.
The following PHP/MySQL code is all valid and works perfectly.
$sql = "select `ID`,`User` from (
select * from
(SELECT
`ID`,
`User`,
`BI`,
(@cnt:= @cnt + (`BI`/(select SUM(`BI`) from `ax`))) as `Entirety`
from `ax` as `t`
CROSS JOIN (SELECT @cnt := 0) AS var
order by `BI`
) d
where `Entirety`>(@rnd)
order by `BI`
) as `l`
cross join (select (@rnd := rand()) as `RandomValue`) as var2
limit 1;";
I then run the $sql through
$result = mysqli_query($db,$sql);
$results = mysqli_fetch_array($result);
where $db is a valid and open connection to the MySQL server. But the return of the object when I do
print_r($result);
comes out as
mysqli_result Object (
[current_field] => 0
[field_count] => 2
[lengths] =>
[num_rows] => 0
[type] => 0
)
I don't want this and the num_rows should be '1' as that is what shows in HeidiSQL when I run it. Here's an image of HeidiSQL showing the results:

Anyone have any ideas?
The COLUMNS of the table ax are ID, User and BI.
In the SQL, it creates two temporary columns called 'Entirety' which is the probability counter anda column called RandomValue which is a rand() from 0 -> 1.
The only row in this table, has values of (1,1,10). Even though it is 10, it has a Entirety of '1' which means it is a 100% guaranteed-to-be-chosen. Despite this, nothing is chosen.


echo mysqli_error($db);after mysqli_query