/    Sign up×
Community /Pin to ProfileBookmark

How To Force MySql With Php To Show Final Row Only?

Buds!

How to code so php forces mysql to show only the final row of the column ?
The followings are how I coded (2 copied youtube tuts) to show all rows to allow user to delete multiple rows:

SAMPLE 1

[code=php]
<?php
session_start();
require “conn.php”;
require “site_details.php”; ?>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>Follow Users</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<body>
<form name=”form” action=”” method=”post”>
<table border=1 cellpadding=1 cellspacing=1>
<tr>
<th>Id</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>Delete</th>
</tr>
<?php
$res=mysqli_query($conn,”SELECT * FROM users”);
while($row=mysqli_fetch_array($res))
{
echo “<tr>”;
echo “<td>”; ?> <input type=”checkbox” name=”num[]” class=”other” value=”<?php echo $row[“id”]; ?>” /> <?php echo “</td>”;
echo “<td>”; echo $row[“ids”]; echo “</td>”;
echo “<td>”; echo $row[“usernames”]; echo “</td>”;
echo “<td>”; echo $row[“passwords”]; echo “</td>”;
echo “<td>”; echo $row[“emails”]; echo “</td>”;
echo “</tr>”;
}
?>
</table>
<input type=”submit” name=”submit” value=”delete selected”>
</form>
<?php
if(isset($_POST[“submit”]))
{
$box=$_POST[‘num’];
while (list ($key,$val) = @each ($box))
{
mysqli_query($conn,”DELETE FROM users WHERE id=’$val'”);
}
?>
<script type=”text/javascript”>
window.location.href=window.location.href;
</script>
<?php
}
?>

</body>
</html>

[/code]

SAMPLE 2:

[code=php]

<?php
session_start();
require “conn.php”;
require “site_details.php”; ?>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>Follow Users</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<body>
<table border=1 cellpadding=1 cellspacing=1>
<tr>
<th>Id</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>Delete</th>
</tr>
<?php
$sql = “SELECT * FROM users”;
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result))
{
echo “<tr>”;
echo “<td>”.$row[‘ids’].”</td>”;
echo “<td>”.$row[‘usernames’].”</td>”;
echo “<td>”.$row[‘passwords’].”</td>”;
echo “<td>”.$row[’emails’].”</td>”;
echo “<td><a href=delete2b.php?id=”.$row[‘ids’].”>Delete</a></td>”;
}

?>
</table>
</body>
</html>

[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 10.2017 — I'm not sure what you mean by "final row of the column"?

If you want the last result row of a query based on some sort of column ordering, you can use an appropriate ORDER BY with a LIMIT of 1, e.g.
<i>
</i>SELECT col_1, col_2, col_3
FROM your_table
ORDER BY col_1 DESC
LIMIT 1
Copy linkTweet thisAlerts:
@ThrallixApr 10.2017 — You should use prepared mysqli.

Regular MySQLI has a huge risk of being injected.

$stmt = $conn->prepare("SELECT col1, col2, col3 FROM users WHERE username = ? ORDER BY id DESC LIMIT 1");

$stmt->bind_param("s", $username);

$stmt->execute();

$stmt->store_result();

$stmt->bind_result($col1, $col2, $col3);

$stmt->fetch();
Copy linkTweet thisAlerts:
@uniqueideamanauthorApr 12.2017 — I'm not sure what you mean by "final row of the column"?

If you want the last result row of a query based on some sort of column ordering, you can use an appropriate ORDER BY with a LIMIT of 1, e.g.
<i>
</i>SELECT col_1, col_2, col_3
FROM your_table
ORDER BY col_1 DESC
LIMIT 1
[/QUOTE]



Thanks. I got likewise samples from 3 others too.

Here's what my note looks like:

Sample 1

SELECT * FROM users

ORDER BY id DESC

LIMIT 1

Sample 2

SELECT * FROM users ORDER BY usernames DESC LIMIT 1

Sample 3

select * from tbl_name where id=:id order by desc limit 1

Sample 4

SELECT col_1, col_2, col_3

FROM your_table

ORDER BY col_1 DESC

LIMIT 1
×

Success!

Help @uniqueideaman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...