Prasinus Albus
Prasinus Albus

Reputation: 416

multiply all values retrieved from mysql with a constant

I am retrieving all values of a database in the form of two rows and need to multiply all values with a single constant. mysql query:

$book= "SELECT distinct names, values FROM table1 where address =?"
        mysqli_stmt_bind_param($stmm, "s", $usa);
        mysqli_stmt_execute($sttm);
        $result= mysqli_stmt_get_result($sttm);
        $ress = mysqli_fetch_array($result)

In the while loop I want to multiply all these values with a predefined variable $calc: I now want to do something like this:

        $i = 1;     
    while($ress = mysqli_fetch_assoc($result))
        {   
        echo '<tr><td>'.$i.'</td>';
        $i += 1;
        echo '<td>'.$ress['names'].' </td>';
        echo '<td> '.$ress['values']*$calc.'</td>';
}
echo '</tr></table>';
}

Please help

Upvotes: 0

Views: 285

Answers (3)

Phil
Phil

Reputation: 1464

I think that your didn't fetch data from your database. Your loop section also has some HTML tag match problem. I tried the following code with my local database, the loop section works well.

<?php 
include '../inc/conn_stackoverflow.php';
$book= $con->prepare("SELECT distinct `date`, cntr FROM counter where id >?");
$usa=0;
$book->bind_param("i", $usa);
mysqli_stmt_execute($book);
$result= mysqli_stmt_get_result($book);
//echo "<pre>";
//var_dump($result);
$calc=100;

echo "<table>";
$i=1;
while($ress = mysqli_fetch_assoc($result))
        {   
        echo '<tr><td>'.$i.'</td>';
        $i += 1;
        echo '<td>'.$ress['date'].' </td>';
        echo '<td> '.((float)$ress['cntr']*(float)$calc).'</td>';
        echo '</tr>';
}
echo '</table>';

Upvotes: 1

cute_programmer
cute_programmer

Reputation: 372

there are some reason for this not to work

main reason is that the data retrieved from database is not int type, i think the $ress['values'] is returning a string so you need to check it or parse it to int before multiplying

Upvotes: 1

Mayank Majithia
Mayank Majithia

Reputation: 1966

you might have problem in while loop.

you can use foreach, while you have records in (array)$ress

    foreach($ress as $key => $row)
    {   
        echo '<tr><td>'.$key.'</td>';
        echo '<td>'.$row['names'].' </td>';
        echo '<td> '.($ow['values'] * $calc).'</td>';
    }
   echo '</tr></table>';   

Upvotes: 1

Related Questions