Displaying Not Found Data From MySQL
I wrote simple code to fetch data from MySQL using PHP.
This is the code:
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_BOTH);
{
echo "<p>";
echo "$row[id_kendaraan]";
echo "<p>";
echo "$row[mangkal]";
}
?>
The script is working, if the data is returned by the db call, I can see the results displayed. But, if the query result has no data, the script just displays blank. I want to show a message that says - 'Data not found'. How can I do that?
I have more than one record for a query but the script displays just one data. Please help me to show all records.
php mysqli
add a comment |
I wrote simple code to fetch data from MySQL using PHP.
This is the code:
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_BOTH);
{
echo "<p>";
echo "$row[id_kendaraan]";
echo "<p>";
echo "$row[mangkal]";
}
?>
The script is working, if the data is returned by the db call, I can see the results displayed. But, if the query result has no data, the script just displays blank. I want to show a message that says - 'Data not found'. How can I do that?
I have more than one record for a query but the script displays just one data. Please help me to show all records.
php mysqli
1
To show the message when no row is found you have to check the row count, and to show all the rows, use fetchall and then use a while or foreach loop.
– Soheyl
Jan 2 at 11:56
@Soheyl sorry, i new to php and mysql. How to do that?
– Ahmad Z
Jan 2 at 11:59
Check the answer below :)
– Soheyl
Jan 2 at 12:07
If an answer solved it, just accept it by ticking the checkmark next to it.
– Funk Forty Niner
Jan 2 at 14:55
@FunkFortyNiner sorry, I newbie with stacjoverflow
– Ahmad Z
Jan 4 at 13:46
add a comment |
I wrote simple code to fetch data from MySQL using PHP.
This is the code:
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_BOTH);
{
echo "<p>";
echo "$row[id_kendaraan]";
echo "<p>";
echo "$row[mangkal]";
}
?>
The script is working, if the data is returned by the db call, I can see the results displayed. But, if the query result has no data, the script just displays blank. I want to show a message that says - 'Data not found'. How can I do that?
I have more than one record for a query but the script displays just one data. Please help me to show all records.
php mysqli
I wrote simple code to fetch data from MySQL using PHP.
This is the code:
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_BOTH);
{
echo "<p>";
echo "$row[id_kendaraan]";
echo "<p>";
echo "$row[mangkal]";
}
?>
The script is working, if the data is returned by the db call, I can see the results displayed. But, if the query result has no data, the script just displays blank. I want to show a message that says - 'Data not found'. How can I do that?
I have more than one record for a query but the script displays just one data. Please help me to show all records.
php mysqli
php mysqli
edited Jan 2 at 14:56


Funk Forty Niner
1
1
asked Jan 2 at 11:51
Ahmad ZAhmad Z
124
124
1
To show the message when no row is found you have to check the row count, and to show all the rows, use fetchall and then use a while or foreach loop.
– Soheyl
Jan 2 at 11:56
@Soheyl sorry, i new to php and mysql. How to do that?
– Ahmad Z
Jan 2 at 11:59
Check the answer below :)
– Soheyl
Jan 2 at 12:07
If an answer solved it, just accept it by ticking the checkmark next to it.
– Funk Forty Niner
Jan 2 at 14:55
@FunkFortyNiner sorry, I newbie with stacjoverflow
– Ahmad Z
Jan 4 at 13:46
add a comment |
1
To show the message when no row is found you have to check the row count, and to show all the rows, use fetchall and then use a while or foreach loop.
– Soheyl
Jan 2 at 11:56
@Soheyl sorry, i new to php and mysql. How to do that?
– Ahmad Z
Jan 2 at 11:59
Check the answer below :)
– Soheyl
Jan 2 at 12:07
If an answer solved it, just accept it by ticking the checkmark next to it.
– Funk Forty Niner
Jan 2 at 14:55
@FunkFortyNiner sorry, I newbie with stacjoverflow
– Ahmad Z
Jan 4 at 13:46
1
1
To show the message when no row is found you have to check the row count, and to show all the rows, use fetchall and then use a while or foreach loop.
– Soheyl
Jan 2 at 11:56
To show the message when no row is found you have to check the row count, and to show all the rows, use fetchall and then use a while or foreach loop.
– Soheyl
Jan 2 at 11:56
@Soheyl sorry, i new to php and mysql. How to do that?
– Ahmad Z
Jan 2 at 11:59
@Soheyl sorry, i new to php and mysql. How to do that?
– Ahmad Z
Jan 2 at 11:59
Check the answer below :)
– Soheyl
Jan 2 at 12:07
Check the answer below :)
– Soheyl
Jan 2 at 12:07
If an answer solved it, just accept it by ticking the checkmark next to it.
– Funk Forty Niner
Jan 2 at 14:55
If an answer solved it, just accept it by ticking the checkmark next to it.
– Funk Forty Niner
Jan 2 at 14:55
@FunkFortyNiner sorry, I newbie with stacjoverflow
– Ahmad Z
Jan 4 at 13:46
@FunkFortyNiner sorry, I newbie with stacjoverflow
– Ahmad Z
Jan 4 at 13:46
add a comment |
2 Answers
2
active
oldest
votes
You need to run a loop to iterate through each result. Something similar to this -
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
while($row = $result->fetch_array(MYSQLI_BOTH)) {
echo "<p>";
echo "$row[id_kendaraan]";
echo "</p><p>";
echo "$row[mangkal]";
echo "</p>";
}
?>
In the code above, a while
loop is used to iterate through results one by one. Each time $row
will be updated with new data and will be printed.
add a comment |
Use a while
loop like following:
if($result->num_rows > 0)
{
while($row = $result->fetch_array(MYSQLI_BOTH))
{
echo "<p>".$row[id_kendaraan]."</p><br><p>".$row[mangkal]."</p>";
}
} else {
echo "No Record Found.";
}
and how to displaying message that the data not exist or not matches with the query?
– Ahmad Z
Jan 2 at 12:14
I have updated the answer. Please check it.
– dexter
Jan 2 at 12:26
Oke, thanks you @dexter
– Ahmad Z
Jan 2 at 12:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005854%2fdisplaying-not-found-data-from-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to run a loop to iterate through each result. Something similar to this -
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
while($row = $result->fetch_array(MYSQLI_BOTH)) {
echo "<p>";
echo "$row[id_kendaraan]";
echo "</p><p>";
echo "$row[mangkal]";
echo "</p>";
}
?>
In the code above, a while
loop is used to iterate through results one by one. Each time $row
will be updated with new data and will be printed.
add a comment |
You need to run a loop to iterate through each result. Something similar to this -
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
while($row = $result->fetch_array(MYSQLI_BOTH)) {
echo "<p>";
echo "$row[id_kendaraan]";
echo "</p><p>";
echo "$row[mangkal]";
echo "</p>";
}
?>
In the code above, a while
loop is used to iterate through results one by one. Each time $row
will be updated with new data and will be printed.
add a comment |
You need to run a loop to iterate through each result. Something similar to this -
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
while($row = $result->fetch_array(MYSQLI_BOTH)) {
echo "<p>";
echo "$row[id_kendaraan]";
echo "</p><p>";
echo "$row[mangkal]";
echo "</p>";
}
?>
In the code above, a while
loop is used to iterate through results one by one. Each time $row
will be updated with new data and will be printed.
You need to run a loop to iterate through each result. Something similar to this -
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
while($row = $result->fetch_array(MYSQLI_BOTH)) {
echo "<p>";
echo "$row[id_kendaraan]";
echo "</p><p>";
echo "$row[mangkal]";
echo "</p>";
}
?>
In the code above, a while
loop is used to iterate through results one by one. Each time $row
will be updated with new data and will be printed.
answered Jan 2 at 12:08


