0

I'm trying to get a php script to run multiple times to return several items from a mysql server to the main page. Here is the code:

function getServerData(){
var serverData = <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT videoId FROM videoList LIMIT 1;";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo json_encode($row["videoId"]);
        $deleteCommand = "DELETE FROM videoList WHERE videoId = '".$row["videoId"]."';";
        $conn->query($deleteCommand);
        $result = "";
    }
} else {
    echo "'No results'";
}

$conn->close();
?>;
console.log(serverData);
return serverData;
}

Is it possible to have this code be used multiple times, or would I have to look for another route?

Elden
  • 11
  • 3
  • 2
    You'll need to learn the difference between [server-side and client-side scripting](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). What you're looking for is likely Ajax (if you want a dynamic no-reloading structure), but you can likely achieve it via PHP as well. – Qirel Jun 21 '16 at 21:23
  • Why not just change `LIMIT 1` to `LIMIT 5` or however many you want? – Patrick Q Jun 21 '16 at 21:25
  • Looked at ajax, and unless I'm mistaken, the only function that gets data from it is the XMLHttpRequest. However, that also has the problem of locking the data inside of a container, and I was unable to find a way to get it out. – Elden Jun 21 '16 at 21:26
  • @Patrick Q The other half of the site lets the user input a video id at anytime. What I wanted to happen was for the script to get one item at a time, then when another item is added to the stack it would get that item. I have no prior knowledge of how many video ids might be in the stack. – Elden Jun 21 '16 at 21:31

0 Answers0