Displaying Not Found Data From MySQL












-3















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]";
}
?>



  1. 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?


  2. I have more than one record for a query but the script displays just one data. Please help me to show all records.











share|improve this question




















  • 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
















-3















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]";
}
?>



  1. 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?


  2. I have more than one record for a query but the script displays just one data. Please help me to show all records.











share|improve this question




















  • 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














-3












-3








-3








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]";
}
?>



  1. 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?


  2. I have more than one record for a query but the script displays just one data. Please help me to show all records.











share|improve this question
















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]";
}
?>



  1. 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?


  2. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















0














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.






share|improve this answer































    1














    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.";
    }





    share|improve this answer


























    • 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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 12:08









        Ravi Kumar GuptaRavi Kumar Gupta

        7381029




        7381029

























            1














            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.";
            }





            share|improve this answer


























            • 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
















            1














            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.";
            }





            share|improve this answer


























            • 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














            1












            1








            1







            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.";
            }





            share|improve this answer















            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.";
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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



















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith