Lets say I wanted to increment a counter in a database every time someone visits a webpage.
The database called 'example' looks like this:
|Name.....| Value..........| id |
===================
|count......| 5................| 1 | <---- 1st row
and the code on the webpage looks like this:
$db = mysqli_connect("localhost", ....);
$q = "SELECT Value FROM example WHERE id = '1'";
$r=mysqli_query($db, $q);
$result = mysqli_fetch_array($r);
$increment = $result[0] + 1;
$q = "UPDATE example SET Value = '$increment' WHERE id = '1'";
mysqli_query($db,$q);
If two people access the webpage at the same time, person A will fetch the value of 5 and immediately after that, person B will fetch the same value. They will both increment it to six and both perform the update one after the other entering 6 as the counter value when it should really be 7 since two people visited the page. How can I prevent this?
you can do it in one statement:
"UPDATE example SET Value = Value + 1 WHERE id = '1'";
so also no other task can change it between your read and update.
Here your script
$db = mysqli_connect("localhost", ....);
$q = "UPDATE example SET Value = Value + 1 WHERE id = '1'";
mysqli_query($db,$q);
Related
guys
I don't know why but php convert my value into 0 each time.
So, i have video table.
Inside i have a row called like and dislike.
Lets say i have 10 dislike.
Every time i check previously value like that:
$sql = "SELECT * FROM video WHERE video_id = '$id'";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()){
$like=$row['vid_like'];
$dislike=$row['vid_dislike'];
and i echo dislike i got 10 in return
Just after that i do that for increase dislike:
$dislike1= $dislike +1;
echo "dis = {$dislike1}";
$sqla = "UPDATE video SET vid_dislike='$dislike1' AND vid_like='$like' WHERE video_id = '$id' ";
mysqli_query($conn, $sqla);
my echo result is 11. But when i check table after that query, my dislike value is set to 0...
Your update query is not well formatted.
It must look like this :
UPDATE t1 SET col1 = col1 + 1, col2 = col1;
See documentation.
Here you wrote "AND" instead of a comma, which is by the way useless in this case since the second part is vid_like='$like' where $like value didn't change.
So this is the code I have so far, I'm leaving out the code I used to connect to the database since that's not important and isn't the problem.
$id = strip_tags(mysql_real_escape_string($_GET['id']));
$sql_value = "SELECT value FROM table";
$sql = mysql_query("UPDATE table SET value='[idk what to do here]' WHERE id='$id'");
so the $id selects the id from the url or API and the $sql_value selects the values from the table.
I want the value in the same row as the id specified to increment by 1
E.G.
id = 0, value = 0;
id = 1, value = 0;
id = 2, value = 0;
id = 3, value - 0;
If in the API I type: "id=2"
I want the PHP script to increment the corresponding "value" by 1
E.G.
id = 0, value = 0;
id = 1, value = 0;
id = 2, value = 1;
id = 3, value - 0;
Assuming that your value field is an INT. You can do that by:
"UPDATE table SET value=value+1 WHERE id='$id'"
But i strongly recommend you to take a look at mysqli or PDO and start to make prepared statements to handle the data. See more here:
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
http://php.net/manual/en/pdo.prepare.php
Use:
$sql = mysql_query("UPDATE table SET value=value+1 WHERE id='$id'");
As long as the value is an int this will work:
UPDATE table SET value=value + 1 WHERE id='$id'
UPDATE tablename t SET value = ( t.value+1 ) WHERE t.id = '$id'
Do you want?
"UPDATE table SET value=value+1 WHERE id='$id'"???
I have a standard MySQL database, with around 60 rows (as in user accounts). When I first made it I made the mistake of making session IDs the same as the simple account ID, now I want to fix my mistake and I am obviously not going to go through 60 rows to reset them different secure session IDs, so I am writing this function:
function generate_sessionid(){
return bin2hex(openssl_random_pseudo_bytes(32));
}
function assign_all_sessionids(){
$sessionid = generate_sessionid();
$conn = sql_connect();
$result = mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE 1");
sql_disconnect($conn);
}
assign_all_sessionids();
Problem: Every account in the database gets the same random session ID as the rest. How do I make it recall the function for each row in order to allow it to be random for each row?
Try get user's count from DB and simply execute it N times
function assign_all_sessionids(){
$conn = sql_connect();
// getting users count
// here just change 'id' to your id parameter
$result = mysqli_query($conn, "SELECT id FROM accounts");
$arr = $result->fetch_array(MYSQLI_NUM);
// executing N times
for($i = 0; $i < $result->num_rows; $i++){
$sessionid = generate_sessionid();
// here just change 'id' to your id parameter again
mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE `id`=".$arr[$i]);
}
sql_disconnect($conn);
}
You can do what you want by first setting all the session ids to NULL:
UPDATE accounts
SET sessionid = NULL;
Then, inside the loop:
UPDATE accounts
SET sessionid = '$sessionid'
WHERE sessionid IS NOT NULL
LIMIT 1;
Normally you don't want to execute queries in a loop, however in this case you need to get all of the current unique identifiers, loop and generate a new identifier and then update one:
function assign_all_sessionids(){
$conn = mysqli_connect('whatever...');
$select = mysqli_query($conn, "SELECT sessionid FROM accounts");
while(list($id) = mysqli_fetch_assoc($select)) {
$sessionid = generate_sessionid();
$update = mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE sessionid='$id'");
}
}
I have a page that when the user clicks on a horse to see more info on that horse (referred by ID number in mysql table), it only returns the first record, NOT the one you chose.
The page is http://www.cbarlranch.com/?pg=forsale
<?php require_once('dbaseinfo.php'); ?>
<?php
//set variables
$colname_rsStallion = "1";
if (isset($HTTP_GET_VARS['ID'])) {
$colname_rsStallion = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['ID'] : addslashes($HTTP_GET_VARS['ID']);}
//select database
mysql_select_db($db, $conn);
//build the query
$query_rsStallion =
sprintf("SELECT * FROM table WHERE ID = '%s'", $colname_rsStallion);
//set more variables
$rsStallion = mysql_query($query_rsStallion, $conn) or die(mysql_error());
$row_rsStallion = mysql_fetch_assoc($rsStallion);
$totalRows_rsStallion = mysql_num_rows($rsStallion);
?>
Please help. Thanks!
Try using $_GET['ID'] rather than $HTTP_GET_VARS. If your IF fails the it will default to the record with ID = 1 (which is likely your first record)
How do I update the row $pageviews by +1 each time someone visits my page. The table name is news and the row to be updated is $pageviews whose default value is +1. Should I use the update function? And how do I implement it? Below is my code. Can you show a demonstration using these data?
$data = mysql_query("SELECT * FROM news") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$id = $info['id'];
$pageviews = $info['pageviews'];
}
All you want to do is to increment pageviews value each time that page renders. All you need for that:
1) Get the current value
2) Run UPDATE query updating current value + 1
To do so, you can write a function that would look like as,
function inc_page_views($id) {
$res = mysql_query(sprintf("SELECT * FROM `news` WHERE `id` ='%s'", mysql_real_escape_string($id)));
$data = mysql_fetch_array($res);
// Target view count
$target = $data['pageviews'];
$query = sprintf("UPDATE `news` SET `pageviews` = '%s' WHERE `id` ='%s'", $target + 1, $id);
return mysql_unbuffered_query($query);
}
And then, when rendering a page, you can simply call inc_page_views(..here page id..) and it will do the rest
use this query
mysql_query("Update news SET pageviews = pageviews + 1 ");
or
store the current page id in $current_page_id.
mysql_query("Update news SET pageviews = pageviews + 1 where id = '$current_page_id' ");