Create new function to add values

function wallet($a,$field,$id)

{

    $selectwallet=mysqli_query($a,"select `$field` from `tbl_wallet` where `userid`='".$id."'");

$walletResult=mysqli_fetch_array($selectwallet);

return $walletResult["$field"];

    }

    function winning($a,$field,$id)

{

    $selectwinning=mysqli_query($a,"select `$field` from `tbl_winning` where `userid`='".$id."'");

$walletwinning=mysqli_fetch_array($selectwinning);

return $walletwinning["$field"];

    }

i want to add this 2 functions and create anather function like

function winning($a,$field,$id)

$walletbalance=$walletwinning+$walletresult;

please help me i am getting errors

Please don’t bump other topics with your question. You can use bbcode [code][/code] tags, on separate lines, before/after your code, to cause it to be formatted as code. I have made this edit to your code above.

Sounds like those two functions could be combined into one function after all that is the primary reason to have a function in order to save a coder some typing. :wink:

I personally would do something like the following.

$table = 'tbl_wallet';
$field = 'name of the field';
$searchValue = 1; // Just an placeholder value what would be in the database table;

function fetchColumn ($table, $field, $searchValue) {
        $db_options = [
            /* important! use actual prepared statements (default: emulate prepared statements) */
            PDO::ATTR_EMULATE_PREPARES => false
            /* throw exceptions on errors (default: stay silent) */
        , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            /* fetch associative arrays (default: mixed arrays)    */
        , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ];
      
   $pdo= new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);

   $sql = 'SELECT ' . $field . ' FROM ' . $table . ' WHERE user_id =:id LIMIT 1';
   $stmt = $pdo->prepare($sql); // 
   $stmt->execute([ 'id' => $searchValue ]);
   return $stmt->fetch(PDO::FETCH_ASSOC);
}
$walletResult = fetchColumn($table, $field, $searchValue);

The above could could be streamline immensely and I used PDO instead of mysqli as I haven’t used mysqli in a very long time. Though I would recommend PDO over mysqli as I think it’s easier to use and code. The above code hasn’t been test and there might be some errors, but I was just trying to give the direction I would go.

Sponsor our Newsletter | Privacy Policy | Terms of Service