Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* contains a function that connects to server selects the DB, defines an error msg & returns a value */
- include ("connect.php");
- $count = 0; // count for creation & populating of tables
- $errMsg = ""; // $errMsg defined in connectDB() & returned via reference
- $db_connect = connectDB(&$errMsg);
- //************ create the friends table if it doesn't exist ************//
- $SqlQuery = "CREATE TABLE IF NOT EXISTS friends (friend_id INTEGER NOT NULL AUTO_INCREMENT
- PRIMARY KEY, friend_email VARCHAR(50) NOT NULL, password VARCHAR(20) NOT NULL,
- profile_name VARCHAR(30) NOT NULL, date_started DATE NOT NULL,
- num_of_friends INTEGER UNSIGNED)";
- $SqlResult = @mysqli_query($db_connect, $SqlQuery)
- or die("<p>Unable to create the friends table" . $errMsg . "</p>");
- //***************** check if the friends table exists *****************//
- $SqlQuery = "SELECT * FROM friends";
- $SqlResult = @mysqli_query($db_connect, $SqlQuery)
- or die("<p>Unable to find the friends table" . $errMsg . "</p>");
- //************** check if records exist in friends table ************//
- $numRows = @mysqli_num_rows($SqlResult);
- if ($numRows == 0){
- //************** Create 10 records into friends table *************//
- $SqlQuery = "INSERT INTO friends(friend_id, friend_email, password,
- profile_name, date_started, num_of_friends)
- VALUES(0001, 'bs@gmail.com', 'xtc99', 'BSamuels', '2013-05-05', 3),
- (NULL, 'starter77@yahoo.com.au', 'z1699', 'JAnt', '2013-05-05', 3),
- (NULL, 'dwnandout@gmail.com', 'gfd70', 'TomTom', '2013-05-05', 2),
- (NULL, 'lminelle@hotmail.com', 'eftg9', 'LizaM', '2013-05-05', 2),
- (NULL, 'sbeach@gmail.com', 'sb123', 'SandyB', '2013-05-05', 2),
- (NULL, 'newbie@gmail.com', 'neu73', 'NeuB', '2013-05-05', 2),
- (NULL, 'VerukaS@gmail.com', 'VS4me', 'VSalt', '2013-05-05', 2),
- (NULL, 'Maas@hotmail.com', 'M445', 'Maas', '2013-05-05', 2),
- (NULL, 'Ojay@gmail.com', '0j4y', 'Ojay', '2013-05-05', 2),
- (NULL, 'jb@yahoo.com.au', 'JB1977', 'Joadsters', '2013-05-05', 2)";
- $SqlResult = @mysqli_query($db_connect, $SqlQuery)
- or die("<p>Unable to add records to friends table" . $errMsg . "</p>");
- $count++;
- }
- //*********** create the myfriends table if it doesn't exist ***********//
- $SqlQuery = "CREATE TABLE IF NOT EXISTS myfriends (friend_id1 INTEGER NOT NULL,
- friend_id2 INTEGER NOT NULL)";
- $SqlResult = @mysqli_query($db_connect, $SqlQuery)
- or die("<p>Unable to create the myfriends table" . $errMsg . "</p>");
- //***************** check if the myfriends table exists *****************//
- $SqlQuery = "SELECT * FROM myfriends";
- $SqlResult = @mysqli_query($db_connect, $SqlQuery)
- or die("<p>Unable to find the myfriends table" . $errMsg . "</p>");
- //************** check if records exist in friends table ***************//
- $numRows = @mysqli_num_rows($SqlResult);
- if ($numRows == 0){
- //*************** Create 20 records into myfriends table **************//
- $SqlQuery = "INSERT INTO myfriends(friend_id1, friend_id2)
- VALUES(1, 2), (1, 3), (1, 6), (2, 1), (2, 3), (2, 6), (3, 1), (3, 2),
- (4, 5), (4, 7), (5, 4), (5, 7), (6, 1), (6, 2), (7, 4), (7, 5), (8, 9),
- (8, 10), (9, 8), (9, 10), (10, 8), (10, 9)";
- $SqlResult = @mysqli_query($db_connect, $SqlQuery)
- or die("<p>Unable to add records to the myfriends table" . $errMsg . "</p>");
- $count++;
- }
- @mysqli_close($db_connect);
- if ($count > 0){
- echo "<p><span class='content_text'>Tables successfully created and populated.</span></p>";
- } else {
- echo "<p><span class='content_text'>Tables and records already exist.</span></p>";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement