PHP - Php Connect To Two Databases
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 Similar TutorialsJust wondering, which is better php databases or sql? I have phpMyAdmin and I've that you can convert the database into php scripts? 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? 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 What would be the fastest way to search 2+ databases with the same search information? Each database is different, and may return different information. 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... 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... 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>"; } 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. What's the best way for putting actual quotations into a database? I was using... $quote = htmlspecialchars(mysqli_real_escape_string($dbc, $_POST['quote'])); Should I be? 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 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... OK, so I have a question about databases and how they sort things being loaded into them. I have a database that I have images uploaded to (some people dont like to do it this way but this is what is going to work for my project). I have it set up with an id column that auto-increments. When I upload the files for some reason it is putting the highest id number first and the second one is the lowest then it goes in order from there. Why would this be happening and how do I correct it? 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 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>"; } Hi guys, nice being a part of the forum, my question is, i have a network of differente websites, each of these websites work in the same services. All there backend, databases are the same, having there same tables, etc. Only thing that changes in them is the CSS for differente design of course. What i need it to do is when one of the websites insert a new product the other sites receives the same product of this website. All these site have the same capability, when site A insert a new product site B and C receives the same product in there databases, when site C insert new product site A and B receives the same product and forward. How can i achieve this accomplishment, maybe i should put this question in the mysql section but maybe here can help me. Be well Hello, I'm using this code in a php file to connect to 2 databases (something that I need to do): $conn_local = mysql_connect('localhost','root','',TRUE); $conn_local2 = mysql_connect('localhost','root',''); mysql_select_db('db1',$conn_local); mysql_select_db('db2',$conn_local2); I'm then trying to use this code to call it from the 1st Database: $sql = "SELECT * FROM news_items ORDER BY news_date DESC LIMIT 0,$limit"; $result = mysql_query($sql,$conn_local); if(mysql_num_rows($result)!=0){ while($row = mysql_fetch_array($result)) ...etc Although I am getting this error: Quote Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\includes\function.php on line 17 If I ONLY include the 1 database and don't bother putting the ',$conn_local' into the mysql_query it will work fine and return the records needed. It only seems to be when I try and include more than 1 database. Any ideas where I'm going wrong? Thanks. Basically, I have a database table called 'users' and I would like to populate a drop down box with these values of 'users'. How?? - to call upon the values is this: 'upduser2' Right now, all I am using is a text box, in where you have to type in the users name manually (this is so an admin can change variables and settings according to that current user). This is what I am using so far: <h3>Update User Level</h3> <? echo $form->error("upduser"); ?> <table> <form action="adminprocess.php" method="post"> <tr> <td> Username:<br /> <input type="text" name="upduser" maxlength="30" value="<? echo $form->value("upduser"); ?>" /></td> <td> Level:<br /> <select name="updlevel"> <option value="1">1 </option> //example settings <option value="9">9 </option> </select></td> <td><br /> <input type="hidden" name="subupdlevel" value="1" /> <input type="submit" value="Update Level" /></td> </tr> </form> </table></td> </tr> Much help would be appreciated. |