Leon Leon - 9 months ago 18
MySQL Question

Converting 'String' value from MySQL table into Interger

I am writing some script in attempt to convert a

"String"
value, which is retrieved from a MySQL database table, into an
INTEGER
, which I will then use within my PHP code.


The PHP Select-statement:

Code:

$my_number = "SELECT my_number from numbers_table where login = '".$_SESSION['login']."'";
$result_my_number = mysqli_query($conn,$my_number);

$converted_number = ($result_my_number + 20); // add 20 to my_number

echo $converted_number;


This results in nothing...

I done some research and attempted this:

Code:

$converted_number = ((int)$result_my_number) + 20; // convert my_number into an INTEGER


Also did not get any thing from this, neither a error message.

So I thought I would extract everything from the table, into an array:

Code:

$my_number = "SELECT * from numbers_table where login = '".$_SESSION['login']."'";
$result_my_number = mysqli_query($conn,$my_number);
$row_my_number = mysqli_fetch_array($result_my_number);

if ($row_my_number) {
$actual_number = $row_my_number['my_number'];
}

$final_number = ($actual_number + 20);

echo $final_number;


And also did not get anything from this. Just a blank screen.

Answer Source

You need to use mysqli_fetch_row() to get the number from the database.

$my_number = "SELECT my_number from numbers_table where login = '".$_SESSION['login']."'";
$result = mysqli_query($conn,$my_number);
$row = mysqli_fetch_row($result);

$result_my_number = $row['my_number'];

$converted_number = ($result_my_number + 20);     //  add 20 to my_number

echo $converted_number;