1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How To Add Up Values From Mysql Database & Display In PHP Help

Discussion in 'PHP' started by phplduser, Jul 25, 2020.

  1. #1
    Hi all,

    I'm trying yo do the following -
    SEMrush
    Lets say my database is called 'A', in A i have a table called 'B', in b i have a column called 'C' that records a number count everytime the page loads (visits), I'm trying to figure out how to add up the total count in 'C' and display the result using PHP?

    The database connection is ok, I just need help with writing a statement and then display the results, Any help would be great, I'm not a coder so don't know where to start!

    Best Regards
     
    phplduser, Jul 25, 2020 IP
    SEMrush
  2. Efetobor Agbontaen

    Efetobor Agbontaen Greenhorn

    Messages:
    27
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    18
    #2
    If I understand you correctly, then it's pretty easy. Use:

    SELECT SUM(C) FROM B;
    Code (markup):
     
    Efetobor Agbontaen, Jul 25, 2020 IP
    phplduser likes this.
  3. phplduser

    phplduser Member

    Messages:
    295
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Hi,

    Thank you, will give it a try, I take it I also need to echo the result using PHP as well?

    Best Regards
     
    phplduser, Jul 25, 2020 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    26,241
    Likes Received:
    3,867
    Best Answers:
    108
    Trophy Points:
    665
    #4
    Look up some mysql tutorials and work through them. They'll give you a grounding in how it all ties together.
     
    sarahk, Jul 25, 2020 IP
    phplduser likes this.
  5. Efetobor Agbontaen

    Efetobor Agbontaen Greenhorn

    Messages:
    27
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    18
    #5
    Oh it looks like you're quite far from having a working code. Like @sarahk said, your best bet is to get a tutorial on querying a mysql database with php. Or hire a developer to assist.

    If u consider the second option, I'm available....
    If you already setup your project, I could also help take a quick look and make edits
     
    Efetobor Agbontaen, Jul 25, 2020 IP
    phplduser likes this.
  6. phplduser

    phplduser Member

    Messages:
    295
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Hi,

    Thank you all for your advice and offer, will take a look online to see if I can figure it out by way of tutorials, if I get stuck will post the code for others to see!

    Best Regards
     
    phplduser, Jul 26, 2020 IP
  7. phplduser

    phplduser Member

    Messages:
    295
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    43
    #7
    Hi all,

    I have a question, Is this code for automatically connecting to a database?

    <?php
    if(!defined('IN_SCRIPT')) die("");
    ?>
    PHP:
    I have a database table called 'directory_listings', within this table is a column called 'visits' that has a numerical value, I'm trying to add up all the values in 'visits' and display them as a total number but first wondered if above code is the database connection.

    Best Regards
     
    phplduser, Jul 26, 2020 IP
  8. Efetobor Agbontaen

    Efetobor Agbontaen Greenhorn

    Messages:
    27
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    18
    #8
    No, this code has nothing to do with connecting to a database.

    It just checks if the IN_SCRIPT constant is declared. If it's not, php should terminate execution by dying =D

    It is usually used to prevent direct access to some php files from the URL. Php files that should only be called internally... but there are many other use cases
     
    Efetobor Agbontaen, Jul 26, 2020 IP
    phplduser likes this.
  9. phplduser

    phplduser Member

    Messages:
    295
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    43
    #9
    Hi,

    Ok, that explains a lot, i can run a query in the database to get the results i'm looking for and it gives me this code, Just need to figure out how to do on the site, so i now know that i have to create a database connection (many thanks).

    $sql = "SELECT SUM(visits) FROM directory_listings";
    PHP:
    Best Regards
     
    phplduser, Jul 26, 2020 IP
  10. Efetobor Agbontaen

    Efetobor Agbontaen Greenhorn

    Messages:
    27
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    18
    #10
    You should lookup tutorials on Database connections. Search for "How to connect to a mysql database with php PDO".

    Basically, your code should look like this:

    <?php
    $servername = "localhost";//usually localhost
    $username = "username";//your database user
    $password = "password";//the password of your database user
    
    try {
      $conn = new PDO("mysql:host=$servername;dbname=nameOfDB", $username, $password);
      echo "Connected successfully";
    
    
    $sql = "SELECT SUM(visits) FROM directory_listings";
    $query = $conn->prepare($sql);
      $query->execute();
    
    } catch(PDOException $e) {
      echo "Connection failed: " . $e->getMessage();
    }
    ?>
    
    PHP:
    Don't forget to close your Database connection otherwise the connection stays open and your hosting company will hate you :D

    Please verify the codes cause it's been long I wrote such. I use Frameworks that abstract the connection parts

    Edit
    Replace nameOfDB with the name of your DB
     
    Efetobor Agbontaen, Jul 26, 2020 IP
    phplduser likes this.
  11. phplduser

    phplduser Member

    Messages:
    295
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    43
    #11
    Hi,

    Thank you for taking the time to write your code, I’ve just sorted it out! More trail and error but it’s working.

    again, thank you!

    Best Regards
     
    phplduser, Jul 26, 2020 IP
    Efetobor Agbontaen likes this.
  12. NetStar

    NetStar Notable Member

    Messages:
    2,444
    Likes Received:
    528
    Best Answers:
    21
    Trophy Points:
    245
    #12
    Let me help you.... You need to LEARN PHP if you plan to write PHP. All this Copy & Paste and asking question after question will get you NO WHERE. In fact, it will take you even more time!!!!!! And your username is "phplduser" haha..
     
    NetStar, Aug 9, 2020 at 7:00 PM IP
  13. mmerlinn

    mmerlinn Prominent Member

    Messages:
    2,576
    Likes Received:
    508
    Best Answers:
    6
    Trophy Points:
    320
    #13
    Start by learning how to code. If you can read and write, you can code. Coding is nothing more than writing sentences that your computer can understand.
     
    mmerlinn, Aug 9, 2020 at 7:06 PM IP
    Efetobor Agbontaen likes this.