/    Sign up×
Community /Pin to ProfileBookmark

PHP echo last inserted row

Hey every one the question is simple but it is not working.
I have a query and i need to echo all the rows in this order :
First row echoed : Have the biggest ID
Last row echoed : Have the Smallest ID

Here is the code :

[code=php]
<div class = “chat” style = “padding : 1em;” >
<form action = “index.php” method = “POST” >
<input type = “text” name = “chat” style = “width:50%; height: 30px;” onkeypress=”return isNumberKey(event)” maxlength=”130″ >
<input type = “submit” name = “csub” value = “Share” >
</form>
</div>
<?php
if (empty($_POST[‘chat’])) {
echo ‘Share what is on your mind !’;
}else{
if (isset($_POST[‘csub’])) {
$chat = $_POST[‘chat’];
$date = date(“F j, Y, g:i a”);
require ‘connect.php’;
$userc = $_COOKIE[“user”];
mysqli_query($GLOBALS[“___mysqli_ston”], “INSERT INTO chat VALUES (‘NULL’, ‘$userc’, ‘$chat’, ‘$date’) “);
header(“Location: index.php”);
mysqli_close();
}
}

$servername = “localhost”;
$dbusername = “root”;
$password = “”;
$dbname = “a_database”;
$conn = new mysqli($servername, $dbusername, $password, $dbname);
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

$sql = ”
SELECT * FROM chat
WHERE Username = ‘$userc’
OR Username IN(SELECT User_two FROM friends WHERE User_one = ‘$userc’)
OR Username IN(SELECT User_one FROM friends WHERE User_two = ‘$userc’)
“;
$result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo ‘
<div style = “padding : 1em; border :6px double gray; margin-top: 15px; background-color: white;” >
<table style = “width: 90%”>
<tr>
<td><b>’.$row[“Username”].'</b></td>
<td> ~ ‘.$row[“date”].'</td>
</tr>
<tr>
<td><IMG src=”http://www.mathblogging.org/images/icons/small-default-avatar.jpg”></td>
<td>’.$row[“chat”].'</td>
</tr>
</table>
</div>’;
}
}
?>

[/code]

Please i have been looking for days and no answer. Thanks for reading.
All i want is to tell me how to edit the code because i tried using all the PHP functions i could find :
Last_Inserted_Id
Order by …
ANd none work they always give me errors. (expected mysqli Boolean given)

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 28.2015 — <i>
</i>. . . ORDER BY whatever_the_id_column_is DESC
Copy linkTweet thisAlerts:
@JadauthorJul 29.2015 — <i>
</i>. . . ORDER BY whatever_the_id_column_is DESC
[/QUOTE]


Ok so i did this and my code is now :

[code=php]<?php
include 'header.php';
?>

<div class = "chat" style = "padding : 1em;" >
<form action = "index.php" method = "POST" >
<input type = "text" name = "chat" style = "width:50%; height: 30px;" onkeypress="return isNumberKey(event)" maxlength="130" >
<input type = "submit" name = "csub" value = "Share" >
</form>
</div>
<?php
if (empty($_POST['chat'])) {
echo 'Share what is on your mind !';
}else{
if (isset($_POST['csub'])) {
$chat = $_POST['chat'];
$date = date("F j, Y, g:i a");
require 'connect.php';
$userc = $_COOKIE["user"];
mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO chat VALUES ('NULL', '$userc', '$chat', '$date') ");
header("Location: index.php");
mysqli_close();
}
}

$servername = "localhost";
$dbusername = "root";
$password = "";
$dbname = "a_database";
$conn = new mysqli($servername, $dbusername, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "
SELECT * FROM chat
WHERE Username = '$userc'
OR Username IN(SELECT User_two FROM friends WHERE User_one = '$userc')
OR Username IN(SELECT User_one FROM friends WHERE User_two = '$userc')
ORDER BY whatever_the_id_column_is DESC
";

$result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '
<div style = "padding : 1em; border :6px double gray; margin-top: 15px; background-color: white;" >
<table style = "width: 90%">
<tr>
<td><b>'.$row["Username"].'</b></td>
<td> ~ '.$row["date"].'</td>
</tr>
<tr>
<td><IMG src="http://www.mathblogging.org/images/icons/small-default-avatar.jpg"></td>
<td>'.$row["chat"].'</td>
</tr>
</table>
</div>';
}
}
include 'footer.php';
?>
[/code]


thats the right way right ? well here is the error im getting :

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:wampwwwDemoSiteindex.php on line 43
Copy linkTweet thisAlerts:
@JadauthorJul 29.2015 — OK NVM IM JUST STUPID LOL i replaced the ORDER BY whatever_the_id_column_is DESC by ORDER BY id DESC AND it worked ! thx alot ...
×

Success!

Help @Jad 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 5.16,
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,
)...