Notice: Undefined variable: num

Qeaxe

I have three files conn.php, func.php and index.php in the conn.php i have my connection to the database and in index.php I've included the connection file while in func.php I've a function that counts the number of columns

func.php

<?php
function countusers($connection, $column, $table)
{
    $stmt = mysqli_prepare($connection, "
                                    SELECT
                                        COUNT($column)
                                    FROM
                                        $table");
    if($stmt)
    {
        mysqli_stmt_bind_result($stmt, $num);
        mysqli_execute($stmt);
        mysqli_stmt_fetch($stmt);
        mysqli_stmt_close($stmt);
    }
    return $num;
}

But it doesn't seem to work I've tried adding the connection file in the function and it worked.

here is my index file

index.php

<?php
require_once "conn.php";
include "func.php";
echo countusers($conn, "id", "mods");

conn.php

<?php
$conn = mysqli_connect("localhost", "root", "", "test", 3306);
if(!$conn)
{
    echo "An Error Occurred";
}
TimBrownlaw

A quick read up in php.net states that the bind_result must occur after the execute... I have not run this so its a suggestion only.

So you could try...

//... SNIP...
    if($stmt)
    {
        mysqli_stmt_execute($stmt); // Changed this to  mysqli_stmt_execute       
        mysqli_stmt_bind_result($stmt, $num);
        mysqli_stmt_fetch($stmt);
        mysqli_stmt_close($stmt);
    }
    else {
       $num = 0;
    }
    return $num;
// ... SNIP ...

Also what is your default value for $num in the case that $stmt is false? You need to decide and set that.

UPDATE: The default value for $num has been added in the case the stmt fails for any reason. I have since run this code and it works.

The changes suggested in the code snippet DOES provide a value for $num, whereas the original code did not.

As to the validity of the actual method/implementation used is another matter, this answer directly addressed the question at hand.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Notice: Undefined variable: table

PHP Notice: Undefined variable

PHP Notice: Undefined variable:

Notice: Undefined variable with <?= $property

Notice: Undefined variable in function +=

Notice: Undefined index: variable

Notice: Undefined variable: mysqli

Notice: Undefined variable: x in include()

Notice: Undefined variable: on line 24

Notice: Undefined variable: id in PHP

"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP

"Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP

undefined variable notice when variable is defined

Notice: Use of undefined constant for already defined variable

PHP Notice: undefined variable, but they are already defined

weird situation where undefined variable notice is raised

PHP MySql - Notice: Undefined variable Error

why there is undefined index notice though variable is defined?

Severity notice: Undefined variable - images_model

SYMFONY4 : "Notice: Undefined variable: parentClass"

php warning session variable, Notice: Undefined index:

PHP error undefined variable notice how to fix?

Receiving Error: Notice: Undefined variable – but already defined

Notice: Undefined variable: grandTotal in PHP 7.1

Not able to fix "PHP Notice: Undefined variable" and "Undefined index" in code

Notice: Undefined variable: world in abc.php on line

Notice: Undefined variable: row when PDO returns no data

Notice : Undefined variable: result in new_admin.php on line 42

I am facing an Undefined Variable:Notice Error in PHP

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive