I am attempting to match rows in a mysql table using the values from table1.column1 and table2.column3 and then copy the value from table2.column2 into table1.column1 for each match. The query below does what I need to do, but only when I execute it manually (through phpmyadmin). When I try to execute it from PHP I receive the error Unknown column table1.column1 in 'field list'. Here is my PHP code:
<?php
mysql_connect($host,$user,$pass);
$db_selected = mysql_select_db($data);
$sql = "UPDATE table1 t1, table2 t2
SET t1.column1 = t2.column2
WHERE t1.column1 = t2.column3";
$result = mysql_query($sql);
if (!$result) {
echo mysql_error();
} ?>
I know that the mysql connection info works because I am able to execute other queries. From my research on the error it seems that I might need backticks around some part of the query but after several tries I can't figure out the correct way.
EDIT 1 - As requested here is the real query:
UPDATE wp_mf_custom_groups,wp_mf_posttypes
SET wp_mf_custom_groups.post_type=wp_mf_posttypes.type
WHERE wp_mf_custom_groups.post_type=wp_mf_posttypes.id
Outputs the error
Unknown column 'wp_mf_custom_groups.post_type' in 'field list'
Additional information I just realized might be conflicting with it. Before this happens I also renamed the table using:
RENAME TABLE wp_mf_module_groups TO wp_mf_custom_groups
Maybe since the table was just renamed it cant reference it?
Worked when I added backticks to the columns only after WHERE
UPDATE wp_mf_custom_groups,wp_mf_posttypes
SET wp_mf_custom_groups.post_type=wp_mf_posttypes.type
WHERE wp_mf_custom_groups.`post_type=wp_mf_posttypes.id
If query works with PHPmyadmin try this
<?php
$con = mysql_connect($host,$user,$pass) or die('Failed to connect');
$db_selected = mysql_select_db('db_name', $con);
$sql = "UPDATE table1 t1, table2 t2
SET t1.column1 = t2.column2
WHERE t1.column1 = t2.column3";
$result = mysql_query($sql, $con);
if (!$result) {
echo mysql_error();
} ?>
Related
$result = mysqli_query($conn,"SELECT * FROM table1 INNER JOIN `table2`
ON `table1`.`id`=`table2`.`id`;");
while ($row = mysqli_fetch_array($result) ){
echo 'name <textarea>'.$row["name"].'</textarea>';
}
on LAb (wampserver) its working i get information about "name", but on really website i not get infromation about "name" .
May some error occur but aren't displayed.
First I suggest you to check the connection:
$conn = mysqli_connect("$host", "$user "my_password", "$DBname");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
If it works, check for query errors:
if($result = mysqli_query($conn,"SELECT * FROM table1 INNER JOIN `table2`
ON `table1`.`id`=`table2`.`id`;"))
{
while ($row = mysqli_fetch_array($result) ){
echo 'name <textarea>'.$row["name"].'</textarea>';
}
}
else echo($conn->error);
Other possible causes may are:
ON clause
it's true the condition:
ON table1.id=table2.id
or it should be something like this?
`ON `table1`.`id`=`table2`.`rif_id`
no data to select in table1 or table2
In the live database did you check if there are data to select? May happens to forget to insert data in the live database.
Also, did you tried the query manually in PhpMyAdmin?
I don't know what you trying to collect, but you can use below query to get the record. it will work for you.
"Select t1.* from table1 t1 inner join table2 t2
ON t1.id=t2.id"
Please check the connection may be live connection is different. Also remove semicolon(;) after table2.id. May be your problem is resolved
I'm still getting mysql error in my PHP script.
Problematic code:
$mquery = mysql_query("SELECT m.`id`, m.`name`, NULL AS `type`, NULL AS `code`, 0 AS `cat` FROM `menu` m UNION ALL SELECT l.`id`, l.`name`, l.`type`, l.`code`, l.`cat` FROM `lines` l UNION ALL SELECT s.`id`, s.`name`, s.`site`, s.`site`, s.`cat` FROM `sites` s ORDER BY `name` ASC");
if(!$mquery) { echo mysql_error(); die(); }
while($mdata = mysql_fetch_assoc($mquery)) { ... }
When i put this query into PhpMyAdmin - all is OK, i will get result. When I put this query into MySQL Workbench - all is OK, i will get result.
AND NOW: When I run script with parameter (http://domain/index.php?site=ABC) - ALL IS OK. When I run script without parameter (http://domain/index.php) - I get mysql error on this query: "Table 'test.menu' doesn't exist".
What "test.menu"?! Where is "test"? I don't want any "test", I have no "test" in my query. And why is it related on parameter in url? It's not dynamically generated query. Where is problem?
Sorry for my english
Solved, script structure:
$mydb = mysql_connect(...);
mysql_select_db(..., $mydb);
mysql_set_charset('utf8', $mydb);
function newMenu($db)
{
$mquery = mysql_query("...", $db);
if(!$mquery) { echo mysql_error(); die(); }
while($mdata = mysql_fetch_assoc($mquery) { ... }
}
myMenu($mydb);
But what i don't understand is: Why is it working without "$db" when parameter 'site' is in url?
Please check your mysql configuration in PHP. I am pretty sure you are using database name as 'test' because you've copied the code from somewhere. Change it to the actual name of your database and it will work.
The second parameter of mysql_query must be a connection variable.
<?php
$con = mysql_connect("localhost", "root", "mypass") or
die("Could not connect: " . mysql_error());
mysql_select_db("tutorials");
$result = mysql_query("select * from tutorials");
echo "<h2>Here is a list of the topics:</h2>";
while ($row = mysql_fetch_array($result)) {
echo $row['name']."<br />";
}
mysql_close($con);
?>
Could you give full php script? is the database config related to 'site' parameter ?
I want to execute these queries
$q1 = "INSERT INTO t1 (id,desc) VALUES (1,'desc');" <br>
$q2 = "SET #last_id = LAST_INSERT_ID();" <br>
$q3 = "INSERT INTO t2 (parentid,desc) VALUES (#last_id, 'somedesc');"<br>
Will this work correctly 3 mysqli_query something like this?
$res = mysqli_query($q1);
$res2 = mysqli_query($q2);
$res3 = mysqli_query($q3);
To start, desc is a MySQL reserved word
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
and must be wrapped in backticks if you're going to decide on using it, without renaming it to something else than desc, say description for instance.
Therefore, you will need to change it to the following, assuming your DB connection is established, and using $con as an example, which you haven't shown us what your DB connection is.
$q1 = "INSERT INTO t1 (id,`desc`) VALUES (1,'desc')";
$q2 = "SET #last_id = LAST_INSERT_ID()";
$q3 = "INSERT INTO t2 (parentid,`desc`) VALUES (#last_id, 'somedesc')";
minus all of your <br> tags, since you are inside PHP, unless that wasn't part of your code, but in trying to format your code in your question.
Sidenote: Your semi-colons were misplaced.
and passing DB connection to your queries:
$res = mysqli_query($con,$q1);
$res2 = mysqli_query($con,$q2);
$res3 = mysqli_query($con,$q3);
Plus, adding or die(mysqli_error($con)) to mysqli_query() to check for possible errors in your queries.
I have two databases, one online (mysql) and one in my office (SQL Server) which I would like to compare and update where a value is different.
I am using php to connect to the SQL Server database and run a query to retrieve the information, then connecting to the Mysql database running a query. Then I need to compare the two queries and update where necessary.
Is there somewhere I can look for tips on how to do this, I am sketchy on PHP and struggling really.
This is as far as I have got-:
<?php
$Server = "**server**";
$User = "**user**";
$Pass = "**password**";
$DB = "**DB**";
//connection to the database
$dbhandle = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server");
//select a database to work with
$selected = mssql_select_db($DB, $dbhandle)
or die("Couldn't open database $DB");
//declare the SQL statement that will query the database
$query = "SELECT p.id, p.code, ps.onhand";
$query .= "FROM products p with(nolock)";
$query .= "INNER JOIN productstockonhanditems ps with(nolock)";
$query .= "ON ps.ProductID = p.ID";
$query .= "WHERE ps.StockLocationID = 1";
//execute the SQL query and return records
$get_offlineproduct2 = mssql_query($query);
mysql_connect("**Host**", "**username**", "**password**") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$get_onlineproducts = mysql_query(SELECT w.ob_sku, w.quantity
FROM product_option_value AS w
ORDER BY ob_sku)
or die(mysql_error());
//close the connection
mssql_close($dbhandle);
?>
I am looking to compare the value p.code to w.ob_sku and whenever they match copy the value of ps.onhand to w.quantity so the online database has the correct quantities from the office database.
My question I guess is how close am I to getting this right? Also am I doing this the right way, I don't want to get so far and realise that i am just wasting my time...
Thanks!
You do not need to fetch any record from MySQL, since you actually want to update it.
I would do something like this:
$query = 'SELECT p.code, ps.onhand FROM (...)';
// execute the SQL query and return a result set
// mssql_query() actually returns a resource
// that you must iterate with (e.g.) mssql_fetch_array()
$mssqlResult = mssql_query($query);
// connect to the MySQL database
mysql_connect("**Host**", "**username**", "**password**") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
while ( $mssqlRow = mssql_fetch_array($mssqlResult) ) {
$mssqlCode = $mssqlRow['code'];
$mssqlOnHand = $mssqlRow['onhand'];
mysql_query(
"UPDATE product_option_value SET quantity = $mssqlOnHand WHERE ob_sku = $mssqlCode"
// extra quotes may be required around $mssqlCode depending on the column type
);
}
I have a code which was used in an application where I am having a problem in rollback. Even if I 's2' returns false rollback isn't happening i.e. table 'products' is getting droped.
Can anyone explain why it isn't working or how should I change it.
Note: tables are of Innodb engine..I use mysql 5.0+
mysql_query('SET AUTOCOMMIT=0;');
mysql_query('START TRANSACTION;');
$sql = 'DROP TABLE '.$this->Product->tablePrefix.'products';
$s1 = mysql_query($sql);
$sql = 'RENAME TABLE '.$this->Product->tablePrefix.'temp12212 TO '.$this->Product->tablePrefix.'products';
$s2 =mysql_query($sql);
if($s1 && $s2){
mysql_query('COMMIT;');
$this->Session->setFlash('Commit Successful to Database');
}else{
mysql_query('ROLLBACK;');
$this->Session->setFlash('Commit failed due to some errors<br> auto-rollbacked to previous state');
}
DROP TABLE is one of the commands in MySql that cause a implicit commit.
http://dev.mysql.com/doc/refman/5.1/en/implicit-commit.html
Use this instead:
'RENAME TABLE '.$this->Product->tablePrefix.'products TO backup_table
, '.$this->Product->tablePrefix.'temp12212 TO '.$this->Product->tablePrefix.'products';
You cannot rollback a DROP TABLE or RENAME TABLE statement as they cause an implicit commit.
I sorted the problem this way instead!!! thanks all for your reply :-)
$sql = 'DROP TABLE IF EXISTS '.$this->Product->tablePrefix.'temp_backup';
mysql_query($sql);
$sql = 'RENAME TABLE '.$this->Product->tablePrefix.'products TO '.$this->Product->tablePrefix.'temp_backup, '.$this->Product->tablePrefix.'temp TO '.$this->Product->tablePrefix.'products';
$status =mysql_query($sql);
if($status){
$sql = 'DROP TABLE '.$this->Product->tablePrefix.'temp_backup';
mysql_query($sql);
$this->Session->setFlash('Commit Successful to Database');
}else{
$this->Session->setFlash('Commit failed due to some errors<br> auto-rollbacked to previous state');
}