Bharath Mg
Bharath Mg

Reputation: 1127

Fetching rows in Php MySQL Database

I want to increment $x in this function to get next record in table. But it is incrementing only once . Any Help is appreciated

This is the function I call,

<script type="text/javascript">
    function getnextques(){
        document.getElementById("quescontent").innerHTML= '<?php
        $x++;
        $res = mysql_query("SELECT * FROM question_content WHERE id=".$x) or die(mysql_error());  
        while($row=mysql_fetch_array($res)){
            echo $row['ques'] ;
        }; 
        ?>'
}
</script>

from ,

<a href="javascript:getnextques()" class="startbutton" id="mediumone"> Next </a>

Upvotes: 0

Views: 561

Answers (3)

Mihai Iorga
Mihai Iorga

Reputation: 39704

you cannot mix like that Javascript with PHP. PHP is server side based, javascript is browser based.

You should use Ajax for that (jQuery)

eg:

<script type="text/javascript">
var x = 0;
function getnextques(){
    var newX = x++;
    $.ajax({
        type: "POST",
        url: 'script.php',
        data: { x: newX },
        success: function(data) {
            document.getElementById("quescontent").innerHTML = data;
        }
    });
}
</script>

and script.php

<?php
    // mysql connection
    $x = (int) $_POST['x'];
    $res = mysql_query("SELECT * FROM question_content WHERE id=".$x) or die(mysql_error());  
    while($row=mysql_fetch_array($res)){
        echo $row['ques'] ;
    }
 ?>

Upvotes: 4

Sunish Menon
Sunish Menon

Reputation: 162

If you are not familiar with Ajax, or could not use it for any reason, take the user to next page to show the next question.

Upvotes: -1

Gustav
Gustav

Reputation: 1341

Your php code runs on the server and will be statically returned to the browser, where the javascript runs. You need to use Ajax in order to dynamically fetch content.

Upvotes: 0

Related Questions