/    Sign up×
Community /Pin to ProfileBookmark

No error messages but no data displayed either? SQL LAST / TOP 1

I don’t know why I’m not getting error reports and that there is nothing being shown, there is data in the database.

Login info ommited

I’m trying to echo back the last entry for a specific column.

[code=php]<?php
error_reporting(E_ALL);
error_reporting(-1);

$servername = “localhost”;
$username = ” “;
$password = ” “;
$dbname = ” “;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = “SELECT TOP 1 Rc FROM computers
ORDER BY Rc DESC”;
$result = $conn->query($sql);
echo “$result”;
?>[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 15.2014 — $result will be either a Boolean or a mysqli_result object, not a string, so there would be nothing (logically) to echo, assuming there are no errors.
[code=php]
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
die("<pre>Connect error: '".$conn->connect_error."'</pre>");
// do something "nicer" for production version
}
$sql = "SELECT TOP 1 Rc FROM computers
ORDER BY Rc DESC";
$result = $conn->query($sql);
if($result == false) {
die("<pre>Query error: '".$conn->error."'</pre>");
// ditto
}
// let's see what you got, then decide how you want to deal with it:
echo "</pre>RESULT:n";
while(($row = $result->fetch_assoc()) != false) {
print_r($row);
}
echo "</pre>";
[/code]

(WARNING: totally untested, and I work almost exclusively with PDO these days, so who know what mistakes I made. ? )
Copy linkTweet thisAlerts:
@iBeZiDec 15.2014 — Using "TOP" is only valid in SQL Server as far as I know, for MySQL you should be using "LIMIT"

<i>
</i>SELECT * FROM computers
ORDER BY Rc DESC LIMIT 1
×

Success!

Help @GreenAce92 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.18,
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,
)...