Create 2nd tables and add data

Posted by Tyler Matema on Stack Overflow See other posts from Stack Overflow or by Tyler Matema
Published on 2012-10-29T16:37:02Z Indexed on 2012/10/29 17:00 UTC
Read the original article Hit count: 161

Filed under:

I have this task from school, and I am confuse and lost on how I got to do this. So basically I have to create 2 tables to the database but I have to created from php.

I have created the first table, but not the second one for some reason. And then, I have to populate first and second tables with 10 and 20 sample records respectively, populate, does it mean like adding more fake users? if so is it like the code shown below?

*I got error on the populating second part as well

Thank you so much for the help.

<?php
        $host = "host";
        $user = "me";
        $pswd = "password";
        $dbnm = "db";

        $conn = @mysqli_connect($host, $user, $pswd, $dbnm);
        if (!$conn)
            die ("<p>Couldn't connect to the server!<p>");

        $selectData = @mysqli_select_db ($conn, $dbnm);
        if(!$selectData)
        {
            die ("<p>Database Not Selected</p>");
        }

        //1st table
            $sql =  "CREATE TABLE IF NOT EXISTS  `friends`  
          (  
          `friend_id` INT NOT NULL auto_increment,   
          `friend_email` VARCHAR(20) NOT NULL,  
          `password` VARCHAR(20) NOT NULL,  
          `profile_name` VARCHAR(30) NOT NULL,  
          `date_started` DATE NOT NULL,  
          `num_of_friends` INT unsigned,  
          PRIMARY KEY (`friend_id`)  
          )";

        //2nd table
          $sqlMyfriends = "CREATE TABLE `myfriends`
          (  
          `friend_id1` INT NOT NULL,   
          `friend_id2` INT NOT NULL,
          )";


        $query_result1 = @mysqli_query($conn, $sql);
        $query_result2 = @mysqli_query($conn, $sqlMyfriends);

        //populating 1st table
        $sqlSt3="INSERT INTO friends (friend_id, friend_email, password, profile_name, date_started, num_of_friends) 
                VALUES('NULL','[email protected]','123','abc','2012-10-25', 5)";
        $queryResult3 = @mysqli_query($dbConnect,$sqlSt3)


        //populating 2nd table
        $sqlSt13="INSERT INTO myfriends VALUES(1,2)";
        $queryResult13=@mysqli_query($dbConnect,$sqlSt13);


        mysqli_close($conn);

    ?>

© Stack Overflow or respective owner

Related posts about php