LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash and php parsing (https://www.linuxquestions.org/questions/linux-newbie-8/bash-and-php-parsing-840644/)

eoc92866 10-26-2010 08:34 PM

bash and php parsing
 
i am trying to get a script that i'm calling to have information from a sql populate into rows... but i'm not getting the data to output correctly into the rows. can someone please help?

<table>
<thead>
<tr>
<th>Name</th>
<th>Help</th>
<th>Folder</th>
<th>Department</th>
</tr>
</thead>
<?php
while ($result = shell_exec("/bin/bash someScript.sh"))
$row = $result
{
echo "<tr>";
echo "<td>".$row['name']."</td>";
if ($row['help']==0)
{
echo "<td><img src='images/Red.gif'></td>";
}
else
{
echo "<td><img src='images/Green.gif'></td>";
}

echo "<td>".$row['folder']."</td>";
echo "<td>".$row['department']."</td>";

echo "</tr>";
}
?>

</table>

PehJota 10-26-2010 08:53 PM

First... code tags (or in this case, php tags), which provide proper code formatting and indentation, would help us to read your code more easily.

Second, '$row = $result' should throw a syntax error as there's no semicolon delimiting the end of the statement.

Third,
Code:

while ($result = shell_exec("/bin/bash someScript.sh"))
$row = $result

is a loop with a single body statement. Everything after it, i.e.
Code:

{
echo "<tr>";
echo "<td>".$row['name']."</td>";
if ($row['help']==0)
{
echo "<td><img src='images/Red.gif'></td>";
}
else
{
echo "<td><img src='images/Green.gif'></td>";
}

echo "<td>".$row['folder']."</td>";
echo "<td>".$row['department']."</td>";

echo "</tr>";
}

is just a bunch of code that gets executed once. I assume you want '$row = $result;' inside of that code block? Or better yet, why do you have different variables '$row' and '$result' if they are to always have the same value?

Fourth, shell_exec() returns a string, not an array. It returns the output (stdout) printed by the called command. So if your shell script (which you haven't posted) outputs some data from a database, you'll have to parse that in PHP.

And fifth, I think this should probably go in the programming forum, since it's more related to PHP programming than newbie Linux. But oh well.

At any rate, try fixing those errors. And you should always have 'error_reporting(E_ALL);' at the top of your code, as it makes debugging much easier. PHP hides notices and such by default, so if you have some little error such as misusing data types, PHP won't tell you unless you set reporting to E_ALL.

eoc92866 10-26-2010 11:15 PM

hi,

i made some of the adjustments that you asked. i'm not quite sure on the array that you're referring to, my hope with the shell script is to have the sql information populate result and then parse out the sql data. then, create new rows as there are multiple lines of information that i'd like to separate into columns. i'm just not sure entirely how to get there. :)

<?php

'error_reporting(E_ALL);'

?>



<table>
<thead>
<tr>
<th>Name</th>
<th>Help</th>
<th>Folder</th>
<th>Department</th>
</tr>
</thead>

<?php
while ($result = shell_exec("/bin/bash someScript.sh"))
{
'$row = $result;'
echo "<tr>";
echo "<td>".$row['name']."</td>";
if ($row['help']==0)
{
echo "<td><img src='images/Red.gif'></td>";
}
else
{
echo "<td><img src='images/Green.gif'></td>";
}

echo "<td>".$row['folder']."</td>";
echo "<td>".$row['department']."</td>";

echo "</tr>";
}
?>

</table>

PehJota 10-26-2010 11:46 PM

Quote:

Originally Posted by eoc92866 (Post 4140451)
Code:

'error_reporting(E_ALL);'
Code:

        '$row = $result;'

Don't include the quotes in your code... I used quotes to separate the code from the rest of the sentence.

Quote:

Originally Posted by eoc92866 (Post 4140451)
i'm not quite sure on the array that you're referring to, my hope with the shell script is to have the sql information populate result and then parse out the sql data.

I'm referring to this:
Quote:

Originally Posted by eoc92866 (Post 4140451)
Code:

$row['name']

You're trying to access an array index 'name' in the variable '$row'. The problem is that '$row' is a string, not an array (see the documentation for shell_exec()). And I hope you realize that putting shell_exec() in a while loop calls the script multiple times.

And please put your code in [CODE] [/CODE] BB code tags. You can easily do this using the PHP file icon on the editor toolbar.

eoc92866 10-27-2010 12:25 AM

Code:

        $row = $result['name', 'help', 'folder', 'department'];
is there a better command than shell_exec() to call shell script and get the sql categories?

eoc92866 10-27-2010 06:46 PM

hi i remade the data... so i change the parameters around and i'm trying to place the array data into while statement that will be looped to recreate more lines as information is populated.

unfortunately i'm not getting this to work.


Code:

<?php

        $result = shell_exec("/bin/bash someScript.sh");

                $lines = explode ("\n", $result);

                $assoc_array = array();

                for ($i=1; $i<=count($lines); $i++)
                {
                $parts = explode ("\t", $lines[$i]);
                        $assoc_parts = array("help" => $parts [0], "name" => $parts [1], "folder" => $parts [2], "department" => $parts [3]);
                $assoc_array[]=$assoc_parts;

                }


?>

<br/>


        <table>
                <thead>

                        <tr>
                                <th>Help</th>
                                <th>Name</th>
                                <th>Folder</th>
                                <th>Department</th>
                        </tr>
                </thead>

<?php

      while($row = $assoc_array)
        {
echo                      "<tr>";
echo                            "<td>".$row['help']."</td>";
                                        if ($row['help']==0)
                                        {
echo                                    "<td><img src='/images/Red.gif'></td>";

                                        }
                                        else
                                        {
echo                                    "<td><img src='/images/Green.gif'></td>";
                                        }

echo                            "<td>".$row['name']."</td>";
echo                            "<td>".$row['folder']."</td>";
echo                            "<td>".$row['department']."</td>";

echo                      "</tr>";

        }
?>


PehJota 10-27-2010 07:12 PM

The first part of the code looks much better; it looks like it should run fine now. Your problem is in your output loop.

Quote:

Originally Posted by eoc92866 (Post 4141554)
Code:

      while($row = $assoc_array)

This will loop as long as '$assoc_array' has a non-false value (which it always will, unless there are no rows returned by the shell script). Instead, you want to iterate over the array. The easiest way to do this would be with an index ('$i' for example); your while loop might then look like this:

Code:

      $i = -1;  // -1 because ++$i on the first while condition check will make this 0.
      while($row = $assoc_array[++$i])

And as a performance enhancement, you can even prevent elements of '$assoc_array' from being copied by assigning by reference:

Code:

      $i = -1;  // -1 because ++$i on the first while condition check will make this 0.
      while($row = &$assoc_array[++$i])

Of course, this isn't very robust; if your script returns an empty line, the loop will break (not to mention, your '$assoc_parts = array(...' line will throw undefined offset notices if you have error reporting set to E_ALL). Perhaps then checking '$i' against 'count($assoc_array)' would be better (you'll probably want to use a for loop in that case); I'll leave improving on that up to you.

eoc92866 10-28-2010 12:58 PM

i now can make the lines repeat to the number of files required...

Code:

        for ($i=1; $i<=count($assoc_array); $i++)
but the actual files that i want to print out are not printing into the rows

Code:

$result = shell_exec("/bin/bash someScript.sh");
someScript is another bash command that selects data from sql

Code:

mysql -u username -ppassword -h x.x.x.x -b -B -e "SELECT name, help, folder, department FROM tableName" database

Tinkster 10-28-2010 01:10 PM

Quote:

Originally Posted by PehJota (Post 4140393)
First... code tags (or in this case, php tags), which provide proper code formatting and indentation, would help us to read your code more easily.


I'd like to advise against the use of PHP tags; the colour
coding of the source is of less benefit IMNSHO than the
need to scroll sideways forever to read loooong lines of
text ...



Cheers,
Tink

Tinkster 10-28-2010 01:12 PM

Out of curiosity ... which RDBMS are you using? I find the concept
of using a shell-script to retrieve SQL results from PHP rather
bewildering, to say the least.


Ooops ... just noticed in the last post: MySQL.

Why don't you just use PHPs MySQL classes to interrogate
the database? Makes MUCH more sense.



Cheers,
Tink

eoc92866 10-28-2010 01:18 PM

most of the previous work has been written in bash and i'd like to transition the data from writing to text utilizing bash to working with sql+php and bash to execute the major work going on.

PehJota 10-28-2010 01:18 PM

Quote:

Originally Posted by eoc92866 (Post 4142473)
but the actual files that i want to print out are not printing into the rows

Code:

$result = shell_exec("/bin/bash someScript.sh");
someScript is another bash command that selects data from sql

Code:

mysql -u username -ppassword -h x.x.x.x -b -B -e "SELECT name, help, folder, department FROM tableName" database

Passing your MySQL password on the command line is a big security risk, as explained briefly in the mysql(1) man page and more thoroughly in section 5.3.2.2, "End-User Guidelines for Password Security", of the documentation.

That aside, yes, by default, the mysql command-line client provides output in a tabular format (for readability in an interactive context). You may want to look at these two options (from the man page):
Code:

      ·  --batch, -B

          Print results using tab as the column separator, with each row on a new line. With this option, mysql does not use
          the history file.

          Batch mode results in nontabular output format and escaping of special characters. Escaping may be disabled by using
          raw mode; see the description for the --raw option.

...

      ·  --raw, -r

          For tabular output, the “boxing” around columns enables one column value to be distinguished from another. For
          nontabular output (such as is produced in batch mode or when the --batch or --silent option is given), special
          characters are escaped in the output so they can be identified easily. Newline, tab, NUL, and backslash are written
          as \n, \t, \0, and \\. The --raw option disables this character escaping.

          The following example demonstrates tabular versus nontabular output and the use of raw mode to disable escaping:

              % mysql
              mysql> SELECT CHAR(92);
              +----------+
              | CHAR(92) |
              +----------+
              | \        |
              +----------+
              % mysql -s
              mysql> SELECT CHAR(92);
              CHAR(92)
              \\
              % mysql -s -r
              mysql> SELECT CHAR(92);
              CHAR(92)
              \

But then again, why are you using a shell script and an external client to perform MySQL queries? Why aren't you just using the PHP MySQL extension?
EDIT: Ah, well you sneaked in a post while I was typing. ;) Bash isn't doing the work in that mysql command. It's the mysql client (written in C) that does the work. Is this really better than letting the PHP MySQL client (also written in C) do the work for you?

