Update mySQL table if row from another table is certain value - php

UPDATE table1 SET row1 =CONCAT( row1, 'word1, word2')
I want to add a condition to the query above.
I'm looking to add text values to row1 on table1, which already contains some texts, the query above does the trick, but additionally to that, I need to add a condition. I have another table called "table2", with a column, let's say "row2" that equals to "1". I need to run the query above IF row2 in table2 equals to 1.
I'm running the query directly on PHP MyAdmin
Thanks in advance.

$query = "SELECT `row2` FROM `table2`";
$res = mysqli_query($query);
$row = mysqli_fetch_array($res);
if($row['row2'] == 1)
{
//run your update table code: UPDATE table1 SET row1 =CONCAT( row1, 'word1, word2')
}

Related

How to read and update table row by row sql and php?

I have a database where I want to read a table row by row and then for each row I want to update a cell. I have written the below code but is there a better way of doing this ? Better way I mean it takes less time than my code.
Here is my code :
$List = 'some text...';
$SQL = "SELECT * FROM TABLE";
$res = mysqli_query($database, $SQL);
while($row = mysqli_fetch_assoc($res)){
if(strpos($List,$row['name']) !== false){
mysqli_query($database, "UPDATE TABLE SET `yes`=1 WHERE name='".$row['name']."'");
}else{
mysqli_query($database, "UPDATE TA SET `yes`=0 WHERE name='".$row['name']."'");
}
}
That approach is not the best one, the idea of SQL is to manage and be able to do that in a more efficient way. Instead of iterating each row and executing total rows +1 queries you can do only 2 queries and update the information.
for Example:
-- Update all to 0
UPDATE TABLE SET `yes`=0 ;
-- Update the one on the list to 1
UPDATE TABLE SET `yes`=1 WHERE name in ('value1','value2','value3');
This approach could save you a lot of execution time and be more efficient event on very large datasets.
i have 2 solution here
Solution 1 :
Copy the table and perform the update and rename the orginal table and also rename copy 1 table to original 1
table_1 copy to table_2
perform the operation on table_2
then rename table_1 to bkp_table_1
then rename table_2 to table_1
Solution 2
with the user select insert the data in new table which you what to update.

PHP Mysql update multiple columns with multiple row values by single query

I am trying to update rows in mysql, but I have to use a "for" loop for multiple update queries, which have multiple values in each query. The queries are like the following:
update table set column1='100',column2='140,column3='150' where id =1
update table set column1='120',column2='145,column3='154' where id =2
update table set column1='141',column2='148,column3='155' where id =3
I am using a "for" loop to run multiple queries with different id's, I want to run a single query to update all rows, but not by using "case". Is that possible?
you can use loop for generating dynamic query.
please have a look.
This might be helpful to you.
$data[] = array("column1"=>100, "column2"=>140, "column3"=>150, "id"=>1 );
$data[] = array("column1"=>120, "column2"=>145, "column3"=>154, "id"=>2 );
$data[] = array("column1"=>142, "column2"=>148, "column3"=>155, "id"=>3 );
foreach($data as $dat){
$query = "UPDATE table SET column1=".$dat['column1'].", column2=".$dat['column2'].", column3=".$da['column2']." WHERE id=".$dat['id'];
echo $query;
}
Why would you put the WHERE clause if you wanna update all the ID's? If you don't wanna update all the ID's you will have to do it in a for loop.
UPDATE tableName SET column1 = '100', column2 = '140', column3 = '150'
You can use WHERE IN(...) for this.
Eg:
update table set column1='100',column2='140,column3='150' where id IN (1,2,3)

Retrieving column name based on user input in MYSQL using php

Currently i am doing a PHP GET and getting values after clicking the submit button.
I got the values out from the submit button but I dont know how to reference them to the values in column.
For example, after clicking the submit button, I get the value 1.
Inside my database i have 3 columns
Col 1 Col 2 Col 3
2 3 1
How do i get the COLUMN name by just having the value itself? Thank you so much!
Ugly/silly, but would do what you want:
Query:
SELECT * FROM yourtable WHERE 1 in (col1, col2, col3, ... ,colN)
and then manually pick out the value in client code:
$row = mysql_fetch_assoc($result);
$matches = array_search(1, $row);
var_dump($matches);
You can't get away from listing all of the columns in the query, because there's no WHERE x IN (*)-type construct.
I would do it like this.
Get all table columns
then loop throug results and run select for each column
$columnVsValue = array();
foreach($columns as $column) {
$q = "SELECT id FROM table where {$column} = {$value}";
if(TRUE) { // you have to check that query return value
$columnVsValue[] = $column;
}
}
I did it quick probably there is more efficient way to do it

Pass PDO result to another temproary table or insert to temp table

Hello ,
$sql = "select col1 , col2 from table where id=2"; // sometimes query larger
$q = $conn->prepare($sql);
$q->execute(array_values($v));
$q->setFetchMode(PDO::FETCH_BOTH);
while($r = $q->fetch())
{
echo " $r[$i]";
}
Code is Working Fine.
Now i want To save Query Result to Another Temporary Table.
i Dont know no. of columns generated in Query result. Each time Query is different so columns and data is different. So How store that Query result to another table.
You could let MySQL do that work for you, e.g. via
CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl WHERE ...
see http://dev.mysql.com/doc/refman/5.1/en/create-table.html

MySQL fetch column value of particular row

I need to fetch a particular column value of a particular row using php in mysql.
For example if I want to fetch column2 value in row2, I tried using:
$qry = mysql_query("SELECT * from $table");
$data = mysql_fetch_row($qry);
$result = $data[1];
but this always returns only the first row value.
SELECT Column2
FROM $table
ORDER BY Something
LIMIT 1,1;
Or, if you know the key of the row
SELECT Column2
FROM $table
WHERE Key = Something
-- Optional: if you want 2nd after filtering
-- ORDER BY Something
-- LIMIT 1,1;
select COLUMNNAME from TABLENAME where ROWELEMENT="SOME_VALUE";
This should be your sql query..
You will need to access the value of that element using
$result['COLUMNNAME']
Complete code should look like this.
$query="select COLUMNNAME from TABLENAME where ROWELEMENT='SOME_VALUE'";
$result=mysql_query($query);
$result=mysql_fetch_array($result);
$columnvalue=$result['COLUMNNAME'];
Its bcoz mysql_query returns the result in an associative array form.
After the above code, You can access all elements like this.
$columnname[$index];
This will return the first value,
According to your question, You will need to first get the column values in a saperate array, and then access it using your index value..

Categories