Retrieve last inserted id based on column PHP [duplicate] - php

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.

Related

Select Specific Rows in a Specific Order [duplicate]

This question already has an answer here:
MySQL sort by some list
(1 answer)
Closed 2 years ago.
I have a script (in PHP) that goes through a bunch of different comparisons to generate an ordered array of entries in a table by the row id. Then I'm imploding the array into a string and using WHERE to select those specific rows, however I don't know how to order them in the same order as they were in the array.
$order_array = [50,49,42,52,53,54,51,48,47]
$order_string = implode(',', $order_array);
// echo $order_string returns '50,49,42,52,53,54,51,48,47'
$sql_todo = "SELECT * FROM todo_list WHERE id IN ({$order_string})";
if ($result_todo = mysqli_query($link, $sql_todo)) {
while ($row_todo = mysqli_fetch_assoc($result_todo)) {
This successfully selects the desired rows, but they are not in the same order as the array. I know that I haven't told it to order them that way (so it didn't), but I don't know how to make it happen.
Thanks for your time,
Seth
You could use the field() function:
"SELECT * FROM todo_list WHERE id IN ({$order_string}) ORDER BY FIELD(id, {$order_string})"
field() returns the index of its first argument in the list, which you can directly use for ordering.
Side note: you should probably use a prepared statement rather than concatenating values in the query string (if your values come from outside your code, this is a must-have).

How to get random tuples from database? [duplicate]

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

Storing data, in arranged sequence into mysql

I asked random question to student & stored his answers using his session id into mysql & than extracted the same way.
I used Order By RAND() function in my query while asking the question
$query = "SELECT * FROM question ORDER BY RAND() LIMIT 0,1";
But Now I Want to Store answers in an arranged sequence for such purpose i can use Q_ID but i don't want to show q_id to user. So how can i store q_id into another table without showing it to user.
Secondly i want to show correct answers to my students too.
but i really don't know how to tackle these thing :(
query for storing answers
$order= "INSERT INTO radio (Option1,Option2,Option3,user) VALUES ('".$Option['Option1']."','".$Option['Option2']."','".$Option['Option3']."','".session_id()."')";
Query For Extracting Data From mysql
$qry=mysql_query("SELECT * FROM radio where user='".session_id()."'", $con);
Without your actual database schema/more information it is difficult to give you the best advice possible.
Add a field to the table radio so that it looks something like this:
RADIO (*user*, *questionId*, Option1, Option2, Option3)
(due to stack overflow formatting stars represent the primary key(s))
where questionID is a foreign key that references QUESTION(id). If you don't have an id field in your question table add that too.
Then when you display the question to the user, save the question ID as a hidden input field inside the tag like so:
<input type='hidden' name='questionId' value='".$data['id']."' />
When the form is submitted you will have the question ID available in your $_POST array at $_POST['questionId']
Then you can modify your INSERT query to insert the question ID as well and simply not display it when you display the data later.
--
As for displaying the correct answers to the students, you will need to store the correct answer in your table somehow. One way would be to add a field to your question table indicating which of the options is the correct answer, I.E. a TINYINT(1) which will contain a 1, 2, or 3 depending on which answer is correct. You can then use that to generate a page with the correct answer to the question.

How to echo random row from database? [duplicate]

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.

How to count and display clicks on a link [duplicate]

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'

Categories