Jump to content

robot

New Members
  • Content Count

    4
  • Joined

  • Last visited

Everything posted by robot

  1. many thanks. There is some mistake in code. $stmt->execute(['post_id' => $_GET['post_id']); host = 'localhost'; $db = 'dbname'; $user = 'root'; $pass = ''; $charset = 'utf8'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $pdo = new PDO($dsn, $user, $pass, $opt); //Create a prepared statement $sql = "SELECT SUM(average) FROM Example GROUP BY post_id WHERE post_id = :post_id"; $stmt = $pdo->prepare($sql); //Execute the query, passign the parameter(s) as an array $stmt->execute(array('post_id' => $_GET['post_id'])); //Since there is a single valule - just fetch the column instead of the whole row $sumOfAvgForPostId = $sth->fetchColumn(); @Guru In database table there are many columns but two (post_id and average) are needed to calculate the average. The table for example has post_id where the id of the post has been saved. If somebody created a post the post id will be generated automatically. The column average contains the value if somebody rate the post. This will be written as integer value in the column average. For example 4 people created four different post. so there are 4 differnt post_ids. lets say 100, 200, 89, 59. now the column average - here will be the rating value saved by different poeple. The two columns in the table will be shown as: post_id average 100 90 100 80 100 80 100 90 68 70 68 70 68 70 89 100 59 100 59 50 59 50 Result what I want to show. If I will be on post with id 100 than 340*0.1 = 34 If I will be on post with id 70 than 210*0.1 = 21 If I will be on post with id 89 than 100*0.1 = 10 If I will be on post with id 59 than 200*0.1 = 20 At present I shows 21 on all of the post. this was the code for that $_sql = "SELECT SUM(average) FROM Example GROUP BY post_id"; $_daten = mysql_fetch_assoc($_result); $_datenavg = $_daten["SUM(average)"]; $_totalst = $_datenavg * 0.1; echo "Total:" .' ' .$_totalst ?> Now as Psycho recommended to write all in pdo and wrote a code. It doesnot function. $sql = "SELECT SUM(average) FROM Example GROUP BY post_id WHERE post_id = :post_id"; Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE post_id = ?' I am trying to do it without PDO. Thanks
  2. Many thanks. It works but for every post the average is same. I want to show according to post id. If postid is 89 than it should show the average of the postid 89 only. At present It shows on every post the same value. $_sql = "SELECT SUM(average) FROM Example GROUP BY post_id"; $_daten = mysql_fetch_assoc($_result); $_datenavg = $_daten["SUM(average)"]; $_totalst = $_datenavg * 0.1; echo "Total:" .' ' .$_totalst ?> Thanks a lot
  3. Hi All, There are two different field in database table. average here the number for e.g. 50 60 30 30 post_id here the id is integer 100, 159 89 89 and so on post_id is not primary key so the value can be repeated average field has also same values For static value it functions. for. e.g. If I write post id 89 than the result is 60. SELECT SUM(average) FROM example WHERE post_id = 89 The result = 60 I want to select the sum of average of each post_id. SELECT SUM(average) FROM example WHERE post_id = distinct post_id How can I write the post_id as variable? Thanks a lot
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.