I'm having trouble adding 1 to a column value in MySQL. I've used backticks on the column name and value isn't incrementing. Here is my query:
$update = $connectdb->prepare("UPDATE `strings` SET posted=posted, `response-comment`=`response-comment` + 1 WHERE `id`=?");
$update->execute(array($id));
Why isn't my query working? The value $id is correct, the column response-comment should increase by 1.
Try using this for your SQL statement (presuming strings is the name of your table:
UPDATE `strings` SET `response-comment`=`response-comment` + 1 WHERE `id`=?
Be careful with tick marks
If improperly coded you can end up with quotes which is going to transform you integer value into a string and thus changing the behavior of your request.
Have you tried with out , justresponse-comment = response-comment + 1`
Related
I'm working on syncing two PostgreSQL databases using a PHP script. I am not able to query the entire table so I have to use an id column to grab records in batches.
The id column is a string column, not numeric. However, there are numerical ids in the column. This is where I'm having an issue.
When I prepare the SQL statement in PHP, when I happen to get an id that is numeric, when I bind it to the statement, it doesn't put quotes around the value because it thinks its an int, not a string.
How do I force it to be a string and always put single quotes around the id??
If I put the quotes around the ? in the query it treats it as text and the parameter doesn't get bound to the statement.
As you can see in the code I also tried casting the $start variable as a string. $start contains the starting id.
Here is the code:
$sql = "select id from properties where id > ? order by id limit ?";
$params = [(string) $start, 50000];
$rows = $this->wolfnet->select($sql, $params);
Any quick assistance will be highly appreciated. I've table row in mysql named as mob_categories, now data is being is saved as '|' separated for instance (cat 1|cat 2|cat 3 and so on. Now i need to get the value from another column in the same table if the input value matches.
for instance if the value is cat 1 i need to select the value from another column is named as deveice_token wherever it matches with 'cat 1'
I tried this code but its not working somehow
SELECT * from table_name where find_in_set('cat 1',mob_categories) <> 0
so i modified a bit with the following code but it works with only numeric value if the value exits in this format in this format (i.e) 1,2,3
$userNotification = $wpdb->get_results("SELECT * from table_name where 1 IN (mob_categories); ");
What's i'm missing specifically?
Since data in mob_categories are separated by | rather than by comma (,) so you need to make it compatible for FIND_IN_SET first.
So replace all the | by comma (,) first.
SELECT * from table_name where find_in_set('cat 1', REPLACE(mob_categories,'|',',')) > 0
I am creating an website where i am using user specified values as the column name of my table and it works fine but, when it comes to update it am unable to do it in the same process
here are my codes to create a column name from user specified value
mysqli_query($sql,"ALTER TABLE `cmpcheck` ADD `$emailID` VARCHAR( 100 ) NOT NULL ");
Here are my codes to update the above column
mysqli_query($sql,"UPDATE cmpcheck SET `$emailID` = `".$q."` LIMIT 1 ");
Any Help?
You are using the wrong quotes for your value:
mysqli_query($sql,"UPDATE cmpcheck SET `$emailID` = '".$q."' LIMIT 1 ");
^ ^
With a backtick (`) you are referencing to a column-name.
Besides that: make sure to properly escape both $emailID and $q
Backticks go around table and column names. You should use single quotes around string values.
mysqli_query($sql,"UPDATE cmpcheck SET `$emailID` = '$q' LIMIT 1 ");
Also, there's no reason to switch from variable substitution (as you do for $emailID) to concatenation with ..
It would be even better to use a prepared statement to substitute $q, instead of variable substitution into the query. But you can't do that for the column name.
You could try :
mysqli_query($sql,"UPDATE cmpcheck SET `".$emailID."` = '".$q."' LIMIT 1 ");
Also look at preparing your statements.
I have a column called name in a table info
So I want to update that with it's current value, like this name + user_input. I tried with this code but not working
mysql_query("UPDATE info SET name = name + '$user_input' WHERE id='$user_id'");
But It returns 0 and update column to 0....
Any idea how to accomplish this task??
You should use CONCAT to concatenate string in MySQL (+ is for addition arithmetic operations which I guess can't properly work with names) :
UPDATE info SET name = CONCAT(name,'$user_input') WHERE id='$user_id'
I feel like there is a simple solution, but I cant find it anywhere.
I've got a while() loop and at the beginning of each loop, I subtract 1 from a number that I get from a MYSQL database. But for some reason, it stops at 0. I need the number to go negative if the value pulled from the database is 0 or a negative number.
//Get members
$select = mysql_query("SELECT * FROM members") or die(mysql_error());
//Start the loop
while($members= mysql_fetch_row($select)){
//Get limit
$limit = $members['limit'];
$newlimit = $limit - 1;
mysql_query("UPDATE `members` SET `limit` = '".$newlim."' WHERE `email`='".$members['email']."'");
}
Any advice? This code successfully works until the value gets to 0. But I need it to keep subtraction.
Thanks in advance!
Brandon
Edited: updated the code above. Had an error
My guess is that yout limit column is an UNSIGNED INT. Unsigned numbers can be twice as large as signed numbers, but they cannot have a negative value (hence unsigned).
You could use an ALTER TABLE statement to change the column to INT, but you need to be careful because if any of the values stored in the limit field are greater than 2147483647 (if you change it to a signed INT), these values will not be preserved.
remove the extra = symbol
$newlimit = $limit - 1;
and replace $newlim with $newlimit in your SQL statement
also please note that you could be doing the same thing in a single SQL statement without any loop or php involved:
UPDATE `members` SET `limit`=`limit`-1