Jump to content

I am trying to figure out how to write this function so I can pass a variable to it from a foreach loop and return two variables that I can echo.

 

I know the first function ( get_uniq_tcp_ports ) works, i tested it before I started working on the count_tcp function.

function count_tcp($port) {        global $db;        $query = "SELECT dpt,count(dpt) FROM logs WHERE proto = 'tcp' and dpt = :port";        $statement = $db->prepare($query);        $statement->execute();        $statement->bindValue(":port", $port);        $array = $statement->fetch();        $statement->closeCursor();        return $test = array($dpt,$count);}<html><h2>TCP</h2>        <?php $ports = get_uniq_tcp_ports(); foreach ($ports as $port) : ?>                <?php count_tcp($port);                 echo $test[$dpt]; echo $test[$count]; ?></br>        <?php endforeach; ?></html> 
Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/
Share on other sites

Link to post
Share on other sites

Maybe @Limecat86 can help.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6434768
Share on other sites

Link to post
Share on other sites

-snip-

What is not working? And shouldn't the html be in a <body>?

If you are gonna answer me, please do quote me. Going to sleep now, but will try to help you tomorrow if your issue is not resolved. 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6434899
Share on other sites

Link to post
Share on other sites

What is not working? And shouldn't the html be in a <body>?

If you are gonna answer me, please do quote me. Going to sleep now, but will try to help you tomorrow if your issue is not resolved. 

 

I have no output currently - I dont even see my html tags, so I suspect the function is wrong.

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6434930
Share on other sites

Link to post
Share on other sites

<?php

function count_tcp($port) {

        global $db;

        $query = "SELECT dpt,count(dpt) FROM logs WHERE proto = 'tcp' and dpt = :port";

        $query_params = array(':port' => , $port);

 

        $statement = $db->prepare($query);

        $statement->execute($query_params);

 

        $row = $statement->fetch();

        return $row;

}

?>

 

<html>

    <body>

        <h2>TCP</h2>

        <?php 

 

        $ports = get_uniq_tcp_ports(); 

        foreach ($ports as $port) {

             $row = count_tcp($port);

             echo $row['dpt'] . " | " . $row['count(dpt)'];

        } 

        ?>

    </body>

</html> 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6434934
Share on other sites

Link to post
Share on other sites

Ok, obviously I can't execute your code without the DB and the rest of the code but there are a few things I can see that don't look correct.

 

1.

        $array = $statement->fetch();        $statement->closeCursor();        return $test = array($dpt,$count);

Here you are fetching the data from the results into $array which I assume will look something like:

array(    'dpt' => 'value'    'count' => 1,)

Yet further down you never use $array and instead try and use $dpt and $count which are unset variables. Try just returning $array instead.

 

2.

<?php $ports = get_uniq_tcp_ports(); foreach ($ports as $port) : ?>

Please put this on two lines. There should never be code on a line after a ;

 

3.

<?php count_tcp($port);

This will return data from your function (when written correctly) but without stating where the returned data will be stored it will be lost. You need something like:

<?php $tcpResult = count_tcp($port);

4.

echo $test[$dpt]; echo $test[$count]; ?></br>

Again on this line $test, $dpt and $count are undefined. Using the changes in point 3 you should be able to access your data like:

$tcpResult['dpt']

Again you shouldn't have code after a ; but we can concatenate the string using periods so this line should look something like:

echo $tcpResult['dpt']." ".$tcpResult['count']; ?></br>
Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6434941
Share on other sites

Link to post
Share on other sites

I have no output currently - I dont even see my html tags, so I suspect the function is wrong.

-snip-

If this resolves it, I saw the error :P

Unijab, you forgot to put <body> tags inside the html part of the code.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6434949
Share on other sites

Link to post
Share on other sites

If this resolves it, I saw the error :P

Unijab, you forgot to put <body> tags inside the html part of the code.

Nah, that's not it. 

 

Refer to post above for a good explanation. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6434957
Share on other sites

Link to post
Share on other sites

<?phpfunction count_tcp($port) {        global $db;        $query = "SELECT dpt,count(dpt) FROM logs WHERE proto = 'tcp' and dpt = :port";        $query_params = array(':port' => , $port);         $statement = $db->prepare($query);        $statement->execute($query_params);         $row = $statement->fetch();        return $row;}?> <html>    <body>        <h2>TCP</h2>        <?php          $ports = get_uniq_tcp_ports();         foreach ($ports as $port) {             $row = count_tcp($port);             echo $row['dpt'] . " | " . $row['count(dpt)'];        }         ?>    </body></html> 

 

I have a blank page after making these changes.

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6434983
Share on other sites

Link to post
Share on other sites

I have a blank page after making these changes.

Oops, missed semi colon and additional comma (line 23 for semi-colon and 5 for comma). 

<?phpfunction count_tcp($port) {    global $db;    $query = "SELECT dpt,count(dpt) FROM logs WHERE proto = 'tcp' and dpt = :port";    $query_params = array(':port' => $port);    $statement = $db->prepare($query);    $statement->execute($query_params);    $row = $statement->fetch();    return $row;}?><html>    <body>        <h2>TCP</h2>        <?php         $ports = get_uniq_tcp_ports();         foreach ($ports as $port) {             $row = count_tcp($port);             echo $row['dpt'] . " | " . $row['count(dpt)'];        }         ?>    </body></html> 
EDIT: Also, try looking through your error.log file to help you troubleshoot the issue.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435003
Share on other sites

Link to post
Share on other sites

If this resolves it, I saw the error :P

Unijab, you forgot to put <body> tags inside the html part of the code.

 

While correct use of HTML tags should always be done, most modern browsers will automatically add them in where required so missing them out will rarely break a webpage any more

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435029
Share on other sites

Link to post
Share on other sites

Oops, missed semi colon and additional comma (line 23 for semi-colon and 5 for comma). 

<?phpfunction count_tcp($port) {    global $db;    $query = "SELECT dpt,count(dpt) FROM logs WHERE proto = 'tcp' and dpt = :port";    $query_params = array(':port' => $port);    $statement = $db->prepare($query);    $statement->execute($query_params);    $row = $statement->fetch();    return $row;}?><html>    <body>        <h2>TCP</h2>        <?php         $ports = get_uniq_tcp_ports();         foreach ($ports as $port) {             $row = count_tcp($port);             echo $row['dpt'] . " | " . $row['count(dpt)'];        }         ?>    </body></html> 

 

These changes resulted in many lines of the following.

 

| 0

| 0

| 0

| 0

| 0

| 0

| 0

| 0 

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435055
Share on other sites

Link to post
Share on other sites

 

Ok, obviously I can't execute your code without the DB and the rest of the code but there are a few things I can see that don't look correct.

 

1.

        $array = $statement->fetch();        $statement->closeCursor();        return $test = array($dpt,$count);

Here you are fetching the data from the results into $array which I assume will look something like:

array(    'dpt' => 'value'    'count' => 1,)

Yet further down you never use $array and instead try and use $dpt and $count which are unset variables. Try just returning $array instead.

 

2.

<?php $ports = get_uniq_tcp_ports(); foreach ($ports as $port) : ?>

Please put this on two lines. There should never be code on a line after a ;

 

3.

<?php count_tcp($port);

This will return data from your function (when written correctly) but without stating where the returned data will be stored it will be lost. You need something like:

<?php $tcpResult = count_tcp($port);

4.

echo $test[$dpt]; echo $test[$count]; ?></br>

Again on this line $test, $dpt and $count are undefined. Using the changes in point 3 you should be able to access your data like:

$tcpResult['dpt']

Again you shouldn't have code after a ; but we can concatenate the string using periods so this line should look something like:

echo $tcpResult['dpt']." ".$tcpResult['count']; ?></br>

 

 

1

The output from the sql query ran from phpmyadmin gives....

 

Capture.png

 

 

 

2

I am entirely a noob at php... I dont really understand how to work with arrays and returns and functions.

 

 

3-5

I'll give those changes a try and return

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435086
Share on other sites

Link to post
Share on other sites

1

The output from the sql query ran from phpmyadmin gives....

 

Capture.png

 

In that case change this line:

$query = "SELECT dpt,count(dpt) FROM logs WHERE proto = 'tcp' and dpt = :port";

to:

$query = "SELECT dpt,count(dpt) AS count FROM logs WHERE proto = 'tcp' and dpt = :port";

This will change the 'count(dpt)' column to 'count' which is a bit easier to read

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435127
Share on other sites

Link to post
Share on other sites

In that case change this line:

$query = "SELECT dpt,count(dpt) FROM logs WHERE proto = 'tcp' and dpt = :port";

to:

$query = "SELECT dpt,count(dpt) AS count FROM logs WHERE proto = 'tcp' and dpt = :port";

This will change the 'count(dpt)' column to 'count' which is a bit easier to read

 

How do I return those two columns so I can echo them?

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435145
Share on other sites

Link to post
Share on other sites

How do I return those two columns so I can echo them?

 

This line here fetches the result:

$array = $statement->fetch();

If you want to see the result after this line you could do:

var_dump($array);

This will show you what is stored in $array. It should look similar to:

array(    'dpt' => 'value'    'count' => 1,)
Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435171
Share on other sites

Link to post
Share on other sites

 

This line here fetches the result:

$array = $statement->fetch();

If you want to see the result after this line you could do:

var_dump($array);

This will show you what is stored in $array. It should look similar to:

array(    'dpt' => 'value'    'count' => 1,)

 

Currently getting this repeated

on var_dump

 

array(2) { ["dpt"]=> NULL ["count"]=> NULL }

array(2) { ["dpt"]=> NULL ["count"]=> NULL }

array(2) { ["dpt"]=> NULL ["count"]=> NULL }

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435210
Share on other sites

Link to post
Share on other sites

What does get_uniq_tcp_ports() return?

 

 

With my php as the following I get this output which is a list of the unique integers in that column of the sql table.

<?php $ports = get_uniq_tcp_ports();                foreach ($ports as $port) {                        echo $port[dpt];                        echo '</br>';                } ?>

Capture_1.png

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435245
Share on other sites

Link to post
Share on other sites

Ok, so I'm guessing your $ports array has 3 results in it. The count_tcp seems to be returning the array but there are no results.

Can you post your code as you have it now just to check please

Its his for each loop causing problems.

Your for each loop iterates through an array, but when you call the function to count the port you give it an array, not a number.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435300
Share on other sites

Link to post
Share on other sites

This looks promising.....

I actually got var_dump to display something other than false or NULL

 

 

Code:

// Get all distinct tcp portsfunction get_uniq_tcp_ports() {      global $db;      $query = "SELECT distinct(dpt) FROM logs where proto = 'tcp' ";      $statement = $db->prepare($query);      $statement->execute();      $ports = $statement->fetchAll();      $statement->closeCursor();      return $ports;}// Count the portsfunction count_tcp($port) {        global $db;        $query = "SELECT dpt,count(dpt) as count FROM logs WHERE proto = 'tcp' and dpt = :port";        $statement = $db->prepare($query);        $statement->bindValue(":port", $port);        $statement->execute();        $row = $statement->fetch();        $statement->closeCursor();        return $row;}<?php $ports = get_uniq_tcp_ports();                foreach ($ports as $port) {                        $tcpResult = count_tcp($port[0]);                        var_dump($tcpResult);                 } ?>

Dump:

 

Capture_2.png

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
https://linustechtips.com/topic/479705-php-help-please/#findComment-6435477
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×