0

I've tried to convert my string array to INT by using intval But the result / return always int(1) while the database value was 14698

my database :

+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| curr  | char(3) | NO   |     | NULL    |                |
| rate  | int(11) | NO   |     | 0       |                |
+-------+---------+------+-----+---------+----------------+

+----+------+-------+
| id | curr | rate  |
+----+------+-------+
|  1 | USD  |     1 |
|  2 | IDR  | 14698 |
|  3 | JPY  |   112 |
|  4 | EUR  |     1 |
|  5 | THB  |    33 |
+----+------+-------+

Here's my crudQuery.php page :

    <?php

require 'config.php';

if (isset($_POST['save'])) {

    require_once 'functions.php';

    $qty = $_POST['qty'];
    $qty = (int) $qty;
    $curr = $_POST['curr'];
    $price = $_POST['price'];
    $price = (int) $price;

    $rate = queryInt("SELECT rate FROM rate_master WHERE curr = '$curr'");

    var_dump($rate);echo $rate[0];

here's my functions.php page :

<?php
require 'config.php';

    function queryInt($query) {
        global $conn;
        $result = mysqli_query($conn, $query);
        $rows = [];
        while ($row = mysqli_fetch_assoc($result)) {
            $rows [] = intval($row); 
        }
        return $rows;
    }

Then, the result of var_dump was :

array(1) { [0]=> int(1) } 1

What I suppossed to do, in order to return the value from database imstead of 1 ?

1
  • 1
    change var_dump($rate);echo $rate[0]; to var_dump($rate);echo $rate[1]; Commented Dec 5, 2019 at 13:00

1 Answer 1

1
while ($row = mysqli_fetch_assoc($result)) {
    $rows [] = intval($row); 
}

mysqli_fetch_assoc returns an array, you can not cast an array “into int”, and expect that to have a meaningful result.

intval($row) needs to be intval($row['rate'])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.