Unable to use sql DB in nightbot customapi

So im writing an quotscript for nightbot and im realizing that with an MySql database…

ive written all my code but now nigtbot only prints: Quote

my command is !randomquote
the response code is: Quote $(customapi http://myurl.com/quotes.php) !
the expected result: Quote #[QuoteId]: [Quote] !
the real result: Quote
(im not using the real url due to the fact that the url should be private)

so does anyone know a solution ?

Have you checked the output of your API? It should be a single line, less than 400 characters response that is formatted in plain text (not HTML). Any other formatting will not show up properly.

In this picture you can see that the <body> segment only holds the text !:

the <iframe> and the stuff in the header is generated by some of my browser addons!

heres a code snipped where i print into the EMPTY document:

if ($result->num_rows > 0) { $output = $result->fetch_assoc(); if (!empty($output)) { echo "#[" . $output["id"] . "]: ". $output["quote"]; } else { echo "outp.empty"; } }

Well considering the rendered page is HTML and not plain text, that would be why it’s not being printed by Nightbot. Like already stated, it must return a single line in plain text, not HTML.

as is said, those html lines are autogenerated by firefox (my browser) if you visit an plaintext site…
the site does only contain the #[1]: IST… DAS… ALLES ? part !

an prove:

while this:

is not loaded by nightbot, (EDIT: the text in the <body> tag)
this is loaded just fine:

my toughts are that may the respond time of the “quotes.php” site is to long (like ~10 to ~20ms)
wile the “t.php” site can respond imediatly !

Well unless you can link to the specific API that is not working so it can be examined, there’s nothing we can do for you. The only URL I found for your Twitch channel was generating PHP warnings.

SO here is the content of the quotes.php that i call!

    <?php
$servername = "REDACTED";
$username = "REDACTED";
$password = "REDACTED";
$dbName = "REDACTED";
$indexId = "";
$newQuote = "";
$actionId = 0;
if ($_GET["index"] != null) {
    $indexId = $_GET["index"];
    $actionId = 1;
}
else if ($_GET["add"] != null) {
    $actionId = 2;
    $newQuote = $_GET["add"];
}
else {
    $actionId = 0;
}
$conn = new mysqli($servername, $username, $password, $dbName);
if ($conn->connect_error) {
    die("Connection Failed: " . $conn->connect_error);
}
if ($actionId == 0) {
    $sqlQuery =  "SELECT id FROM quotes";
    $result = $conn->query($sqlQuery);
    $max = $result->num_rows;
    if ($max > 0) {
        $rand = mt_rand(1, $max);
        $sqlQuery = "SELECT * FROM quotes WHERE id =" . $rand;
        $result = $conn->query($sqlQuery);
        if ($result->num_rows > 0) {
            $output = $result->fetch_assoc();
            if (!empty($output)) {
                echo "#[" . $output["id"] . "]: ". $output["quote"];
            }
            else {
                echo "outp.empty";
            }
        }
        else {
            echo "ERROR = result--" . $result->num_rows;
        }
    }
    else {
        echo "ERROR = max--" . $max;
    }

}
if ($actionId == 1) {
 //TODO ADD EXPLICIT QUOTE RETRIVAL !
}
if ($actionId == 2) {
    //TODO ADD QUOTE ADDING
}
$conn->close();

eventually it helps a bit.
at the moment i call the quotes.php file without any arguments !
EDIT: the url i use is: http://twitchstats.890m.com/quotes.php

So from your code, you’re getting stuck with $actionId = 1; or $actionId = 2; because you’re not properly parsing input querystring parameters. Checking for null is not enough. You can add echos inside the first two if-statements to test which it is stuck in, as well as the value of the $_GET parameter to see why your if-statement is failing.

Thank you, that did the trick !
ive now gone to that that i changed the if vars to this:

$actionId = -1;
if ($_GET["actionId"] == 0) {
    $actionId = 0;
}
if ($_GET["actionId"] == 1) {
    $actionId = 1;
    $indexId = $_GET["index"];
}
else if ($_GET["actionId"] == 2) {
    $actionId = 2;
    $newQuote = $_GET["add"];
}
else {
    echo 'NO ACTION DEFINED !';
}

wich now properly loads the desired text !
Thank you very much !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.