Ravi Kumar GuptaRavi Kumar Gupta
7381029
7381029
add a comment |
add a comment |
Use a while
loop like following:
if($result->num_rows > 0)
{
while($row = $result->fetch_array(MYSQLI_BOTH))
{
echo "<p>".$row[id_kendaraan]."</p><br><p>".$row[mangkal]."</p>";
}
} else {
echo "No Record Found.";
}
and how to displaying message that the data not exist or not matches with the query?
– Ahmad Z
Jan 2 at 12:14
I have updated the answer. Please check it.
– dexter
Jan 2 at 12:26
Oke, thanks you @dexter
– Ahmad Z
Jan 2 at 12:29
add a comment |
Use a while
loop like following:
if($result->num_rows > 0)
{
while($row = $result->fetch_array(MYSQLI_BOTH))
{
echo "<p>".$row[id_kendaraan]."</p><br><p>".$row[mangkal]."</p>";
}
} else {
echo "No Record Found.";
}
and how to displaying message that the data not exist or not matches with the query?
– Ahmad Z
Jan 2 at 12:14
I have updated the answer. Please check it.
– dexter
Jan 2 at 12:26
Oke, thanks you @dexter
– Ahmad Z
Jan 2 at 12:29
add a comment |
Use a while
loop like following:
if($result->num_rows > 0)
{
while($row = $result->fetch_array(MYSQLI_BOTH))
{
echo "<p>".$row[id_kendaraan]."</p><br><p>".$row[mangkal]."</p>";
}
} else {
echo "No Record Found.";
}
Use a while
loop like following:
if($result->num_rows > 0)
{
while($row = $result->fetch_array(MYSQLI_BOTH))
{
echo "<p>".$row[id_kendaraan]."</p><br><p>".$row[mangkal]."</p>";
}
} else {
echo "No Record Found.";
}
edited Jan 2 at 12:26
answered Jan 2 at 12:04


dexterdexter
1,28311020
1,28311020
and how to displaying message that the data not exist or not matches with the query?
– Ahmad Z
Jan 2 at 12:14
I have updated the answer. Please check it.
– dexter
Jan 2 at 12:26
Oke, thanks you @dexter
– Ahmad Z
Jan 2 at 12:29
add a comment |
and how to displaying message that the data not exist or not matches with the query?
– Ahmad Z
Jan 2 at 12:14
I have updated the answer. Please check it.
– dexter
Jan 2 at 12:26
Oke, thanks you @dexter
– Ahmad Z
Jan 2 at 12:29
and how to displaying message that the data not exist or not matches with the query?
– Ahmad Z
Jan 2 at 12:14
and how to displaying message that the data not exist or not matches with the query?
– Ahmad Z
Jan 2 at 12:14
I have updated the answer. Please check it.
– dexter
Jan 2 at 12:26
I have updated the answer. Please check it.
– dexter
Jan 2 at 12:26
Oke, thanks you @dexter
– Ahmad Z
Jan 2 at 12:29
Oke, thanks you @dexter
– Ahmad Z
Jan 2 at 12:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005854%2fdisplaying-not-found-data-from-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
To show the message when no row is found you have to check the row count, and to show all the rows, use fetchall and then use a while or foreach loop.
– Soheyl
Jan 2 at 11:56
@Soheyl sorry, i new to php and mysql. How to do that?
– Ahmad Z
Jan 2 at 11:59
Check the answer below :)
– Soheyl
Jan 2 at 12:07
If an answer solved it, just accept it by ticking the checkmark next to it.
– Funk Forty Niner
Jan 2 at 14:55
@FunkFortyNiner sorry, I newbie with stacjoverflow
– Ahmad Z
Jan 4 at 13:46