This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to set cookies for uuid
Hello, my question is how to count and display clicks on a php value (since the link id changes a lot, ex. http://site.com/id=232323) . so for example, i want to display this
http://gjfkgfkgf.com/id=12345- 323 clicks
instead of this
http://gjfkgfkgf.com/id=12345
Create a MySQL table with the fields id and count
Every time the site is loaded, connect to the MySQL database, fetch the count value from the database using the id in the URL, add 1 to the count and UPDATE the MySQL row with the new count.
If you feel like being advanced you can add 1 to the count column with a single query :-)
UPDATE linkcount
SET `count`=`count`+1
WHERE `id`='12345'
Now you can fetch the number of visits to a specific link by
SELECT `count`
FROM linkcount
WHERE `id`='12345'
Related
This question already has answers here:
How to randomly select rows in SQL?
(13 answers)
Closed 3 years ago.
I'm working on creating a online quiz program. I want to change questions every time new user logging in. Is there any way to get questions from database randomly?
I have tried rand() function but it generates same question again and again.
You can use NEWID() of MySQL. The SQL SELECT NEWID() function returns the random row. GUID in memory for each row. By definition, the GUID is unique and random so, you will get the perfect random record.
Example:
SELECT * FROM Table ORDER BY NEWID()
TO know more you can click here
This question already has answers here:
Update the value of a field in database by 1 using codeigniter
(3 answers)
Codeigniter, increase database value by value contained in variable [duplicate]
(3 answers)
Closed 21 days ago.
My database contains a table, in this table I have a column "Earned". This column contains some value and I have to add some amount to this column(Earned).
Right now I am able to do in this way..
$this->db->select('Earned');
$this->db->where('CustMobile', $LinkMobile);
$query_result=$this->db->get('customers');
$result=$query_result->row();
$finalAmount=$result->Earned+$Commsion;
$this->db->set('Earned', $finalAmount);
$this->db->where('CustMobile', $LinkMobile);
$this->db->update('customers');
I want to know how I can update this value with a single query?
My database looks like this.
I want to change this circled value 1100 to 2000
Try this,
$this->db->query("UPDATE `customers` SET `Earned` = `Earned` + $Commsion WHERE `CustMobile` = $LinkMobile");
Explanation
Earned = Earned + $Commsion ==> This part will grab the value of the field and add the new value.
Example - If $Commsion is 1000 and your respective Earned field is 1100. So it will update as 2100.
although you have accepted the answer and but I would like to add the active record query for it:
$this->db->set('Earned', 'Earned +'. $Commsion .'',false);
$this->db->where('CustMobile', $LinkMobile);
$this->db->update('customers');
This question already has answers here:
Get last insert id after a prepared insert with PDO
(3 answers)
Closed 7 years ago.
I'm looking to be able to retrieve the last inserted id based on username. I know within PDO I am able to use to use lastInsertId() but from what I can tell that only gets the last one inserted with no other parameters. Is this possible in a short and simple?
The only way I thought I could do this was using something like
SELECT id FROM table_name WHERE username=:username ORDER BY enteredTimenDate ASC
Would this be the easiest way I could do this?
EDIT to address duplicate issue. The "Original" question this question is considered a duplicate of just asks how to retrieve the last inserted ID, my question was retrieving the last inserted ID based on another parameter.
Use following query to get the last inserted id:
SELECT id FROM table_name WHERE username=:username ORDER BY enteredTimenDate desc limit 1;
Data is sorted based on the enteredTimenDate in descending order and then picking up the first row by limiting the query result.
This question already has answers here:
How do I reset sequence numbers to become consecutive?
(5 answers)
Closed 8 years ago.
I have a phpmyadmin table with auto increment id. I display this entries 12 in each page, so 1-12 are in the first page 13-24 to the second page and so on. Each entry has a specific position so 1 is top left 4 is top right 9 is bot left and 12 is bot right. If one entry is deleted, (say entry with id=3) from the database, when displayed the entry position is left empty, because when it tries to get the information with databse id=3 it gets nothing. Is there a way when the row is deleted from the databse, to make everything after that entry in the database with -1 id?? so if i delete entry 3, entry 4 becomes the new entry 3, entry 5 becomes the new entry 4 and so on. Is there a function in phpmyadmin that does this or do i have to make my own, or is there a trick I can implement.
try out below code
$deletedId = $id;
//here will be your delete query
//perform below update query after deleting the record
mysql_query("UPDATE $tbaleNAme SET id = (id-1) WHERE id > $deletedId");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL: How to retrieve a random row or multiple random rows?
Pull 5 random records from mysql database
I have a large list of domains in my database in the table "list" under the column "website".
There are about 140 million rows and I just need to know how to echo a random one.
Basically like this:
<?php include('directory/database.php'); ?>
WHATEVER CODE WORKS FOR GETTING A RANDOM DOMAIN FROM THE DATABASE
<?php
echo $domain;
?>
There's just a huge amount of records so I need to know the quickest way to just have one of them randomly selected and displayed on a page. Thanks!
You definitely don't want to use ORDER BY RAND(). MySQL has to build a temporary table. If your table has a unique id column, something like this is much better:
SELECT * FROM `table`
WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` )
ORDER BY id LIMIT 1;
See this blog post for other approaches that work well in php.
Note: This simply repeats my answer on this thread.