clear votes greater than 50 php script - php

I only want to reset votes greater than 50, currently is resets every one
<?
include('mysql_connect.php');
$query = "SELECT id, votes, callback FROM websites";
$result = mysql_query($query) OR die(mysql_error());
$query = "UPDATE websites SET votes = 0";
$result = mysql_query($query) OR die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
echo ('Database has been cleaned with a tissue.');
?>
Im used to coding Java so im not sure if i can use this
$query = "UPDATE websites WHERE votes >= 50 SET votes = 0";
Thanks for your help

you're close. Do this instead.
$query = "UPDATE websites SET votes = 0 WHERE votes >= 50 ";
Also mysql_* functions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

You need the WHERE statement after the SET:
$query = "UPDATE websites SET votes = 0 WHERE votes >= 50";

Related

MySQL alternative to Read/Modify/Write a field

In several PHP codes I have to just increment a field value from a MySQL DB.
Tipically, I use this snippet:
$sql = "SELECT IDpage, numPages FROM Pages WHERE IDpage=".$page;
$result = mysqli_query( $conn,$sql)
$row = mysqli_fetch_array($result);
$num = $row['numPages'] + 1;
$sql = "UPDATE Pages SET numPages=".$num." WHERE IDpage=".$page;;
$result = mysqli_query( $conn,$sql)
Is there any more elegant and concise method?
You don't need to fetch the data first, just do the update.
$sql = "UPDATE Pages SET numPages = numPages + 1 WHERE IDpage = ".$page;
$result = mysqli_query($conn, $sql);
Also, your snippet is missing a few semicolons.

multiple sql statements

$sql = "UPDATE user SET `niveua`='beginner' WHERE `km`< 50";
$result = $con->query($sql);
$sql = "UPDATE user SET `niveua`='trained' WHERE `km`> 50 or `km` < 99";
$result = $con->query($sql);
$sql = "UPDATE user SET `niveua`='expert' WHERE `km`> 100";
$result = $con->query($sql);
I can't get those 3 sql's to work. It's only the third one that is updating.
So how do i get all 3 to work?
Missing AND, also you can simplify your query like below, so that things can be faster
UPDATE user
SET niveua =
CASE
WHEN (`km` < 50 )
THEN 'beginner'
WHEN (`km`> 50 AND `km` < 99 )
THEN 'trained'
WHEN ( `km` > 100 )
THEN 'expert'
ELSE niveua
END;

Cant figure out this mysql to mysqli change

