I am working on a script that updates from:
tendesig_zink_production | euid0_hikashop_product | product_quantity
to:
tendesig_zink_dev | euid0_hikashop_product | product_quantity
I have to do this because our inventory numbers are stored on the production instance. so before pushing a new version from our dev instance i have to update the dev's inventories from production prior to the push. I am a lot rusty with my mySQL, this is what i have so far I need to know what the proper query would be though.
<?php
$host1="localhost"; // destination
$base1="tendesig_zink_dev";
$user1="tendesig_zink";
$password1="1,&#GZAWiB5z";
$host2="localhost"; //source
$base2="tendesig_zink_production";
$user2="tendesig_zink";
$password2="1,&#GZAWiB5z";
$conection1 = #mysql_connect($host1, $user1, $password1)
or die("Error reaching destination<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 1! <br>";
$Db1 = #mysql_select_db($base1, $conection1)
or die("Error reaching destination database:<br>" . mysql_error(). "<br>" . mysql_errno());
print "Database1 reached!<br>";
$conection2 = #mysql_connect($host2, $user2, $password2)
or die("Error reaching source<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 2!<br>";
$Db2 = #mysql_select_db($base2, $conection2)
or die("Error reaching source database:<br>" . mysql_error(). "<br>" . mysql_errno());
print "Database2 reached!!<br>";
$query = 'create table destination_table select * from second_database.source_table';
//echo "<br>".$query."<br>";
$result2 = mysql_query($query2, $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());
mysql_close($conection1);
mysql_close($conection2);
?>
Your query is wrong. You want to do something like this:
CREATE TABLE destination_database.destination_table LIKE source_database.source_table
Then run this:
INSERT INTO destination_database.destination_table
SELECT * FROM source_database.source_table
In (untested) PHP code:
$create_query = 'CREATE TABLE destination_database.destination_table LIKE source_database.source_table';
$result = mysql_query($create_query, $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());
$insert_query = 'INSERT INTO destination_database.destination_table SELECT * FROM source_database.source_table';
$result = mysql_query($insert_query , $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());
Related
Background: I work with phpMyAdmin (MySQL Workbench) in a mysql DB. I write some PHP code to import data in the DB and execute this with the task scheduler of windows. <= this works fine!
My Topic: Now I want to export some data into a file in a Windows folder. At first I write the SQL code in phpMyAdmin to see some debug-infos.
Independent of php my sql-query works fine.
If I put the code in the php-programm my export didn't work.
I think the problem occurs because of my path specification.
The other programmparts, specially the Update-Part, do what they should.
Here is my code:
<?php
include "../config.php";
$conn = new mysqli('192.168.10.120', 'alb5', 'alb5','testdatenbank');
if ( $conn->connect_error ) {
die( "Connection failed: " . $conn->connect_error );
} //$conn->connect_error
$sql = "set #sql = concat(\"SELECT `LS_ID_Nr`, `Stk_pro_Krt_DL` * `Krt_DL` + `RB_Stk_pro_Krt_DL` * `RB_Krt_DL`, `Umstellzeit`, `Produktionszeit`, `Teilmeldung`, `Fertigmeldung`
INTO OUTFILE 'C:/Temp/Export/Test - \", DATE_FORMAT(NOW(), '%Y%m%d%H%i%s'),\" - Test.txt'
fields terminated by ';'
lines terminated by '\r\n'
From praemie where Proof_P = 0\");";
$sql = "prepare s1 from #sql;";
$sql = "execute s1;";
$sql = "DROP PREPARE s1;";
$sql = "UPDATE praemie SET Proof_P = 1 WHERE Proof_P = 0;";
$result = $conn->query( $sql );
echo("Error description: " . mysqli_error($conn));
?>
Does anybody have an idea how I specify a export-path, with sql in php?
Thanks in advance.
This is creepy way of doing this.
I suggest to fetch the data into the php and store it via file_put_contents.
Quick example:
<?php
include "../config.php";
$conn = new mysqli('192.168.10.120', 'alb5', 'alb5','testdatenbank');
if ( $conn->connect_error ) {
die( "Connection failed: " . $conn->connect_error );
} //$conn->connect_error
$query = 'SELECT `LS_ID_Nr` AS `LS_ID_Nr`, `Stk_pro_Krt_DL` * `Krt_DL` + `RB_Stk_pro_Krt_DL` * `RB_Krt_DL` AS ``, `Umstellzeit`, `Produktionszeit`, `Teilmeldung`, `Fertigmeldung`FROM praemie WHERE Proof_P = 0';
$result = $conn->query($sql);
file_put_contents('C:/Temp/Export/Test/test.txt', json_encode($result->fetch_all()));
yeeeah it works now!
Only for other people out there who want do the same.
Two things I have to change. I execute my programm in an other path so I have to write
$include_path = 'C:/xampp/php/pear/PEAR/config.php'; this fixed the first error.
The second point: Your quick example $result = $conn->query($sql); have to be $result = $conn->query($query);
Down below is the whole code:
<?php
$include_path = 'C:/xampp/php/pear/PEAR/config.php';
$conn = new mysqli('192.168.10.120', 'alb5', 'alb5','testdatenbank');
if ( $conn->connect_error ) {
die( "Connection failed: " . $conn->connect_error );
} //$conn->connect_error
$query = 'SELECT `LS_ID_Nr`, `Stk_pro_Krt_DL` * `Krt_DL` + `RB_Stk_pro_Krt_DL` * `RB_Krt_DL` AS `Ges. Stück`, `Umstellzeit`, `Produktionszeit`, `Teilmeldung`, `Fertigmeldung` FROM praemie WHERE Proof_P = 0';
$result = $conn->query($query);
if (mysqli_num_rows($result)!==0)
{
file_put_contents('C:/Temp/Export/' . date('Y-m-d H-i-s') . '.txt', json_encode($result->fetch_all()));
}
$sql = "UPDATE praemie SET Proof_P = 1 WHERE Proof_P = 0";
$result = $conn->query( $sql );
echo("Error description: " . mysqli_error($conn));
?>
*Edit: I integrate a proof if the result (num_rows of query) is not equal to zero and give the file a unique name with the variable date('Y-m-d H-i-s') (attention with windows filename permissions H - i - s not H : i : s)
Thanks alot Damian for all your knowledge and the help of the forum!
I'm happy now.
(~: First attempts with php and it works now fine! :~)
I want to map my PHPExcel based on the picture below.
When I started to map my data that I got from the database I got only the customers information. However, I still want to get the Items that the customer ordered and map it in my PHPExcel.
Here's what I've done so far.
Here's my code:
$sqlmanagement = "select `check`, concat(hour, ':', minute) as `time`,
customer, unit, amount, employee, manager from gndtndr where type = 3 and
typeid = 1 and STR_TO_DATE(DATE, '%m/%d/%Y') BETWEEN '$date1' AND '$date2'
order by STR_TO_DATE(DATE, '%m/%d/%Y')";
$connect = #mysql_connect($dbhost, $dbuser, $dbpass)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" .
mysql_errno());
//select database
$Db = #mysql_select_db($dbname, $connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" .
mysql_errno());
//execute query
$resultmanagement = #mysql_query($sqlmanagement,$connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" .
mysql_errno());
$rowManagementDiscresult = 2;
while($rowmanagement = mysql_fetch_array($resultmanagement)){
$rowmanagement1 = $rowManagementDiscresult + 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowManagementDiscresult,
$rowmanagement['check']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowManagementDiscresult,
$rowmanagement['time']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowManagementDiscresult,
$rowmanagement['customer']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowManagementDiscresult,
$rowmanagement['unit']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowManagementDiscresult,
'1');
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowManagementDiscresult,
$rowmanagement['amount']);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowmanagement1, 'ITEMS
HERE');
$rowManagementDiscresult++;
}
Hoping someone can help me, this is taking forever and I need to speed it up:
SELECT Col2, Col3 FROM Table1 Where Col1 = Whats in Table1 Col1
UPDATE Table2 SET Col2, Col3 Where Col1 = Whats in Table2 Col1
the table I am updating and updating from have no indexes and are around 2.5 millions rows each.
The query is processing around 60 entries per second and will take way too long to finish. The code is below:
<?php
//establish connection to my Database
$link = mysql_connect('localhost', 'info', 'connect') or die('Could not connect: ' . mysql_error());
mysql_select_db('test') or die('Could not select database');
set_time_limit(18000);
//maintain logged in status
mysql_query("INSERT INTO `logtest` (`loggedin`, `timesince`) VALUES ('1', NOW());");
$shere=0;
$sql = "SELECT UniqueID FROM temp WHERE LongDec IS NULL ORDER BY UniqueID DESC";
$fcr = mysql_query($sql) or die(mysql_error());
while($frr=mysql_fetch_array($fcr)){
$shere=$frr[0];
}
mysql_free_result($fcr);
$sql = "SELECT DISTINCT UniqueID, LocationNum FROM temp WHERE UniqueID >=" . $shere . ";";
$fcr = mysql_query($sql);
while($fcrow = mysql_fetch_array($fcr)){
$sql = "SELECT LatDec, LongDec FROM lotest WHERE UniqueID=" . $fcrow[0] . " AND LocationNum=" . $fcrow[1] . ";";
$frr = mysql_query($sql);
$fr = mysql_fetch_array($frr);
$sql = "UPDATE temp SET LatDec=" . $fr[0] . ", LongDec=" . $fr[1] . " WHERE UniqueID = " . $fcrow[0] . " AND LocationNum=" . $fcrow[1] . ";";
mysql_query($sql);
//echo $fcrow[0] . ": " . $fr[0] . "<br>\n";
mysql_free_result($frr);
}
mysql_free_result($fcr);
//Move data to pool
mysql_query("TRUNCATE pool;");
mysql_query("INSERT INTO pool SELECT * FROM temp;");
mysql_close($link);
?>
I think the error should be number in insert fields doesn't match the number in value, but I really cannot see what's wrong in my code. The error is "Here is the error: Column count doesn't match value count at row 1" which shows that it is something wrong in my insert:
<?
$con2=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result2= mysqli_query($con2,"SELECT projects.*, org.orgname FROM projects left outer join org on projects.orgid = org.orgid where projects.projectid = '".$projectid."'");
$row2 = mysqli_fetch_array($result2);
$con3 = mysql_connect("localhost","root","");
if (!$con3)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con3);
echo $row2[target];
$sql3 = "INSERT INTO projectlog
(projectid,projectname,generaluserid,adminuserid,swdfphase,target,objective,type,projectsize,commonth,comyear,duration,detail,projectstatus,projectsatisfaction,overallcomments,projectbenefits,orgid,createtime,lastupdatetime,logcreatetime,userid)
VALUES
('".$projectid."',
'".$row2[projectname]."',
'".$row2[generaluserid]."',
'".$row2[adminuserid]."',
'".$row2[swdfphase]."',
'".$row2[target]."',
'".$row2[objective]."',
'".$row2[type]."',
'".$row2[projectsize]."',
'".$row2[commonth]."',
'".$row2[comyear]."',
'".$row2[duration]."',
'".$row2[detail]."',
'".$row2[projectstatus]."',
'".$row2[projectsatisfaction]."',
'".$row2[overallcomments]."',
'".$row2[projectbenefits]."'
'".$row2[orgid]."',
'".$row2[createtime]."',
'".$row2[lastupdatetime]."',
now(),
'".$_SESSION[myid]."')";
if (!mysql_query($sql3,$con3))
{
die('Here is the error: ' . mysql_error());
}
mysql_close($con3);
?>
It looks like you are missing a comma at the end of the line
'".$row2[projectbenefits]."'
This is definitely a beginner's question. There are two issues. The id in my MYSQL table (set to autoincrement) keeps going up, even though I delete rows from my table (I'm using phpmyadmin). As for the other issue, I can't seem to find a way to work with the row most recently entered by the user. The code echos all existing rows from MYSQL.
I've bolded the most pertinent section of code.
<?php
//establish connection to mysql
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
/*retrieve user input from input form
and initialize variables */
$Word1 = $_POST["Word1"];
$Word2 = $_POST["Word2"];
$Word3 = $_POST["Word3"];
$Word4 = $_POST["Word4"];
$Word5 = $_POST["Word5"];
//select db
mysql_select_db("madlibs", $con);
//insert user input for word 1
$sql = "INSERT INTO test (Word1, Word2, Word3, Word4, Word5)
VALUES
('$Word1', '$Word2', '$Word3', '$Word4', '$Word5')";
if(!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$result = mysql_query ("SELECT * FROM test");
/* take note here */
while($row = mysql_fetch_array($result))
{
echo $row['Word1'] . " " . $row['Word2'] . " " . $row['Word3'] . " " .
$row['Word4'] . " " . $row['Word5'] . " " . $row['id'];
echo "<br />";
} /* take note here */
mysql_close($con);
?>
$result = mysql_query ("SELECT * FROM test order by id desc limit 1");
As for your id question...that's how ids work. They don't fill in gaps.
On a side note: Never ever put user submitted data directly into the query string. Always escape them with mysql_real_escape_string().
SELECT * FROM test ORDER BY Id DESC LIMIT 1