PHP - Compare Two Tables From Two Databases
Hi!
I have two similar tables on two different databases and i need to check if there are any entries on the table1, for example, that there aren't on the second table and print if there are i need to print them. Any suggestions? Thank you... Similar TutorialsI'm working on a web based RPG game, and I want the ability to run different "servers" but I'm not sure what the best way to go about it would be.
First idea: Create a table which would be named, "world" when a character is created, they get to choose a world to join, and then use world_id to know what items, mobs, npc, is in that world. Here is just a quick mock-up of the table.
Second idea: Create a whole new database for the game data, creature_game contains the accounts and forum data. While creature_server_x contains the rest of the game. The structure of each database would be exactly the same.
I want the people that runs the different "servers" to be able to do whatever changes they want, even if that would mean to rewrite the story line. I could do it with both the first and second idea. But I want to hear about what people here think is best and why. Ofc, they won't be able to access or change the tables inside the databases, just the data the tables contains.
Hi Everyone, I am working on developing a blog/comment system for my website. I have 3 tables, the first a blog post table with a row/primary key of id that is auto incremented as each new article is created via a form. The second table is a comments table with a row of article_id and the third table is a category table with an article_id row as well. From the main blog page I have links that will take users to each individual blog post using $_GET like so: Code: [Select] http://www.mydomain.com/blog/article/?id={$row['id']} From here I would like to be able to display the comment for that particular article below the article and also have a sidebar with links to blog categories which will then display each post according to whether they are in that category or not. My question is two-fold: First, what is the best way to pull the id field from the blog/post table, form, or url and enter it into the article_id rows of the second two tables? Should I use a hidden field, the URL id via $_GET, using a join, or what? Secondly: What is the best way to compare and display those rows so that the appropriate comments are being shown for the correct article, etc. This is my first time doing this so I am grateful for any help or suggestions. Thanks, kaiman Hi guys! Once again i require your endless talents! I have 2 tables in my mysql DB. One table is a list of products a store offers, the other is the cart of the user. I need to find the most efficient way or retrieving the images and prices from the LIST OF PRODUCTS table based on the list of PRODUCT NUMBERS stored in the cart table of the user. List of products offered ************************* Prod # * Price * img src * ************************* 001 * 19.99* imgs/pic1.jpg * 002 * 29.99 * imgs/pic2.jpg * ************************* User's Cart ********************* Prod # * quantity * size * ********************* 001 * 10 * m * 002 * 4 * s * ******************** Basically i just want to retrieve ONLY the rows that i need (ones that the user has added to his/her cart) and ASSOCIATE the img src with the quantity and size so that i can lay it out in a table on a view_cart page... If you want a good laugh just take a look at what ive done so far... Code: [Select] $cart = mysql_query("SELECT * FROM ".$session.""); $cart_row_count = mysql_fetch_assoc($cart); $cart_row_count = mysql_num_rows($cart_row_count); if($cart_row_count != 0){ $i = 1; while($cart_result = mysql_fetch_assoc($cart)){ ${"product$i"} = $cart_result['product']; ${"size$i"} = $cart_result['size']; $i++; } $i2 = 1; $query = "product = ".${"$product$i2"}." "; while($i2 <= $i){ $i2++; $query .= "AND ".${"$product$i2"}." "; } } Hey Guys, I'm struggling with some logic of how to do something wondering if anyone can point me in the right direction. Details: I am making a PHP/MySQL game to learn more PHP, I've got my login/logout system created. However I am planning on the game been map based on a 100x100 grid. I have two tables: USERS Username, Password, x, y Test, Test, 1, 1 MAP X, Y, type 0,1,1 0,2,1 0,3,2 0,4,3 0,5,2 etc I am stuck on how to get the user's logged in X and Y coordinates and then compare them with the map table X and Y, to see what type of field to show, 1,2 or 3. Is there a name for this type of function so I can do some research on it? Or could someone give me an example? Would be fantastic if anyone has any advice. Thanks in advance NewcastleFan I have a cart script, and I need to display the products the user has added. This is a school assignment and I'm very stuck on this seemingly 'little' error. Right now, I have two tables: For time's sake, I am showing duplicate products with different prod_id's: Table: products prod_id category sub_category thumb_href image_href title desc summary manufacturer price 17 sofa leather red_leather_sofa_small.jpg red_leather_sofa_main.jpg Red Leather Sofa [BLOB - 134B] [BLOB - 59B] Balenty 999.99 18 sofa leather red_leather_sofa_small.jpg red_leather_sofa_main.jpg Red Leather Sofa [BLOB - 134B] [BLOB - 59B] Balenty 999.99 19 sofa leather red_leather_sofa_small.jpg red_leather_sofa_main.jpg Red Leather Sofa [BLOB - 134B] [BLOB - 59B] Balenty 999.99 And this is the cart table that holds temporary cart items (storing the product ID to pull later): Table: cart temp_cart_item cart_id (assigned by session_id() ) prod_id user_id 22 jier11u0e7cl2ghosjodpaark2 17 asdfasdf 23 jier11u0e7cl2ghosjodpaark2 17 asdfasdf 24 jier11u0e7cl2ghosjodpaark2 35 asdfasdf 25 jier11u0e7cl2ghosjodpaark2 5 asdfasdf 26 jier11u0e7cl2ghosjodpaark2 19 asdfasdf SO, user 'asdfasdf' is logged in and has selected 5 items for his cart - two of which are the same (prod_id 17). Basically, I need to compare the prod_id's of each table and output all products (and quantities) matching the temporary cart prod_id. What I have now to loop through and display the cart items are (bear with me): echo "<h1>Cart</h1>"; } // Display welcome and navigation links displayWelcome(); displayNav(); // Getting the prod_id's from the table cart to compare with the product table $cart_id = session_id(); $user_id = ($_SESSION['login_username']); $query = "SELECT prod_id FROM cart WHERE cart_id='$cart_id' AND user_id='$user_id'"; $result = mysqli_query($cxn,$query) or die("<h2 class=\"warning\">Whoa! Problem with the cart script here!</h2>"); $nrows = mysqli_num_rows($result); if($nrows < 1) { echo "<h2>Your cart is empty</h2><p><a href=\"catalog.php\">Go back to the catalog</a></p>"; } else { // Continue to gather products for all the product IDs in the products DB table... echo "<h4>$nrows items in your cart</h4>"; while($row = mysqli_fetch_array( $result )) { $user_id = ($_SESSION['login_username']); $product = $row['prod_id']; // Is the query my problem? $query = "SELECT * FROM products WHERE prod_id='$product'"; $result = mysqli_query($cxn,$query) or die("<h2 class=\"warning\">Whoa! Problem with the cart script here!</h2>"); $nrows = mysqli_num_rows($result); if($nrows < 1) { // If the item doesn't exist, display an error echo "<h2>That is not a valid product, or any of the items you had in your cart are missing or have been deleted.</h2><p><a href=\"catalog.php\">Go back to the catalog</a></p>"; // Protect from continually displaying an error by deleting the invalid cart record $deletequery = "DELETE FROM cart WHERE prod_id='$product'"; $deleteresult = mysqli_query($cxn,$deletequery) or die("<h2 class=\"warning\">Whoa! Problem with the cart script here!</h2>"); exit(); } while($row = mysqli_fetch_array( $result )) { // Then disply them in a table echo "<h2>Items in Your Cart:</h2>"; echo " <table id=\"products\"> <thead> <tr> <th class=\"col1 headercol\" scope=\"col\">Thumbnail</th> <th class=\"col2 headercol\" scope=\"col\">Summary</th> <th class=\"col3 headercol\" scope=\"col\">Manufacturer</th> <th class=\"col4 headercol\" scope=\"col\">Price</th> </tr> </thead> <tbody>\n"; $query = "SELECT SUM(price) FROM products WHERE prod_id='$product' GROUP BY price"; $priceResult = mysqli_query($cxn,$query) or die("<h2 class=\"warning\">Whoa! Problem with the cart script here!</h2>"); // keeps getting the next row until there are no more to get // Print out the contents of each row into a table echo "<tr> <td class=\"col1\">"; echo "<a class=\"imgHref\" href=\"detail.php?prod_id=".$row['prod_id']."\" title=\"".$row['title']."\"><img src=\"../images/".$row['thumb_href']."\" alt=\"".$row['title']."\" /></a>"; echo "</td>\n"; echo " <td class=\"col2\">"; echo "<h3 class=\"title\"><a href=\"detail.php?prod_id=".$row['prod_id']."\" title=\"".$row['title']."\">".$row['title']."</a> <span class=\"inlineh3\">by <a href=\"categories.php?manufacturer=".$row['manufacturer']."\" title=\"View all products by ".$row['manufacturer']."\">".$row['manufacturer']."</a></span></h3>"; echo "<p class=\"summary\">".$row['summary']."</p> <p class=\"descHeading\">&#9758; <em>Full Description:</em></p> <p class=\"desc\">".$row['desc']."</p>"; echo "</td>\n"; echo " <td class=\"col3\">"; echo "<p class=\"manufacturer\"><a href=\"categories.php?manufacturer=".$row['manufacturer']."\" title=\"View all products by ".$row['manufacturer']."\">".$row['manufacturer']."</a></p>"; echo "</td>\n"; echo " <td class=\"col4\">"; echo "<p class=\"price\">$".$row['price']."</p>"; echo "</td> </tr>\n"; } while($priceRow = mysqli_fetch_array( $priceResult )) { echo "<tr class=\"short\"><td class=\"col1\"><h3><a href=\"clear_cart.php\">Empty Cart</a></h3></td><td class=\"col2\"></td><td class=\"col3 nobackground\">Total:</td><td class=\"col4\">$".$priceRow[0]."</td></tr>"; } echo "</tbody> </table>"; echo "<p class=\"submitOrder\"><form action='submit_order.php' method='post' id='form'> <input type='submit' name='pButton' value='Submit Order'> <input name='order_total' type='hidden' id='order_total' value='".$priceRow[0]."' /> </form> </p>"; } } And so the cart.php looks something like this (this example asdfasdf has 6 items in his cart): So you can see that it only displays ONE item, even though asdfasdf DOES have 6 items in his cart (proven by checking database). And the price is not accurate, either. Can anyone help me in where my problem might be? Is it my query? Or my while clause? Just wondering, which is better php databases or sql? I have phpMyAdmin and I've that you can convert the database into php scripts? ok , here is my mysql code to get all posts from the posts table . Code: [Select] $query = mysql_query("SELECT id,to_id,from_id,post,type,state,date FROM posts WHERE state='0' ORDER BY id DESC LIMIT 50"); and here is the code to display the users friends... Code: [Select] $sqlArray = mysql_query("SELECT friend_array FROM myMembers WHERE id='" . $logOptions_id ."' LIMIT 1"); while($row=mysql_fetch_array($sqlArray)) { $iFriend_array = $row["friend_array"]; } $iFriend_array = explode(",", $iFriend_array); if (in_array($id, $iFriend_array))see now i got as far as , if(in_array($id, $iFriend_array)) How would i put these togeather to where it would get the posts from the posts table that there friends posted? I have a site where I need to have lets call it image1 displayed, then I want to change this image based on a php if statement, for instance: if $var == $var2 change the image ....blah blah so I was also going to have the names of my images stored in my database, i.e. image1.jpg and image2.jpg in my database. the image is in its own div tag set as the background image of the div tag if that makes any diference. Thanks Hi, I am in the procress of creating discussion system however I am a bit puzzled about the best way to go about it. I am starting the discussion by creating an ID number and then match the answer to the initial ID number. However, I dont know whether if is best to put the responses into a different database. I'm a bit puzzled how ID matching systems works. Lets say: Question 1 = ID1 Question 2 = ID2 Question 3 = ID3 Question 1 Answer 1 = ID4 (How is this matched to ID1) Question 2 Answer 1 = ID5 (How is this matched to ID2) is this based on preg_match? I have just read my upcoming modules for my final year at uni and 'multimedia databases' is one of them. I am just wondering if any of you had any clue on what a multimedia database is? I am guessing it's a database populated with directory data, but that would be to simple... I'm trying to connect to two databases and I'm having problems with the following code. I googled to come up with this but can't figure out the errors I'm getting. Code: [Select] $connection="localhost"; $username="user"; $password="password"; $database1="dbone"; $database2="dbtwo"; $db1 = mysql_connect($connection,$username,$password) or die(mysql_error()); $sel1 = mysql_select_db($database1, $db1); $query1 = "SELECT * FROM TBLUSERS"; $result1 = mysql_query($query1, $db1); while($nt1 = mysql_fetch_array($result1, $db1)) { } $db2 = mysql_connect($connection,$username,$password) or die(mysql_error()); $sel2 = mysql_select_db($database2, $db2); $query2 = "SELECT * FROM TBLPD20101101"; $result2 = mysql_query($query2, $db2) or die(mysql_error()); while($nt2 = mysql_fetch_array($result2, $db2)) { } The error I get is Quote Warning: mysql_fetch_array() expects parameter 2 to be long, resource given in C:\xampp\htdocs\HighVisibility\DashBoard2.php on line 13 Warning: mysql_fetch_array() expects parameter 2 to be long, resource given in C:\xampp\htdocs\HighVisibility\DashBoard2.php on line 22 What would be the fastest way to search 2+ databases with the same search information? Each database is different, and may return different information. hi all, I am trying to link 2 mysql tables and display some information from each of them. I have a list of all the possible items for sale in table1 and I am trying to count the number of rows in the other table2 where the items exist. E.g. 'table1' manufacturer model man1 item1 man1 item2 man1 item3 'table2' id model 1 item3 2 item3 3 item2 And the result would show: item1(0) item2(1) item3(2) It would list all the items from table1 and show next to it how many rows are related to that item from table2. I have inserted a quote where I have tried many times to enter something similar to that show in the note below - but I can not get it to work - it just shows the total number of models in table1 for a given manufacturer. The php I have made so far is: <?php case 'manufacturer': $query = " SELECT * FROM table1"; $query .= " WHERE manufacturer = '".$data."' "; $query .= " ORDER BY model "; $result = mysqli_query($cxn,$query); $returnData[''] = "Select a Model..."; while($row = mysqli_fetch_assoc($result)){ // I THINK I NEED TO INSERT SOMETHING LIKE $query2 = "SELECT * FROM table2 WHERE model = table1.model"; $k=$row['model']; $k2=$row2['model']; $counter[$k]+=1; $returnData[$k]=$k; } foreach($counter as $k => $row) { $returnData[$k] .= " ($row)"; } break; ?> Dear All, I am having 2 different DB on 2 different hosts. I am running MySQL Server on my local PC where the user is entering data in the tables. I have a website which has the identical DB on the web. I am able to connect to the database by using the codes on the server. I want to update the server DB with the local system DB by running one update command. I get the error "-SELECT command denied to user 'localusername'@'localhost' for table 'pst_data'" Given below is the code used for the process : //connecting the remote system DB $link = mysql_connect('IPAddress:3306', 'remoteusername', 'Password'); if (!$link) { die('Not connected : ' . mysql_error()); } $db_selected = mysql_select_db('remotedb', $link); if (!$db_selected) { die ('Can\'t use Remote System DB: ' . mysql_error()); } //connecting the local database on the webstite $weblink = mysql_connect("localhost","localusername","password"); if (!$weblink) { die('Not connected : ' . mysql_error()); } $webdb_selected = mysql_select_db('localwebdb', $weblink); if (!$webdb_selected) { die ('Can\'t use WebServer Database : ' . mysql_error()); } //query to fetch the records from remote ystem and insert into local website database $upd_Query=mysql_query("INSERT INTO localwebdb.`table` SELECT * FROM remotedb.`table` where field=' some condition' "); --------------------------------------------------- I have tested that I have connected the remote DB by running queries on the webserver. Could anyone bail me out so that i can copy the DB from remote to local Any help would be appreciated I'm trying to select multiple peoples ID's where different values in different databases tables match. For instance, I want to make sure the person is a different gender than what the person logging in is, which would be set as 1(male) 2(female) in a Genders Column in the Membership_Profile table, while I am also checking to make sure that they are not blocked inside the Membership table and making sure they have a profile image in a user_profilepic table. How would I do this? This all needs to sort by the Date they were added inside the Membership Table and Limit it to 5 results. When I use the below code tho, it pulls up both Male and Females, but seems to look at the Membership correctly. Code: [Select] $sql2 = "SELECT Gender FROM Membership_Profile where UserID = '$user_ID'"; $sql_result2 = mysql_query($sql2); $login_row2 = mysql_fetch_assoc($sql_result2); $user_gender = $login_row2['Gender']; $true_query = mysql_query("SELECT Membership.ID, Membership_Profile.ID, user_profilepic.UserID FROM Membership, Membership_Profile, user_profilepic WHERE Membership_Profile.Gender != '$user_gender' AND Membership.ID = user_profilepic.UserID AND Membership.IsApproved='1' AND Membership.IsLockedOut = '0' AND Membership.UserLevel = '1' AND user_profilepic.Profile_Pic = '1' ORDER BY Membership.CreateDate DESC LIMIT 5;"); $is_odd_row = 1; while ($true_row = mysql_fetch_assoc($true_query)) { $right_user = $true_row['UserID']; $right_user2 = $true_row['ID']; $sql = "SELECT * FROM Users where ID = '$right_user' AND ID = '$right_user2'"; $sql_result = mysql_query($sql); $login_row = mysql_fetch_assoc($sql_result); $right_user_ID = $login_row['ID']; $rightGUID = $login_row['UserId']; $right_name = $login_row['UserName']; $sql = "SELECT DOB, City, State FROM Membership_Profile where ID = '$right_user' AND ID = '$right_user2'"; $sql_result = mysql_query($sql); $login_row = mysql_fetch_assoc($sql_result); $user_dob = $login_row['DOB']; $City = $login_row['City']; $State = $login_row['State']; if ($is_odd_row) { echo "<tr style=\"background-color: #e1ebf8\">\n"; $is_odd_row = 0; } else { echo "<tr style=\"background-color: #f3f7fc\">\n"; $is_odd_row = 1; } echo " <td align=\"left\"> <img width=\"85px\" style=\"margin-right:10px\" src=\"profilepics/$rightGUID/profile/$right_user_ID.jpg\"><br /> <span style=\"font-size:13px; font-weight:900; color:#ffae00;\">$right_name </span><br /> Location:<br /><b>$City, $State</b><br /> Age:<b>";echo CalculateAge("$user_dob"); echo "</b></td></tr>"; } Hello again guys! What if i have 2 databases, db1 and db2. i want to read raw data from db1 and match it's content with data from db2, then produce some information. How can i do it at the same time? I know that to be able to access mysql data, we have to use the mysql_connect() and mysql_select_db(). mysql_select_db() only allows to connect to single database. what should i do? thanks for your help in advance. I'm trying to select multiple peoples ID's where different values in different databases tables match. For instance, I want to make sure the person is a different gender than what the person logging in is, which would be set as 1(male) 2(female) in a Genders Column in the Membership_Profile table, while I am also checking to make sure that they are not blocked inside the Membership table and making sure they have a profile image in a user_profilepic table. How would I do this? This all needs to sort by the Date they were added inside the Membership Table and Limit it to 5 results. When I use the below code tho, it pulls up both Male and Females, but seems to look at the Membership correctly. Code: [Select] $sql2 = "SELECT Gender FROM Membership_Profile where UserID = '$user_ID'"; $sql_result2 = mysql_query($sql2); $login_row2 = mysql_fetch_assoc($sql_result2); $user_gender = $login_row2['Gender']; $true_query = mysql_query("SELECT Membership.ID, Membership_Profile.ID, user_profilepic.UserID FROM Membership, Membership_Profile, user_profilepic WHERE Membership_Profile.Gender != '$user_gender' AND Membership.ID = user_profilepic.UserID AND Membership.IsApproved='1' AND Membership.IsLockedOut = '0' AND Membership.UserLevel = '1' AND user_profilepic.Profile_Pic = '1' ORDER BY Membership.CreateDate DESC LIMIT 5;"); $is_odd_row = 1; while ($true_row = mysql_fetch_assoc($true_query)) { $right_user = $true_row['UserID']; $right_user2 = $true_row['ID']; $sql = "SELECT * FROM Users where ID = '$right_user' AND ID = '$right_user2'"; $sql_result = mysql_query($sql); $login_row = mysql_fetch_assoc($sql_result); $right_user_ID = $login_row['ID']; $rightGUID = $login_row['UserId']; $right_name = $login_row['UserName']; $sql = "SELECT DOB, City, State FROM Membership_Profile where ID = '$right_user' AND ID = '$right_user2'"; $sql_result = mysql_query($sql); $login_row = mysql_fetch_assoc($sql_result); $user_dob = $login_row['DOB']; $City = $login_row['City']; $State = $login_row['State']; if ($is_odd_row) { echo "<tr style=\"background-color: #e1ebf8\">\n"; $is_odd_row = 0; } else { echo "<tr style=\"background-color: #f3f7fc\">\n"; $is_odd_row = 1; } echo " <td align=\"left\"> <img width=\"85px\" style=\"margin-right:10px\" src=\"profilepics/$rightGUID/profile/$right_user_ID.jpg\"><br /> <span style=\"font-size:13px; font-weight:900; color:#ffae00;\">$right_name </span><br /> Location:<br /><b>$City, $State</b><br /> Age:<b>";echo CalculateAge("$user_dob"); echo "</b></td></tr>"; } right i did this code <table width="400" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" class=thinline> <tr class="header" background="includes/grad.jpg"> <td height="20" colspan="3" background="includes/grad.jpg" class=header><div align="center" class="header">Last 10 Kills</div></td> </tr> <tr class="header" background="includes/grad.jpg"> <td width="166" height="20" background="includes/gradgrey.jpg">Name</td> <td width="157" height="20" background="includes/gradgrey.jpg">Rank</td> <td width="157" height="20" background="includes/gradgrey.jpg">Killed Time</td> </tr> <? $c=mysql_query("SELECT * FROM attempts WHERE outcome='Dead' ORDER BY id DESC LIMIT 10"); while($d=mysql_fetch_object($c)){ echo "<tr><td><a href='profile.php?viewuser=$d->target'>$d->target</a></td><td>$d->rank</td><td>$d->date</td></tr>"; } ?> but you see the $d->rank is in another database called users How can i link the databases so the last 10 kills shows the users rank Below is some code I have that I would like to connect to databases that reside on different physical machines. The problem I can't seem to solve is how to ignore machines that are unreachable. Code: [Select] <html> <head> </head> <body> <?php $mysql_conn=new mysqli('192.168.1.67','admin','password','monkey'); $result=$mysql_conn->query("SELECT * from testtable"); echo "<table>"; echo "<th>Test Query</th>"; while ($row=$result->fetch_array(MYSQL_ASSOC)){ echo "<tr><td>"; echo $row['test1']; echo "</td></tr>"; } echo "</table>"; $mysql_conn=new mysqli('192.168.1.126','admin','','monkey1'); $result=$mysql_conn->query("SELECT * from demo"); echo "<table>"; echo "<th>Test Processor</th>"; while ($row=$result->fetch_array(MYSQL_ASSOC)){ echo "<tr><td>"; echo $row['name']; echo "</td></tr>"; } echo "</table>"; $mysql_conn=new mysqli('192.168.1.127','admin','','monkey2'); $result=$mysql_conn->query("SELECT * from demo"); echo "<table>"; echo "<th>Test Processor</th>"; while ($row=$result->fetch_array(MYSQL_ASSOC)){ echo "<tr><td>"; echo $row['processor']; echo "</td></tr>"; } echo "</table>"; ?> </body> </html> If 192.168.1.126 is offline, the webpage doesn't go any further and display 192.168.1.127's info...Appreciate any insight... I have done select statements where you loop through all the records. But what I need to do is pull record from one database than insert parts of that record into the second. The issue I have is no knowing how to select a different db with out stopping the loop of the first. Thanks in advance. |