Php script read from $1

Hello,
I am trying to inject data extracted via php-json to sql database but unable to run it in loop mode. It is working just for one data, then stops.
If it's necessary, I can share the complete php file.

test.php

<?php
include_once 'imdb.class.php';
//mysql config.
$servername = "127.0.0.1";
$username   = "root";
$password   = "mypasswd";
$dbname     = "mariadb";
$dbport     = "3306";

//open mysql connection.
$conn = new mysqli($servername, $username, $password, $dbname, $dbport);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//read video database to array.
$sql = mysqli_query($conn, "select * from video ORDER BY name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
    $video[] = $row;
}

############## here we go ! ######################## 
        $aTests = [
                    'Top Gun',
                    'Chicago',
                ];
###########################################

$i = 1 ;
foreach ($aTests as $sMovie) { #--------!!!
   $i++;
   $IMDB = new IMDB($sMovie);
if ($IMDB->isReady) {
$title = $IMDB->getTitle($bForceLocal = true);
$description = mysqli_real_escape_string($conn, $IMDB->getDescription());
$plot = mysqli_real_escape_string($conn, $IMDB->getPlot($iLimit = 0));
..
..many other queries below...
..
..

$sql = "INSERT INTO video
(name, 2nd_name, 3rd_name, time, protocol, category_id, cat_genre_id_1,.......)
VALUES ('$title $year', '$title $year', '0', '$runtime', '', '3090', '559', '1', '1', ....)
if ($conn->query($sql) !== TRUE) {
    echo '<p style="text-align: center;">Error: ' . $sql . '<br>' . $conn->error . '</p>';
    die;

My question:
Instead of entering data into php file, can I run it like below way?

while read -r line; do
php test.php $line
done < words_from_file

Thank you
Boris

--- Post updated at 08:35 AM ---

Solved now.
Replaced foreach line as shown below :
foreach($argv as $sMovie) {

Thank you
Boris

1 Like

FYI,

Normally when a PHP programmer reads a JSON object from a remote API, they convert the JSON object to a PHP array using json_decode() .

Since you are fetching an array of JSON objects, better (without seeing the entire code or knowing all the details) is to convert each JSON object in the fetched array to a PHP array.

This will make your code much cleaner.

Anyway, glad you have it working.

Consider using json_decode() next time, when you process a JSON object in a PHP script.

EDIT: Fixed error in the name of the PHP function.

For completeness, pursuant to my post above, here is the PHP docs for json_decode()

PHP: json_decode - Manual

There are two PHP functions very important to processing JSON objects:

json_encode() and json_decode()

For example, you can do a mysql query on a DB and use json_encode() on the mysql array returned from the query and echo the resulting JSON to create a simple REST API.

1 Like

Thank You Neo,
I am pretty new on api. It will take much time for a 60years old man to comprehend json.
I am just a user, nothing more but I appreciate your support.

Thank you
Boris

1 Like

Working with JSON objects is actually very simple when you get the hang of it and they make API-life much easier.

On the client side, there are many great ways to easily process JSON with Javascript.

1 Like