Hi there Im still trying to change to mysqli, and I just can get things to go right some times.
The biggest thing I have is the mysqli_result, ive tried what other people have done, and doesnt seem to work.
Here is the code below:
$result = mysqli_query($con, "SELECT referer FROM users WHERE userId = '$key'");
if(mysql_result($result, 0) != "" ){
$referer = mysql_result($result, 0);
$result = mysqli_query($con, "SELECT referer FROM users WHERE userId = $referer'");
if(mysql_result($result, 0) != "" ){
$result2 = mysqli_query($con, "SELECT refered FROM users WHERE userId = $referer'");
$newRefs = mysql_result($result2, 0) + 1;
mysqli_query($con, "UPDATE users SET refered = '$newRefs' WHERE userId = '$referer'");
$result3 = mysqli_query($con, "SELECT userName FROM users WHERE userId = '$key'");
$refered = mysql_result($result3, 0);
}
}
Help would be appreciated.
Kind Regards
Chad
You can't mix mysql_ and mysqli_ functions like that. Also, mysql_result is serious old school. There is no equivalent in mysqli (and that's a good thing). I switched to mysqli_fetch_assoc, which takes your query and returns an associative array with the field names as keys. I kept it all procedural for the sake of uniformity (I hate mixing OOP with procedural). I should note that your code is horribly convoluted as written (for instance $key isn't defined anywhere). It's better to avoid reusing variable named like you have. I also HIGHLY recommend switching to an all-object codebase.
$result = mysqli_query($con, "SELECT referer FROM users WHERE userId = '$key'");
if($row = mysqli_fetch_assoc($result)){
$result2 = mysqli_query($con, "SELECT referer FROM users WHERE userId = '" . $row['referer'] . "'");
if($row2 = mysqli_fetch_assoc($result2)){
$result3 = mysqli_query($con, "SELECT refered FROM users WHERE userId = '" . $row2['referer'] . "'");
$newRefs = mysqli_fetch_assoc($result3);
mysqli_query($con, "UPDATE users SET refered = '" . $newRefs['refered'] . "' WHERE userId = '" . $row['referer'] . "'");
$result4 = mysqli_query($con, "SELECT userName FROM users WHERE userId = '$key'");
$refered = mysqli_fetch_assoc($result4);
}
}
You cannot use mysql_result!
Try to do it like this:
$result = mysqli_query($con, "SELECT referer FROM users WHERE userId = '$key'");
if( mysqli_num_rows($result, 0) ) {
list($referer) = mysqli_fetch_row($result);
....
You can use object oriented style:
$Result = $Con->query("SELECT referer FROM users WHERE userId = '$key'");
if( $Result->num_rows ) {
list($referer) = $Result->fetch_row();
If you're in the process of switching, you should go straight to PDO, not mysqli.
mysqli vs pdo - stackoverflow

Updating the row with the biggest value

In the "user_id" column of my table, I'd like to insert the ID of the user who just registred from my page. The idea is to associate his recent generated income with the users id, just to spot an eventual double registration of the income.
In order to do this, I tought to update the user_id column, on the row where income_id has the biggest value, i.e. the last generated income, but something isn't working. My code is:
$query = "SELECT max( id_income ) FROM `affiliate_income`";
$last_income = mysql_query($query, $conn) or die(mysql_error());
$last = mysql_fetch_assoc($last_income);
$updtsql = "UPDATE affiliate_income SET `id_user`=".$row_user_code['id_user']."WHERE id_income =".$last;
$result = mysql_query($updtsql, $conn) or die(mysql_error());
any ideas?
Actually you can do it in one query,
UPDATE affiliate_income a
INNER JOIN (SELECT MAX(id_income) id_income FROM affiliate_income) b
ON a.id_income = b.id_income
SET a.id_user = 'valueHere'
You get the value of $last as array.So you have to giv the query like the following
$updtsql = "UPDATE affiliate_income SET `id_user`=".$row_user_code['id_user']."WHERE id_income =".$last['id_income'];
You try to get the max id_income but in the second query (updtsql) you try to find the id_income = array. Besides, you do not put a white space before WHERE clause.
$query = "SELECT max( id_income ) AS ii FROM `affiliate_income`";
$last_income = mysql_query($query, $conn) or die(mysql_error());
$last = mysql_fetch_assoc($last_income);
$updtsql = "UPDATE affiliate_income SET `id_user`=".$row_user_code['id_user']." WHERE id_income =".$last['ii'];
$result = mysql_query($updtsql, $conn) or die(mysql_error());

MySQL MyISAM race condition

The function has next structure:
$q = 'LOCK TABLES table1 WRITE;';
mysql_query($q);
$q = 'select id from table1 where is_delete = 0 limit 1;';
$res = mysql_fetch_assoc(mysql_query($q));
if($res) {
$q = 'UPDATE table1 SET is_delete = 1 WHERE id = '".$res['id']."'';
mysql_query($q);
}
$q = 'UNLOCK TABLES;';
mysql_query($q);
I locking all tables, but queries run parallel.
How fix this?
Check whether you're getting any MySQL errors on the LOCK TABLES query:
$q = 'LOCK TABLES table1 WRITE';
$r = mysql_query($q) or die(mysql_error());
However, if this is all you are doing, you can also simply write:
UPDATE `table1 SET `is_delete` = 1 WHERE `is_delete` = 0 LIMIT 1
which does not require any locks at all. Of course, this will only work if the data from your first query is not being processed in any way, and if it doesn't really matter which row you are updating, as long as it's one in which is_delete is set to 0. This is what the code you posted does, too, but it's not immediately obvious to me what you would want to use this code for :)
More generally, if you are using the default InnoDB storage engine for your MySQL table, you may want to look into SELECT ... FOR UPDATE:
http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html
You could write:
$q = 'select id from table1 where is_delete = 0 limit 1 for update';
$res = mysql_fetch_assoc(mysql_query($q));
if($res) {
$q = 'UPDATE table1 SET is_delete = 1 WHERE id = '".$res['id']."'';
mysql_query($q);
}
See also: http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/

Categories