Php script read from $1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Php script read from $1
# 1  
Old 04-04-2019
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
Code:
<?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?
Code:
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
This User Gave Thanks to baris35 For This Post:
# 2  
Old 04-04-2019
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.
# 3  
Old 04-04-2019
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.
This User Gave Thanks to Neo For This Post:
# 4  
Old 04-04-2019
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
This User Gave Thanks to baris35 For This Post:
# 5  
Old 04-04-2019
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.
This User Gave Thanks to Neo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Smarter way to read $1 $2 in php

Hello, I am running under ubuntu14.04 with php5. When I run below php, it creates a token, then adds axaxax and bxbxbx into pc database, and at last, kills created token. What I am trying to do is to add userid and password read from a file. I do not wish to enter username and password manually.... (3 Replies)
Discussion started by: baris35
3 Replies

2. Shell Programming and Scripting

How to tell php to read shell?

.... Solved.... .. .. Hello, I've read couples of similar threads to my question and I strongly believe that I am doing something wrong. What I'm trying to do is to process data with php. It reads data from shell script. Everything goes well but at the end it does not print what it reads... (0 Replies)
Discussion started by: baris35
0 Replies

3. UNIX for Advanced & Expert Users

Read space in script

Hi All, Please let me know how to read space between 2 words in script. It is reading only first chars ES,NZ. But after space it is not reading next 3 chars. ES LPI NZ GBL FR LPI DK LPI for v_sob_cntry_cd in `cat /shared/set_of_books_cntry_cd_list.lst` do ... (6 Replies)
Discussion started by: kiranparsha
6 Replies

4. Shell Programming and Scripting

Script to read a log file and run 2nd script if the dates match

# cat /tmp/checkdate.log SQL*Plus: Release 11.2.0.1.0 Production on Mon Sep 17 22:49:00 2012 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production FIRST_TIME NEXT_TIME... (1 Reply)
Discussion started by: SarwalR
1 Replies

5. Shell Programming and Scripting

Read from script

Hi all. I'm a newbie of bash scripting. I need to create a script to have a rotate backup with 5 USB DISK on a work week. With e2label, I've labelled all disk with all day of the week ( e.g. Monday, Tuesday, Thursday,... ). How can I read if I've plugged the right device ?!?! I find... (3 Replies)
Discussion started by: filloweb
3 Replies

6. UNIX for Advanced & Expert Users

Script without read permission but execute the script

I have a script, except me no one can read the script but they can execute the script. Is it possible? (14 Replies)
Discussion started by: kingganesh04
14 Replies

7. UNIX for Dummies Questions & Answers

help with simple read script!

Hi y'all...I've been wracking my brain over this very simple script that reads in a textfile and echos each line. But I keep getting an error. Below is my script: #!/bin/bash while read myline do echo $myline done < t_file-20080221.01.asc The error I am getting is: syntax error... (1 Reply)
Discussion started by: kevlar28
1 Replies

8. Shell Programming and Scripting

Script does not stop when doing a read

Hi Folks, I have been trying to create a script wherein after it reads a certain number of data, it will pause and ask the user if he wants to continue or not. However, it seems that when it is supposed to read the user's answer, the script will go into a loop. What is wrong with my script here?... (7 Replies)
Discussion started by: rooseter
7 Replies

9. Shell Programming and Scripting

how other user cannot read my script?

Hi All, If I don't want other user read my script, what can i do? :confused: (19 Replies)
Discussion started by: happyv
19 Replies

10. Shell Programming and Scripting

SH script problem with read

Hi, I'm doing a script that reads lines from a file and then copy them to another file, if the user wants it. But I'having problems to get the user selection because when I do the read to ask the user, the script reads the next line of the file. The script looks like this: #!/bin/sh # ... (2 Replies)
Discussion started by: pmpx
2 Replies
Login or Register to Ask a Question