0

I am trying to use ajax (very much a novice in that area) and/or jquery to refresh a div with my mysql query results. My index.php looks something like this:

<head>
$( document ).ready(function() {
(function showscreen() {
   $.ajax({
    url: 'screen.php', 
    success: function(data) {
       $('#screen').html(data);
    },
    complete: function() {

      setTimeout(showscreen, 5000);
    }
  });
})(); 
});
</head>
<body>
<div id="screen"></div>
<?php

require '../dbauth/videos.inc.php';
//This handles my db connections.

$videosdbhandle = mysql_connect(

$videoshostname, 
$videosusername, 
$videospassword

) or die("INVALID USERNAME OR PASSWORD");

$videosselected = mysql_select_db($videosdbname, $videosdbhandle);

$checkvideosql = "SELECT * FROM videos ORDER BY votes DESC";    
$checkvideoquery = mysql_query($checkvideosql, $videosdbhandle); 
$checkvideocount = mysql_num_rows($checkvideoquery); 

if ($checkvideocount > 0) {

$count = 0;

while ($checkvideocount > $count) {

$thevideoname = mysql_result($checkvideoquery, $count, "name");
$thevideovideo = mysql_result($checkvideoquery, $count, "video"); 
$thevideoid = mysql_result($checkvideoquery, $count, "id"); 
$thevideoimage = mysql_result($checkvideoquery, $count, "image"); 

    echo '<div class="screen" id="video';
    echo $thevideoid; 
    echo '" style="display: none; background-image: url(';
    echo "'http://backcurrents.com/videos/";
    echo $thevideoname;
    echo '/';
    echo $thevideoimage;
    echo "'";
    echo ');">';

    echo '<video loop muted autoplay 
poster="http://backcurrents.com/videos/';
    echo $thevideoname;
    echo '/';
    echo $thevideovideo; 
    echo '" class="videobg">
    <source src="http://backcurrents.com/videos/';
    echo $thevideoname;
    echo '/';
    echo $thevideovideo; 
    echo '" type="video/mp4">
    </video>';
    echo '</div>';


$count++;
}


}else{

echo 'NO VIDEOS!';

}

?>
</body>

and then my screen.php looks something like this:

$videosusername = "theusername";
$videospassword = "thepassword";
$videoshostname = "localhost";
$videosdbname = "thedbname";

$videosdbhandle = mysql_connect(

$videoshostname,
$videosusername, 
$videospassword

) or die("INVALID SHOW NAME OR PASSWORD");

$videosselected = mysql_select_db($videosdbname, $videosdbhandle);

$checkhighestvideosql = "SELECT * FROM videos ORDER BY votes DESC";
$checkhighestvideoquery = mysql_query($checkhighestvideosql, 
$videosdbhandle);
$checkhighestvideocount = mysql_num_rows($checkhighestvideoquery); 



if ($checkhighestvideocount > 0) {
$x=0; 

while ($checkhighestvideocount > $x) {

$checkhighestvideoid = mysql_result($checkhighestvideoquery, $x, "id"); 

if ($x=0) {

echo '<script type="text/javascript">';
echo '$(".screen").hide();
$("#video';
echo $checkhighestvideoid; 
echo '").show();  ';
echo '</script>';

}else{
echo '<script type="text/javascript">';
echo '$(".screen").hide();
';
echo '</script>';
}

$x++;
 }

}else{
echo "NO VIDEOS!";  
}

All I am trying to do is refresh my mysql query every 5 seconds but nothing is appearing in the div that the screen.php is supposed to. I have tried several different iterations of this, and error checked that my ajax code is firing by simply placing: echo "TEST TEXT"; in my screen.php, and that works. any time I insert a mysql query into the screen.php, nothing appears. Is there a better method than this to acheive what I am trying to? I basically have a voting system built into a mysql database which stores votes. the video with the most votes gets played onto in the index.php file im building.

Dustin
  • 147
  • 1
  • 1
  • 13
  • 1
    If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5, and completely removed in PHP 7 (which is so old it no longer even receives active support). Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Feb 07 '18 at 23:26
  • Thank you, noted, and am aware of this, however assuming my sql connections were indeed using prepared statements or mysqli, do you have any insight into this? – Dustin Feb 07 '18 at 23:36
  • `$x=0` is an assignment. Not a comparison. Whether that's your issue, I don't know. You don't explain exactly what your AJAX is receiving. Have you tried navigating to the page directly to test? Is it just a blank page? Have you checked error logs etc etc. You also don't say what version of PHP this is. As @Chris said, the functions you're using were removed in V7+ – Jonnix Feb 07 '18 at 23:50
  • Its version 5.5. The $x=0 is not the issue, ive tested to confirm this. The ajax is fetching data from the screen.php file in the same folder as the index.php, as you can see from the script in the header. Ive detailed what screen.php looks like in my question. – Dustin Feb 08 '18 at 00:12

0 Answers0