Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

trying to learn How to Make Load More Results From Database using jQuery,Ajax,PHP and MySQL

Dear Experts, I'm trying to learn How to Make Load More Results From Database using jQuery,Ajax,PHP and MySQL

I found this simple code on the web which is suppose be working but I have the below error.

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in ....

I tried a few things but it didn't work. What do you suggest I should do?

On the top of my two pages I have database connection, and my code is :
<?php 
.......
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
?>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

        <script type="text/javascript">
            $(document).ready(function () {
                $("#load").click(function () {
                    loadmore();
                });
            });

            function loadmore()
            {
                var val = document.getElementById("result_no").value;
                $.ajax({
                    type: 'post',
                    url: 'getrecord.php',
                    data: {
                        getresult: val
                    },
                    success: function (response) {
                        var content = document.getElementById("result_para");
                        content.innerHTML = content.innerHTML + response;
                        document.getElementById("result_no").value = Number(val) + 2;
                    }
                });
            }
        </script>

    </head>

    <body>

    <center>
        <p id="heading">How to Make Load More Results From Database using jQuery,Ajax,PHP and MySQL</p>
        <div id="content">
            <div id="result_para">
                <?php
                $select = mysqli_query($con, "SELECT * FROM siparis limit 0,2");
                while ($row = mysqli_fetch_assoc($select)) {
                    echo "<p class='result'>" . $row['_key'] . "<br>" . $row['uid'] . "</p>";
                }
                ?>
                <input type="hidden" id="result_no" value="2">
                <input type="button" id="load" value="Load More Results">
            </div>
        </div>
    </center>

</body>
</html>

Open in new window


on my seccond page which is getrecord.php

.....
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
    $no = $_POST['getresult'];
    $select = mysqli_query($con,"SELECT * siparis LIMIT $no,2");
	
    while ($row = mysqli_fetch_assoc($select)) {
        echo "<p class='result'>" . $row['_key'] . "<br>" . $row['uid'] . "</p>";
    }

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

I don't see an initial connection to the database.  You need something like this before you try to make a query.
$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

Open in new window


https://www.php.net/manual/en/function.mysqli-connect.php

Also look here.
https://www.php.net/manual/en/mysqli-result.fetch-assoc.php
Avatar of BR

ASKER

Dear Dave Baldwin,
I have it on top of my page.

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
my errror message is : Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in ....
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

Thank you so much Chris Stanyon,
That solved my problem.
Now, It brings me the data, however, it only brings it once,
when I click it load more, it doesn't.

it brings it only once.
what do you suggest I should do?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

You are great Chris Stanyon,
thank you very much
You're welcome