Page 1 of 1

Custom Page - Column Sum

Posted: 2020-10-08 17:38
by jangle
Hello Forum,

I am trying to echo the sum of a column from Mysql DB on a custom PHP page. The DB connection seems to work ok, but I get a Call to undefined function mysql_query() in C:\xampp\htdocs\COVID-19\hooks\COVIDReports.php:19 error when loading the page.

Anything look wrong????

Thanks in advance.

Jim

Code: Select all

<?php
    $servername = "localhost";
    $database = "serp";
    $username = "root";
    $password =  "";
    
    // Create connection
    
    $conn = mysqli_connect($servername, $username, $password, $database);
    
    // Check connection
    
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }    
    echo 'Connected successfully';
    
    
    $result= mysql_query("SELECT SUM(workforce_out) AS totalsum FROM workforce_impact_report_30_days");
    $row = mysql_fetch_assoc($result); 

    $sum = $row['totalsum'];

    echo ("This is the sum: $sum");

    
    mysqli_close($conn); 

    
    
    ?>

Re: Custom Page - Column Sum

Posted: 2020-10-08 18:20
by jsetzer
I recommend using the AppGini built in sql-function if you fetch data from current database.

Are you sure you mean mysql_* functions?
I guess you will have to use mysqli_* functions instead.
I think you have mixed up mysqli-functons and mysql-functions.

Can you please ask your questions in the forum, not as private message to me, then, also other users can help you. Thanks.

Re: Custom Page - Column Sum

Posted: 2020-10-08 19:48
by jangle
I did post this in the forum..... I did sent you a PM with regard to a comment the you sent to me. Did you get that?

Re: Custom Page - Column Sum

Posted: 2020-10-08 19:50
by jangle
Thanks

Can you give me an example of how to use this "AppGini built in sql-function"?

Thanks

Re: Custom Page - Column Sum

Posted: 2020-10-08 20:19
by jsetzer
I must have missed that, sorry!!!

Re: Custom Page - Column Sum

Posted: 2020-10-08 21:52
by jangle
Copy just wanted to make sure you got it.

Can you give me an example of the built in appgini function that you mention.

Thanks

Re: Custom Page - Column Sum

Posted: 2020-10-09 04:25
by jsetzer

Code: Select all

$eo = null;
$rows = [];
$res = sql("select * from TABLENAME", $eo);
if (!$eo) { 
  while($row = db_fetch_assoc($res)) 
    $rows[] = $row;
}

Re: Custom Page - Column Sum

Posted: 2020-10-09 12:57
by jangle
Thank you very much. I will try out....

Re: Custom Page - Column Sum

Posted: 2020-10-09 13:10
by jangle
I am not sure how to make this sum the column and echo the result...?????

Code: Select all

$eo = null;
$rows = [];
$res = sql("select * from TABLENAME", $eo);
if (!$eo) { 
  while($row = db_fetch_assoc($res)) 
    $rows[] = $row;
}

Re: Custom Page - Column Sum

Posted: 2020-10-09 13:14
by jangle
I got it with this.... thanks Jan!!!

Code: Select all

  $result = mysqli_query($conn, 'SELECT SUM(workforce_out) AS value_sum FROM workforce_impact_report_30_days'); 
$row = mysqli_fetch_assoc($result); 
$sum = $row['value_sum'];

    
    echo ("This is the sum: $sum"); 

Re: Custom Page - Column Sum

Posted: 2020-10-09 13:17
by jsetzer
Well done. I'm wondering if $conn is the same DB or a different datasource.

If this is the same DB, you can also use the built-in sqlValue()-function:

Code: Select all

$sum = sqlValue('SELECT SUM(workforce_out) FROM workforce_impact_report_30_days'); 

Re: Custom Page - Column Sum

Posted: 2020-10-16 02:51
by jangle
Thanks Jan

It is the same Db connection.

Re: Custom Page - Column Sum

Posted: 2020-11-08 15:20
by landinialejandro
jangle wrote:
2020-10-09 13:10
I am not sure how to make this sum the column and echo the result...?????

Code: Select all

$eo = null;
$rows = [];
$res = sql("select * from TABLENAME", $eo);
if (!$eo) { 
  while($row = db_fetch_assoc($res)) 
    $rows[] = $row;
}
hi folow the next link to help you how to create a custom page.
https://bigprof.com/appgini/help/advanc ... cess-pages
the most importat is the lib.php.

then you can use de sql function.
Just like Jan sai exist the sqlValue function, this fucntion return a only one value.

Code: Select all

$sum = sqlValue('SELECT SUM(workforce_out)  FROM workforce_impact_report_30_days');
echo $sum;

Re: Custom Page - Column Sum

Posted: 2020-11-20 13:39
by jangle
Thanks all!!!