I have two databases same server same domain and same username and password. I need away to select from the main DB and then the second DB I need to be able to connect to it and insert data.
this is currently what I am trying but it is not working.
public function publish(){
$result = mysql_query("SELECT * FROM customer_detail WHERE approvedforsite = 2");
while($row = mysql_fetch_array($result))
{
echo $row['customer_id'] . " - " .$row['TradingName'] . " - " . $row['Phone'] . " - " . $row['Street'] . " - " . $row['City'] . " - " . $row['State'] . " - " . $row['PostCode'] . " - " .$row['Description'];
echo "<br />";
mysql_query("INSERT INTO realcas_incard_server.approved_business (customer_id, tradingname)
VALUES ('". $row['customer_id'] ."','".$row['TradingName']."')");
mysql_query("INSERT INTO realcas_incard_server.business_stores (customer_id, storeid, phone, street, suburb, state, postcode, description)
VALUES ('". $row['customer_id'] ."', 1, '".$row['Phone']."', '" .$row['Street'] . "', '" . $row['City'] . "', '" . $row['State'] . "', '" . $row['PostCode'] . "','".$row['Description']."')");
$offerresult = mysql_query("SELECT * FROM customer_realcash_offer WHERE businessid = ".$row['customer_id']);
while($offers = mysql_fetch_array($offerresult))
{
mysql_query("INSERT INTO _incard_server.Real_Cash_Offers (business_id,storeid,offer) VALUES (".$row['customer_id'].",1,'".$offers['offer']."')");
echo $offers['offer']. "<br/>";
}
echo "<br />";
echo "<br />";
}
}
You can either use mysql_select_db() to switch between databases on the same server/username/password/connection, or you can use the database_name.table_name.column_name dotted syntax (made easier with aliases, as in select a.column from database_name.table_name as a).
Those are your two options. (The third option is to just use one database, but I'm assuming that's not an option)
say there is a table test in database first
test
id int
and table junk in database second
junk
id int
then
query would be
insert into junk(id) (select id from first.test);
Related
I am looking to update one of the table. After I update, all the duplicate data is getting inserted again. Especially, the cloneSQL part of the code. I tried using DISTINCT, NOT EXISTS but no luck.
if(DB_num_rows($checkResult) > 0){
$cloneSQL = "UPDATE DISTINCT pricematrixdiscount SET
discount='" . $vals[3] . "'
WHERE debtorno='" . $_POST['cloneTo'] . "',
product_line='" . $vals[1] . "',
salestype='" . $vals[2] . "' ";
}
else {
$cloneSQL = "INSERT into pricematrixdiscount
(debtorno,
product_line,
salestype,
discount) VALUES
('" . $_POST['cloneTo'] . "',
'" . $vals[1] . "',
'" . $vals[2] . "',
'" . $vals[3] . "')";
How can I insert only distinct values on the pricematricdiscount table without the duplicates being inserted?
I already saw other post related to this question, but they did not solve my problem. I want to automatically go to next rows and want to insert that row in the table. I am transfering sample of data from another database. I want to insert large set of rows... Right now I am getting first row repeatedly in all rows. I need to go to to next rows automoatically
Below is my sample code...
while ($row = mysql_fetch_array($query)) {
$sql = "INSERT INTO table_name (info1, info2, info3,...) "
."VALUES (" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . ")";
}
This sql Query could help you. You can insert the select datas in one Query. See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/insert-select.html
INSERT INTO table_name
(info1, info2, info3)
SELECT
source_table.info1,
source_table.info2,
source_table.info3,
FROM
source_table
WHERE
(WHERE CLAUSE)
do a separate insert for each selected rows:
while ($row = mysql_fetch_array($query)) {
$sql = "INSERT INTO table_name (incremented_field, info1, info2, info3) "
."VALUES (NULL, " . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . ")";
mysql_query($sql);
}
I have part of the code below:
while($array = $result->fetch_assoc() ){
$second_query = "INSERT INTO".TBL_USERSDONTPAY."VALUES ($array[\"username\"], $array[\"password\"], '0',$array[\"userid|\"], )";
$second_result = $database->query($second_query);
}
The query doesn't seem to work. Any clues? I think it's a problem with the quotes or something. How can actually pass array elements?
here is my whole code i want to move one row to another table
$q = "SELECT * FROM ".TBL_USERS." WHERE username = '$subuser'";
$result = $database->query($q);
if($result && $result->num_rows == 1){
while($array = $result->fetch_assoc() ){
$second_query = "INSERT INTO" . TBL_USERSDONTPAY . "VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] ."')";
$second_result = $database->query($second_query);
if($second_result){
// it worked!
$q = "DELETE FROM ".TBL_USERS." WHERE username = '$subuser'";
$database->query($q);
}
}
}
You need to clean that query up and remove the final comma.
$second_query = "INSERT INTO " . TBL_USERSDONTPAY . " VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] . "')";
I see several issues with your query code
escaping of the array indexes in your string:
you can either end the string and concatenate the parts together:
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] . "')";
or use the {$var} syntax:
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" VALUES ('{$array['username']}', '{$array['password']}', '0', '{$array['userid']}')";
missing spaces (see example code above .. you were missing the spaces before and after the table name)
missing field names. your query may work without if you specify all fields in the right order, but will fail misteriously when you alter the table later (e.g. add a field to the table)
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" (username, password, foo, user_id)".
" VALUES ('{$array['username']}', '{$array['password']}', '0', '{$array['userid']}')";
please note you should actually insert the correct field names in the second line of my example above. You can find more information on this in the MySQL docs for INSERT
Despite reading quite many posts i cannot solve this error-
Unknown column 'alt.atheism1111' in 'field list'
the fields filename,category may have . in the middle of numbers or words,
im using phpmyadmin for database
function insert_rec($cat,$file,$wordid,$synsetid,$seqno)
{
$cat=mysql_real_escape_string($cat);
$file=mysql_real_escape_string($file);
$wordid=mysql_real_escape_string($wordid);
$synsetid=mysql_real_escape_string($synsetid);
$seqno=mysql_real_escape_string($seqno);
echo $cat." ". $file ." ". $wordid." " . $synsetid." " . $seqno;
$sql="INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`) VALUES (`" . $cat . "`,`" . $file. "`,`" . $wordid. " `,`" . $synsetid . "`,`" .$seqno . "`)";
$result=mysql_query($sql);
if(!$result)
{
die(mysql_error());
}
}
$sql="INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`) VALUES (`" . $cat . "`,`" . $file. "`,`" . $wordid. " `,`" . $synsetid . "`,`" .$seqno . "`)";
You need to remove "`" from the above query in the values only and replace it with " ' " (single quote)
Use backticks for field names and single quotes for the values.
$sql = "INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`)
VALUES ('$cat', '$file', '$wordid', '$synsetid', '$seqno')";
It should be wrapped with single quotes not with back tick.
$sql = "INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`) VALUES ('" . $cat . "','" . $file. "','" . $wordid. "','" . $synsetid . "','" .$seqno . "')";
BackTick escapes MYSQL Reserved WORDS.
if u can post ur db schema than it will be easy to check, as of now it look like u have a field as alt.atheism1111 which can be the show stopper
or use this:
$sql = "INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`)
VALUES ('$cat', '$file', '$wordid', '$synsetid', '$seqno')";
Total newbie trying to learn... thanks for all the help so far!
UPDATE - updated code below - Thanks Phill
I'm now trying to get the relevant horse information from a table horses which I can join to race_form with the horse_name field. I want to then display the information for each horse in the race below the while($racecard) info. Does this make it a bit clearer?
I thought I could just do another query and then a while loop with mysql_fetch_array below the one I have already and that would display it and then move onto the next race, but that apparently doesn't work...?!
I'm wondering if you can run 2 while loops after each other inside a while loop? I am working on a horse racing formguide - I can display each race list, but underneath I want to display individual horse details on each horse in the race. Anyway if you look at this page you can see what I'm trying to do:
http://tasmanianracing.com/superform/formguide.php?meetingID=21042011LAUN&Submit=View+Form
Problem is, any time I put a second while loop after the race list, it won't show up. I'm trying to run a query to get the horse details from my horse table and then run a while for each horse in the race, but nothing shows up.
I hope this is clear enough ... my snippet of code is below - please let me know if I've missed out something important:
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $row['race_number'] . " at " . $row['start_time'] . " " . $row['class'] . " over " . $row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $row['raceID'] . "')");
while($row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $row['number'] . "</td><td>" . $row['past10'] . "</td><td>" . $row['horse_name'] . "</td></tr>";
}
echo "</table><br>";
echo "<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
echo "</table>";
You'll probably need to change the names of some of the $row variables, they may interfere with each other.
For example:
while($row_races = mysql_fetch_array($totalraces)){
while($row_details = mysql_fetch_array($racedetails)){
while($row_card = mysql_fetch_array($racecard)){
I think you can get rid of one of your queries:
The first query gets the number of races by selecting a COUNT, This returns one record with the count value.
// This returns one record with the count
$total_races = "SELECT COUNT(race_number) AS totalraces
FROM race_info WHERE meetingID = ('".$safemeetingID."') ";
Next you iterate over the the same records as the rows returned for the race details are the same as the count.
// This looks to return the record(s) with the race details
$race_details = "SELECT * FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')";
I think you can just use this to get the desired results: (I agree to rename the $row variable(s) to something descriptive for each while loop)
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($details_row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");
while($rc_row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
}
echo "</table><br>";
echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
NOT TESTED/PSEUDO CODE
"SELECT *
FROM horses AS h,
INNER JOIN race_info AS ri ON race_form.raceID = race_info.raceID
WHERE horse_name IN (
SELECT horse_name
FROM race_form AS srf
WHERE h.horse_name = srf.horse_name
)
AND race_form.raceID = ('" . $details_row['raceID'] . "')"
The idea is to join the two queries into one, I know this is not the correct syntax but it might give you an idea on how to go about it.
Or you can do another query while loop for the horse names
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($details_row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");
while($rc_row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
$horses = mysql_query("SELECT *
FROM horses
WHERE horse_name = ('" . $rc_row['horse_name'] . "')");
while($horse_row = mysql_fetch_array($horses))
{
// echo horse details here
}
}
echo "</table><br>";
echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
I theory you could and in practice you can, but a while in a while in a for in a while seems a little bit over the top...
I'm sure we can help you make it more efficient if you explain what it is you're trying to do.