-4

problem i am facing is when i hit http://www.localhost/test1.php?user=10&num=10 irrespective of num=10&user=10. it displays all the data in the database . how to get particular data with respect to num or user for example num=5 or 6?

<?php
/* require the user as the parameter */
if(isset($_GET['user']) && intval($_GET['user'])) {

    /* soak in the passed variable or set our own */
    $number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
    $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
    $user_id = intval($_GET['user']); //no default

    /* connect to the db */
    $link = mysql_connect('localhost','username','pwd') or die('Cannot connect to the DB');
    mysql_select_db('marketing',$link) or die('Cannot select the DB');

    /* grab the posts from the db */
    $query = "SELECT * FROM data";
    $result = mysql_query($query,$link) or die('Errant query:  '.$query);

    /* create one master array of the records */
    $posts = array();
    if(mysql_num_rows($result)) {
        while($post = mysql_fetch_assoc($result)) {
            $posts[] = array('post'=>$post);
        }
    }

    /* output in necessary format */
    if($format == 'json') {
        header('Content-type: application/json');
        echo json_encode(array('posts'=>$posts));
    }
    else {
        header('Content-type: text/xml');
        echo '<posts>';
        foreach($posts as $index => $post) {
            if(is_array($post)) {
                foreach($post as $key => $value) {
                    echo '<',$key,'>';
                    if(is_array($value)) {
                        foreach($value as $tag => $val) {
                            echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
                        }
                    }
                    echo '</',$key,'>';
                }
            }
        }
        echo '</posts>';
    }

    /* disconnect from the db */
    @mysql_close($link);
}
?>

1 Answers1

0

In order to have sum of 2 columns saved into the third one automatically you have 2 options: 1. save directly from the code 2. create a trigger.

If a trigger is what you want - here is the trigger

CREATE TRIGGER ins_sum BEFORE INSERT ON user
FOR EACH ROW SET NEW.s3 = NEW.s1+NEW.s2;

Reference to detailed explanation: https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html

And from the code:

INSERT INTO users (s1,s2,s3) values (val1, val2, val1+val2)

Of course here you should change the val1 and val2 to your actual values.

Tata
  • 802
  • 9
  • 19
  • thanks tata. but if i later change the value of s1 or s2 . the value of s remains unchanged means it does not get updated as soon the value is changed. please advice – Nakul Gupta Aug 12 '15 at 08:37
  • to do that you need another trigger. `CREATE TRIGGER ins_sum BEFORE UPDATE ON user FOR EACH ROW SET NEW.s3 = NEW.s1+NEW.s2;` – Tata Aug 12 '15 at 10:39
  • thanks :) . also can you please guide me how can i generate random numeric values from 0 to 50 in s2 column. thanks in advance – Nakul Gupta Aug 12 '15 at 11:11
  • you have a function in mysql rand(). So for values from 0 50 50 - rand()*50 – Tata Aug 12 '15 at 12:07
  • SELECT * FROM data - says select everything from the DB, and it is exactly what happens. Check this link for more info https://dev.mysql.com/doc/refman/5.6/en/select.html. n your case you should go with select * from data where user=X and num=Y. (or something like that) . And I strongly suggest replacing the * with a list of the actual fields that you need to retreive. – Tata Aug 15 '15 at 20:08