Mysql update query for update all users data - php

I want a Mysql query to update all users 'status' filed in 'user' table. I can update one user by running following query.
Table
What I tried
update user set status = 1 where id = 1 and type = 'viber'
Do I need to run the above query inside a loop to update all users data?
I want a query like following
update user set status = 1 where id = 1 and type = 'viber' and id = 2 and type = 'twitter' ..
your help is much appreciated.

Just
update user set status=1;
Updates your whole table.

If you want to just update the status for all records then below query is useful for that.
update user set status=1;

The WHEREclause is used to extract only those records that fulfill a specified criterion. but here you don't want that .so just use this query to update all the records
update user set status=1;

just use:
$update = mysqli_query("UPDATE user SET status = 1")or die(mysqli_error());
if this not works, you can select all data and then update each row by setting user id like this :
$select = mysqli_query("SELECT * FROM user")or die(mysqli_error());
while($row = mysqli_fetch_array($select)){
$id = $row['id'];
mysqli_query("UPDATE user SET status=1 WHERE id='$id' ");
}

Related

Check which columns were modified in an UPDATE query

When we update a MySQL record with php, we can check if it has effect using:
$mysqli->affected_rows;
But how do I check which column has been modified?
Example, in my table have the columns: id / name / age
In a record we have the data: 1 / Woton / 18
If I send an: UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'
Only the age field has changed. How can I determine this?
You cannot directly get the updated columns from the query result.
It can be get from some php query. Firstly we will have to select the row from database which we are going to update in a array variable. Than run the update query for the same row.
Lastly get the same row from database from select query in the new array variable.
Finally we get two arrays.
We can get the updated column with the array_diff_assoc php function.
See the below code for the same.
$sql = "SELECT * from mytable where id=1 limit 1";
$prev = mysqli_fetch_assoc(mysqli_query($conn, $sql));
//Get the column data in the array. Before update.
$sql = "UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'";
$conn->query($sql);
// Update data
$sql = "SELECT * from mytable where id=1 limit 1";
$updated = mysqli_fetch_assoc(mysqli_query($conn, $sql));
// Again run the select command to get updated data.
$UpdatedColumns=array_diff_assoc($updated,$prev);
In a different note: If QueryLog has been enabled in the DB then you (or your script in PHP or Python or any) can easily see/read which part of the content has been updated and even you can monitor the DB.
The good news is, even you can target which table, which query etc.

Find and UPDATE value in mysql php

hello i need find my target row in database (phpmyadmin) and update 1 column in that row.
$stmt = $db->prepare("SELECT * FROM users WHERE user LIKE '%{$username}%'");
$stmt->execute();
$result = $stmt->fetchAll();
$db_exp = $result['0']['expdate'];
i find my target with this code but i need update that i can show column now
Use UPDATE:
UPDATE users SET expdate = :newValue WHERE user LIKE :userName

how UPDATE mysql row plus one and get the value

I updated with success
$result = mysql_query("UPDATE $table SET `queue2` = `queue2` + 1 WHERE `id` = '$getid'");
but how can I get the "queue2" value without opening a new request to MySQL
I can simply get the new value with this command
$selresult = mysql_query("SELECT * FROM $table WHERE `id` = '$getid'") or die(mysql_error());
but I'm afraid that the database can get new update again and i will get higher number
Any idea how to do it ?
you can use query to update the value.
mysql_query("UPDATE user_profile SET userpoints = userpoints + 1 WHERE user_id = '".$user_id."'");
See URL:-
PHP + MySQL transactions examples
Try this:-
printf ("Updated records: %d\n", mysql_affected_rows());
mysql_query("COMMIT");
You will need to use a transaction between the queries to be certain.
The docs for transactions are here. A good SO question that covers it in detail: PHP + MySQL transactions examples
Edit:
Looking at it from a different angle, why don't you do it in reverse though? It might save the need for a transaction (thought it is possible that you get multiple reads before a write):
Get the value for your queue2 value to display in the page from this:
mysql_query("SELECT * FROM $table WHERE `id` = '$getid'");
You have the true value now, so you can run:
$result = mysql_query("UPDATE $table SET `queue2` = `queue2` + 1 WHERE `id` = '$getid'");
No transaction and you know the value of the data before the update.

Randomly pairing users only in PHP - solution

So i wanna pairing users only in PHP:
First, get one avaliable user id //right now not random
mysql_query('SELECT id FROM users WHERE state="0" LIMIT 1'); //state 0 = user avaliable, 1 = talking/playing with someone
$available_id = stuffs to get id from query
And then update:
$result = mysql_query('UPDATE INTO users SET state="1" WHERE (id=$available_id OR id=$my_id) AND state="0"');
if $result == false then it mean that no one row has updated. So back to First step,
if $result == true then what? it can mean that my row has updated or available user, or both. I am confused how to done with it.
mysql_query returns false for error. You can check the count of updated rows using mysql_affected_rows
you can use mysql_num_rows();
http://php.net/manual/en/function.mysql-num-rows.php and mysql_affected_rows(); http://php.net/manual/en/function.mysql-affected-rows.php
your query seems wrong $result = mysql_query('UPDATE INTO users SET state="1" WHERE (id=$available_id OR id=$my_id) AND state="0"'); remove into
Try this code
$first=mysql_query('select id from users where state =0 limit 1');
$available=mysql_fetch_row($first);
$available_id=$available['id'];
$result = mysql_query('UPDATE INTO users SET state="1" WHERE
(id=$available_id OR id=$my_id) AND state="0"');
No,
It is like this
INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on
success or FALSE on error.
if the mysql_query return FALSE means you query get failed and have syntactical error, if TRUE, that means the query ran successfully but that does not mean that the row is updated.
You can really check a row is updated or not with mysql_affected_rows after the successfully execution of UPDATE query
$id = mysql_query('SELECT id FROM users WHERE state="0" LIMIT 1');
$uid = mysql_fetch_array($id);
$id2 = mysql_query('SELECT id FROM users WHERE status="0" AND `id`!='".$uid."' ORDER BY rand LIMIT 1');
$uid2 = mysql_fetch_array($id2);
mysql_query("UPDATE user SET `user`='1' WHERE `id` IN($uid,$uid2)");
and pair it in your pair table.

Can I retreave a variable from mySQL database while I am doing UPDATE?

I'm running this query:
if($ModifiedInGoogle != $row['ModifiedInGoogle']) {
// run update query
mysql_query("UPDATE events
SET EventName = '$EventName',
StartDate = '$StartDate',
WHERE GoogleID = '$GoogleID' ");
If the EventID variable is not being changed in the UPDATE, is there a way I can get it and assign it to an $EventID variable here?
No. UPDATE queries are for putting data into the database. If you want to retrieve data, then you use a separate SELECT query.

Categories