So, I have a website that hits up a very simple PHP script to display data from a database in an HTML table.
My question is simple; is it possible to write the PHP script in such a way that I can use the same script to get the same data and return it in JSON for the android app that will need it (without a single if(onMobileApp) type god statment which is basically equivalent to writing two scripts anyways)?
Or do I have to have two scripts, like
get_songs_web.php
get_songs_android.php
It should be noted that speed of development is crucial here; I'm learning this skill for a 24h competition, so I'm writing procedural PHP, not OO.
As an example of the type of scripts I have, so that you can use it as an example in your responses, here is one of the ones used in the webpage:
<?php
    // Connect to database
    $db_connection = mysqli_connect('localhost', 'root', '', 'songs_list_data')
    or die('Error connecting to MySQL server!');
    // Get data from HTML form
    $name = $_POST["song"];
    $artist = $_POST["artist"];
    // Add song to database
    $add_song_query = $db_connection->prepare("INSERT INTO songs"
            . "(artist, name)"
            . "VALUES (?, ?)");
    $add_song_query->bind_param("ss", $artist, $name);
    $add_song_query->execute();
    $add_song_query->close();
?>
<html>
    <table id="results">
        <?php
            $results_query = $db_connection->prepare("SELECT *"
                    . "FROM songs");
            mysqli_stmt_execute($results_query);
            $result = mysqli_stmt_get_result($results_query);
            $row_num = 1;
            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
            {
                echo '<tr class="songs_table_row">';
                    echo '<td class="row_num">' . $row_num . '</td>';
                    echo '<td class="artst">' . $row['artist'] . '</td>';
                    echo '<td class="name">' . $row['name'] . '</td>';
                echo '</tr>';
                $row_num++;
            }
            mysqli_stmt_close($results_query);
        ?>
    </table>
</html>
<?php
    // Close database connection
    mysqli_close($db_connection);
?>
 
     
     
     
     
    
Remove the mysqli_close it's useless (it's auto closed at the end of the script).
– neoteknic Mar 29 '17 at 15:38