This question already has answers here:
Fetching single row, single column with PDO
(6 answers)
Closed 5 years ago.
I have a table called users with columns(username , name , password , email) ,
I want to get the last inserted row in username column.
I tried something like that :
$statement = $db->prepare("SELECT username FROM users ORDER BY DESC LIMIT 1");
$statement->execute();
$row = $statement->fetch();
But it's returning an array, I just need one value as a string.
How could that be done?
if you want to get the last inserted, you might want to use an increment int field as an ID, not a string.
selecting username and order it desc would not give you the last inserted username.
by using increment int id, you can do this
"select username from users order by id desc limit 1"
as for the username value of your code, you can do $username = $row["username"]
Related
This question already has answers here:
Deleting a row based on the max value
(4 answers)
Closed 5 years ago.
I want to Delete a row with the highest date, but the table I am working with doesn't have auto increment ID's instead it has multiple rows with the same ID, but each row has different information.
I want to DELETE the highest date, because that is also the last date that is inserted with that ID so that is why I want to use MAX, otherwise I am deleteing all the rows with that ID.
Right now I am trying (This is my query from my PHP file):
DELETE FROM onderhoudsLog
WHERE systeemContractID = :systeemContractID
AND startDatum = MAX(:startDatum)
The DELETE does not work.
This is my PHP function:
function onderhoud($id, $startDatum){
$query = "DELETE FROM onderhoudsLog WHERE systeemContractID = :id AND startDatum = MAX(:startDatum");
$q = $this->DB->prepare($query);
$q->execute(Array(
':id' => $id,
':startDatum' => $startDatum,
));
}
Order the data by date and delete only the first record
DELETE FROM onderhoudsLog
WHERE systeemContractID = :systeemContractID
ORDER BY startDatum DESC
LIMIT 1
This question already has answers here:
MySQL Select By Newest Timestamp
(4 answers)
Closed 6 years ago.
I select the latest inserted id from my mySQL database. I also want to select the appropriate name to that latest id.
$pdo = $db->query('SELECT *,MAX(id) AS latest FROM data');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
$id = $row["latest"];
$name = $row["name"];
}
The selecting of the id is working well. But not the latest name is selected, instead always the name of the first row of my table is selected. It doesn't fit to the id
Why not just
SELECT name, id FROM data ORDER BY id DESC LIMIT 1
I wanted to write following answer but i have to confess that i find e4c5's answer better.
SELECT * FROM data where id = (SELECT max(id) FROM data)
This question already has an answer here:
Simplest method to retrieve single (and only) row in SQLite using PDO
(1 answer)
Closed 6 years ago.
I am using PDO+Mysql where I am passing udid from a session and if the rowcount is greater than 0, I would like to fetch id from the table as well as first_name, which is the fifth column in the table.
The problem is id is always returned right but not the first_name, not sure why
$stmt = $con->prepare("SELECT * FROM members WHERE id= :udid");
$stmt->bindValue(':udid', $_SESSION['udid']);
$stmt->execute();
$id=$stmt->fetchColumn();
$first_name=$stmt->fetchColumn(5);
I think you can use fetch() to fetch the entire row and then call each column by its name
$stmt->execute();
$result = $stmt->fetch();
$id = $result['id'];
$name = $result['name'];
//and so on for all the columns
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 7 years ago.
How do I select from a database table a user id that is linked to the minimum value of the table
for example
User ID Pending
------- --------
0 5
1 4
2 7
'$resultSet = $mysqli->query("SELECT MIN(write_pending) FROM writerdata_tb");
if ($temp = $resultSet->fetch_assoc()) {
}'
in this case, I would want the data that is returned to have the value of the user with the least pending, in this case being user 1. How do I set up a MySqli query to handle this? And what would be the best way to save the result as a variable for use in the rest of my php?
Probably something like this, but it depends heavily on what your table structure is like. I've assumed you've got a generic users table with some IDs and this pending value you've mentioned.
SELECT userID FROM users WHERE pending = ( SELECT MIN(pending) FROM users );
The nested Select statement gets the smallest pending value in that column of your table, then you can refine your select userID statement by forcing pending to be the value returned from that nested select call.
UPDATE
To clarify your followup question of processing results, here's a modification of the code you provided:
$resultSet = $mysqli->query("SELECT userID FROM users WHERE pending = ( SELECT MIN(pending) FROM users )");
if($resultSet->num_rows > 0) {
//We use a while loop here, in the event that there are multiple rows
while($row = $resultSet->fetch_assoc()) {
echo $row["userID"]; //Accessing the rows index at "userID"
}
}
This question already has answers here:
Deleting Duplicates in MySQL
(4 answers)
Closed 9 years ago.
This is the table:
username: id:
john 1
john 2
john 56
john 75
john 98
Now I want query and delete one of these rows randomly and keep the other four- assuming I do not know the value of "ID" because it was Auto-Incremented.
Here's the code, I'm not sure what to add so it only deletes one of the rows associated with 'john'
$query = ("DELETE FROM table WHERE username='$name' && id=''");
How do I modify this so it deletes one row with the name john and not all of them?
Fixed Code
Simple solution that worked.
$query = ("DELETE FROM table WHERE username='$name' LIMIT 1");
I believe this will work:
$name = 'john';
$query = ("DELETE FROM table WHERE id = (SELECT id FROM table WHERE username = '$name' ORDER BY RAND() LIMIT 1)");
I'm not sure if its possible, but perhaps LIMIT the result to 1?
$query = ("DELETE FROM table WHERE username='$name' LIMIT 1");