EDIT 2: Oh wow, I just noticed that you are using the batch option. Sorry about that. Have you tried simply running the Bash script without PHP to make sure it's producing the output you expect?

eoc92866 10-28-2010 01:35 PM

yes, i ran the sql command in terminal and get an ouput that's not tab delineated of the information that i need.

i'm hoping to address the security concern afterwards... may not be the best idea, but it's something that i have to think about moving forwards and will probably be another discussion. :)

PehJota 10-28-2010 01:46 PM

Quote:

Originally Posted by Tinkster (Post 4142481)
I'd like to advise against the use of PHP tags; the colour
coding of the source is of less benefit IMNSHO than the
need to scroll sideways forever to read loooong lines of
text ...

Well hopefully people write code that doesn't run horizontally forever... ;) I personally find that well formatted code with indentation (not so much color coding) is far easier to read than the browser rendering of regular formatted HTML (which culls whitespace in text).

Quote:

Originally Posted by eoc92866 (Post 4142500)
yes, i ran the sql command in terminal and get an ouput that's not tab delineated of the information that i need.

i'm hoping to address the security concern afterwards... may not be the best idea, but it's something that i have to think about moving forwards and will probably be another discussion. :)

Well I just ran mysql with the batch option, and I see that it lists field names on the first line, so just skip the first line to solve that. Otherwise, it does use tab characters between field values, so if you PHP script is implemented correctly, it should be able to handle that...

eoc92866 10-28-2010 02:19 PM

i tried:

Code:

SELECT fields FROM table LIMIT 0, 38
and still get the name, help, folder, department fields... is there another command? or am i utilizing the syntax incorrectly?


All times are GMT -5. The time now is 02:13 PM.