0

I'm new to PHP MySqli and this is my first project. I'm trying to return a value from a function by placing in an array.

function riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to){
    global $connection;
    $sql = "SELECT ".$id."  FROM ".$datatable." WHERE ".$id1." BETWEEN '".$date_from."' AND '".$date_to."' AND ".$id." = '".$type."'";

    if ($result = mysqli_query($connection,$sql)) {
        $count = 0;
        while ($row = mysqli_fetch_array($result)) {
            $count = ++ $count; 
        }
        echo "<br>".$type.": " .  $count;
        $counter[$type] = $count;
        return $counter;      
    }

    $type = 'RIDDOR - Major Injury';
    riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to);
    var_dump($counter);

The function works to a point where it will print the result which is baically various counts for the array. However, i need to use the return in a table elsewhere but the var-dump just returns NULL.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
JohnM
  • 71
  • 7
  • 4
    You forgot to assign the returned value to a var just like `$counter = riddor_dates(...)` – B001ᛦ Jan 09 '19 at 11:45
  • [Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Masivuye Cokile Jan 09 '19 at 11:46
  • You also appear to have missed the terminating `}` in the function definition – RiggsFolly Jan 09 '19 at 11:48
  • 1
    Counting can be done with one query `SELECT count()`. – u_mulder Jan 09 '19 at 11:48
  • 1
    Or by `mysql_num_rows` if you also need the data itself – dWinder Jan 09 '19 at 11:49

3 Answers3

1

As all the comment above - you are using $counter as if it global variable - if that is the case you can add global $counter at the begin of the function else if you want to use as return value you can add it to the function argument.

Option 1 - use global variable

function riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to){
    global $connection;
    global $counter;
    $sql = "SELECT ".$id."  FROM ".$datatable." WHERE ".$id1." BETWEEN '".$date_from."' AND '".$date_to."' AND ".$id." = '".$type."'";

    if ($result = mysqli_query($connection,$sql)) {
        $count = mysqli_num_rows($result);
        echo "<br>".$type.": " .  $count;
        $counter[$type] = $count;     
    }
}

$type = 'RIDDOR - Major Injury';
riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to);
var_dump($counter);

Option 2 - use return value

function riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to){
    global $connection;
    $sql = "SELECT ".$id."  FROM ".$datatable." WHERE ".$id1." BETWEEN '".$date_from."' AND '".$date_to."' AND ".$id." = '".$type."'";

    if ($result = mysqli_query($connection,$sql)) {
        $count = mysqli_num_rows($result);
        echo "<br>".$type.": " .  $count;
        return $count;     
    }
}

$type = 'RIDDOR - Major Injury';
$counter[$type] = riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to);
var_dump($counter);

I strongly recommend the second option to avoid using global...

dWinder
  • 11,597
  • 3
  • 24
  • 39
  • Thanks for your reply and it kind of makes sense in my head - Although I've tried both options but it just stops the page displaying. I will need to try at home later to try and find the error that's crashing it. As i can only try on the LIVE site. – JohnM Jan 09 '19 at 12:20
  • You welcome - please tell me if you encounter any issues – dWinder Jan 09 '19 at 12:21
  • _use global variable_ No..hands off! ;) – B001ᛦ Jan 09 '19 at 12:33
  • I have to agree - I preffer the second option but look like the OP original code behave as counter is global... – dWinder Jan 09 '19 at 12:35
  • _original code behave as counter is global..._ IMHO in this case OP would have had the value and they wouldnt have asked the question ... :) – B001ᛦ Jan 09 '19 at 12:44
  • Not sure - if he didn't add the `global` then he also been wonder why the `var_dump($counter);` empty... because the function create new var name `$counter` and return it without no var catching the function return value – dWinder Jan 09 '19 at 13:13
  • Hi David, I’ve managed to install MySQL on my work machine with the DB. The error was simply a ‘i’ missing from $count = mysq(i)_num_rows($result); on both options. It works perfectly now exactly how i wanted. It feels like a very long winded way to get my results. I’m very new to PhP/ MySQL and this is the first project from scratch. Basically this was the only way with my knowledge to call an array from database then count all the various elements in it to echo later into a table. Thank you again very much appreciated :-) – JohnM Jan 09 '19 at 13:59
  • @DavidWinder thank you and marked done. This has given me hours or headache :-) – JohnM Jan 09 '19 at 14:26
0

Your code should be as given below. Instead of counting in php, you can count it in query.

function riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to){
    global $connection; global $counter;
    $sql = "SELECT COUNT(".$id.") as cnt FROM ".$datatable." WHERE ".$id1." BETWEEN '".$date_from."' AND '".$date_to."' AND ".$id." = '".$type."'";
    $count = 0;
    if($result = mysqli_query($connection,$sql)) {
       if ($row = mysqli_fetch_assoc($result)) {
            $count = $row['cnt']; 
        }  
    }
    $counter[$type] = $count;
}
$type = 'RIDDOR - Major Injury';
riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to);
var_dump($counter);
Dinesh Belkare
  • 639
  • 8
  • 24
  • 1
    I think you can assume `$counter` already has value before this call (the function just assign the type in the array) – dWinder Jan 09 '19 at 11:57
  • 1
    If that the case notice you override `$counter` in every call to the function – dWinder Jan 09 '19 at 12:06
0

you seem to be using a expensive approach. There are functions by which you can easily get the query row count.

function riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to){
global $connection;
$sql = "SELECT ".$id."  FROM ".$datatable." WHERE ".$id1." BETWEEN '".$date_from."' AND '".$date_to."' AND ".$id." = '".$type."'";
$result = mysqli_query($connection,$sql)

return mysqli_num_rows($result);
}

$type = 'RIDDOR - Major Injury';
$counter = riddor_dates($type,$id,$id1,$datatable,$date_from,$date_to);
var_dump($counter);
Arman sheikh
  • 176
  • 2
  • 7
  • many thanks for helping. I've used the 'Option 2 - use return value' above by David Winder which works perfectly for what i wanted it to do. It does feel a really long way round but with my current knowledge it does what i need for now till i learn more. Thanks again for your input :-) – JohnM Jan 09 '19 at 14:23