I´ve been trying to make "most popular article" script
retrieve the most popular is no problem.... but I have tried every "add count+" methods
but the count field in my mysql always shows 0
This is my script
$add = "1";
$counter=mysql_query("SELECT * FROM news WHERE newsid = '".$newsid."'");
while ($ntcounter=mysql_fetch_array($counter)) {
mysql_query("UPDATE news SET count = '".$ntcounter[count]+$add."' WHERE newsid = '".$newsid."'")
}
I´m starting to think if the database is not updateable
Is there something I´m missing here?
You can do this in one go:
UPDATE news SET `count` = `count`+1 WHERE newsid = '".$newsid."'
EDIT:
<?php
//TURN ON ERROR REPORTING!!!
error_reporting(E_ALL);
//Type cast the variable to an integer, despite where its set
(int)$newsid=1;
//or
(int)$newsid=$_GET['id'];
//$newsid="1"; is setting 1 as a string
mysql_query("UPDATE news SET `count` = `count`+1 WHERE newsid=".$newsid);
?>
Note if your not checking or casting type. always remember to use mysql_real_escape_string()
You shouldn't be doing it like that at all. MySQL has built-in functionality for increasing values, you can just do:
UPDATE news SET count = count+1 WHERE newsid = '$newsid'
Yes. Your $add should be an integer.
$add = 1;
Related
I'm new to SQL. I've been scouting to understand why this doesn't work but I can't find the solution.
The idea: I want to slect a random row that has 'seen = 0',
display it and update it as 'seen = 1' so it doesn't show anymore.
Here's the code:
$query = 'SELECT * FROM website WHERE seen in (0) ORDER BY RAND() LIMIT 1';
$result = mysql_query($query);
while ($plop = mysql_fetch_array($result))
{
$website_url = $plop['url'];
};
mysql_query('UPDATE website SET seen = 1 WHERE url = $website_url');
The SELECT displays correctly but the seen stays at 0
Thanks for the help!
EDIT: Changing the quotes from single to double made it... Thanks!
mysql_query("UPDATE website SET seen = 1 WHERE url = '$website_url'");
In PHP, single quotes mean that there is no interpolation. You should use double quotes, so that $website_url is replaced by its value in the second query.
The query SELECT * FROM TABLE WHERE id LIKE '%1% is not working properly, it's not select the id 1.
mysql_connect('localhost', 'root' , '');
mysql_select_db('database');
$sql = ("select * from search WHERE id LIKE '%3%'");
mysql_query($sql);
$my_variable = mysql_query($sql);
$display_data = mysql_fetch_row($my_variable);
while ($list = mysql_fetch_assoc($my_variable)) {
$id = $list['id'];
$title = $list['title'];
$keywords = $list['keywords'];
$img = $list['img'];
$link = $list['link'];
}
If you are looking to SELECT id 1 then use = not LIKE. The way LIKE is being used it will match every id that has a 1 in it and you are not guaranteed to get the first one in order, so instead use:
SELECT * FROM search WHERE id = 1
According to the PHP documentation of mysql_fetch_row it
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
Which means that the first result won't show up in the next (mysql_fetch_assoc) procedure. You could try removing the $display_data = mysql_fetch_row($my_variable); line and only use the while($list = mysql_fetch_assoc($my_variable)) { ... } procedure. See if that solves your problem.
$sql = ("select * from search WHERE id ='3'");
The id is an integer use = instead of like . Equal is more accurate.
And first echo your query in your program->
echo $sql;die;
copy that query and run it on your phpmyadmin
and then check is your column id is int type if it is then like will not give you the result. You have to use the where clause here .But if you have the column id is of type varchar then definitely give you the result .
Try to use search tab under your database->table in your phpmyadmin and put the condition there.
You will definitely get your answer there.
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.
If I perform the following MySQL query through PHP:
UPDATE pictures
SET category = '0'
WHERE category = '$categoryID'
AND username = '$username'";
$queryUncat = mysql_query($uncategorise) or die(mysql_error());
It works fine and any category that was equal to $categoryID gets changed to 0. However, if I perform the following:
UPDATE pictures
SET category = '0',
pictureorder = (SELECT COUNT(category) + 1 WHERE category='0' AND username='$username')
WHERE category = '$categoryID'
AND username = '$username'";
$queryUncat = mysql_query($uncategorise) or die(mysql_error());
Not only does pictureorder not equal to the count of the category row plus one, but the category no longer gets changed if equal to $categoryID. I'm not too good at figuring this out as I know only basic MySQL through PHP and am not familiar with it through its own console.
Thanks in advance for any suggestions.
This form of query is not valid. First, your subquery is missing a FROM clause. Second, you cannot select from the same table you are updating in the same query.
http://dev.mysql.com/doc/refman/5.1/en/update.html
Hey, I have a field called STATUS and it is either 1 to show or 0 to hide. My code is below. I am using an edit in place editor with jQuery. Everytime you update it creates a new ROW which I want, but I want only the new one to have STATUS = 1 and the others to 0. Any ideas on how I would do that?
<?php
include "../../inc/config.inc.php";
$temp = explode("_", $_REQUEST['element_id'] );
$field = $temp[0];
$id = $temp[1];
$textboxval = stripslashes(mysql_real_escape_string(preg_replace('/[\$]/',"",$_REQUEST["update_value"])));
$query = "INSERT INTO notes ($field,status,date,c_id) VALUES ('$textboxval','1',NOW(),'$id')";
mysql_query($query);
echo($_REQUEST['update_value']);
?>
I am not sure exactly what you mean - do you want to make all the entries except the new one have status = 0? If so, just issue an update before the insert:
UPDATE notes SET status = 0
However, I should also note that you have a potential SQL injection to worry about. By stripping slashes after applying "mysql real escape string", you are potentially allowing someone to put text in your SQL statement that will execute an arbitrary SQL statement.
Something like this, sorry for the post before, I mis read it the first time then went back:
<?php
include "../../inc/config.inc.php";
$temp = explode("_", $_REQUEST['element_id'] );
$field = $temp[0];
$id = $temp[1];
$textboxval = mysql_real_escape_stringstripslashes((preg_replace('/[\$]/',"",$_REQUEST["update_value"])));
// set older entries to 0 - to not show but show in history
$hide_notes = "UPDATE notes SET status = 0";
mysql_query($hide_notes);
// add new entry with status of 1 to show only latest note
$query = "INSERT INTO notes ($field,status,date,c_id) VALUES ('$textboxval','1',NOW(),'$id')";
mysql_query($query);
echo($_REQUEST['update_value']);
?>
i just ran in to a problem I didn't of the set up of my table doesn't allow me to show more than one client a time and i will be having numerous clients, my bad on planning ha
You really want to get the ID of the newly generated row and then trigger an UPDATE where you all rows where the ID is not the new row, e.g.
UPDATE notes SET status = 0 WHERE id != $newly_generated_id
If the ID column in your table is using AUTO_INCREMENT you can get its ID via "SELECT LAST_INSERT_ID()" and then use the return value in that statement in your UPDATE statement.
Pseudo code:
$insert = mysql_query("INSERT INTO ...");
$last_id = mysql_query("SELECT LAST_INSERT_ID()");
$update = mysql_quqery("UPDATE notes SET status = 0 WHERE id != $last_id");
The only caveat to this approach is where you might have a brief moment in time where 2 rows have status=1 (the time between your INSERT and the UPDATE). I would wrap all of this in a transaction to make the whole unit more atomic.