SQL Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dreamy
    New Member
    • Jul 2009
    • 29

    SQL Question

    Can some 1 tearch how to insert data and view data from multiple table,
    by primary key and unique.
    thz
  • vinothPHP
    New Member
    • Jul 2009
    • 6

    #2
    hi dreamy,

    You need using XML document ? ?

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      You would use something like:

      Insert:
      Code:
      <?php 
      mysqli_query("INSERT INTO tablename VALUES (field1,field2,field3)");
      ?>
      Retrieve:
      Code:
      <?php
      $sql = mysql_query("SELECT * FROM tablename");
      $result = mysql_fetch_array($sql);
      while($result) {
         print $result['field1'].$result['field2'].$result['field3'].'<br/>';
      }
      ?>

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by dreamy
        Can some 1 tearch how to insert data and view data from multiple table,
        by primary key and unique.
        thz
        Hi.

        I'm not entirely sure I'm understanding you correctly. But...

        Inserting data into a database is pretty simple. There is no shortage of info available online to show you how that is done.

        Viewing data from multiple tables based on keys... that sounds to me like joins.
        Assuming you are using MySQL, you could check out 12.2.8.1. JOIN Syntax in the manual, or perhaps start with a nice Join Tutorial.

        If that's not what you are after, please elaborate.
        Originally posted by vinothPHP
        hi dreamy,

        You need using XML document ? ?
        Not sure how multiple tables and keys relate to XML documents. Those are RDBMS terms. XML is more... flat.

        Comment

        • dreamy
          New Member
          • Jul 2009
          • 29

          #5
          dear, friend, i not need use the XML documernt
          i try and c. thz

          Comment

          • dreamy
            New Member
            • Jul 2009
            • 29

            #6
            Can i ask how to get data to store in multiple table?
            and retrieve from multiple table?

            i got try many time but still got error.

            thz

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              There is a little doubt.

              Whether you want to store same data in multiple fields or different data to different fields?

              Comment

              • dreamy
                New Member
                • Jul 2009
                • 29

                #8
                Dear, dheerajjoshim
                thz and sorry.
                ok, i need to store different data to different field.
                but when retireve all different data is in same page.

                i got find the code as using method JOIN or LEFT JOIN.

                but i still can get error.

                i have aldy set the primary key and the foreign key,
                then how to recall the data by using primary key and foreign key?

                can i know the different bet store data in multiple fields and different data to different field?

                thz a lot

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Could you show us an example of the code that is causing your errors?

                  The method used to join tables based on a Primary Key and Foreign Key is pretty simple, really.

                  For example, if you have these tables:
                  Code:
                  Member
                  +-----+--------+
                  | MID | Name   |
                  +-----+--------+
                  |   1 | First  |
                  |   2 | Second |
                  |   3 | Third  |
                  +-----+--------+
                  
                  Contact
                  +-----+--------+-------+-------------------+
                  | CID | MID_FK | Type  | Value             |
                  +-----+--------+-------+-------------------+
                  |   1 |      1 | Email | first@example.com |
                  |   2 |      1 | Phone | 5885522           |
                  |   3 |      3 | Email | third@example.com |
                  +-----+--------+-------+-------------------+
                  You could get a list of emails by doing this:
                  [code=mysql]SELECT m.`MID`, m.`Name`, c.`Value`
                  FROM `Member` AS m
                  LEFT JOIN `Contact` AS c
                  ON c.`MID_FK` = m.`MID`
                  WHERE
                  c.`Type` = 'Email';[/code]
                  Which would return:
                  Code:
                  +-----+--------+-------------------+
                  | MID | Name   | Value             |
                  +-----+--------+-------------------+
                  |   1 | First  | first@example.com |
                  |   2 | Second | NULL              |
                  |   3 | Third  | third@example.com |
                  +-----+--------+-------------------+
                  Note that replacing the LEFT JOIN with an INNER JOIN would give the same results, except that it would exclude members that do not have an email listed. (In this case, the Second member.)

                  Comment

                  Working...