LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-26-2010, 09:34 PM   #1
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Rep: Reputation: 0
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>
 
Old 10-26-2010, 09:53 PM   #2
PehJota
Member
 
Registered: Aug 2010
Distribution: Desktop and netbook: Debian Squeeze; Router: DD-WRT
Posts: 43

Rep: Reputation: 5
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.

Last edited by Tinkster; 10-28-2010 at 02:04 PM. Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
 
Old 10-27-2010, 12:15 AM   #3
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
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>
 
Old 10-27-2010, 12:46 AM   #4
PehJota
Member
 
Registered: Aug 2010
Distribution: Desktop and netbook: Debian Squeeze; Router: DD-WRT
Posts: 43

Rep: Reputation: 5
Quote:
Originally Posted by eoc92866 View Post
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 View Post
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 View Post
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.

Last edited by Tinkster; 10-28-2010 at 02:05 PM. Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
 
Old 10-27-2010, 01:25 AM   #5
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

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

Last edited by Tinkster; 10-28-2010 at 02:06 PM. Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
 
Old 10-27-2010, 07:46 PM   #6
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
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>";

        }
?>

Last edited by Tinkster; 10-28-2010 at 02:06 PM. Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
 
Old 10-27-2010, 08:12 PM   #7
PehJota
Member
 
Registered: Aug 2010
Distribution: Desktop and netbook: Debian Squeeze; Router: DD-WRT
Posts: 43

Rep: Reputation: 5
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 View Post
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.

Last edited by Tinkster; 10-28-2010 at 02:07 PM. Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
 
Old 10-28-2010, 01:58 PM   #8
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
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

Last edited by Tinkster; 10-28-2010 at 02:08 PM. Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
 
Old 10-28-2010, 02:10 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by PehJota View Post
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
 
Old 10-28-2010, 02:12 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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

Last edited by Tinkster; 10-28-2010 at 02:13 PM. Reason: [i]edit[/i]
 
Old 10-28-2010, 02:18 PM   #11
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
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.
 
Old 10-28-2010, 02:18 PM   #12
PehJota
Member
 
Registered: Aug 2010
Distribution: Desktop and netbook: Debian Squeeze; Router: DD-WRT
Posts: 43

Rep: Reputation: 5
Quote:
Originally Posted by eoc92866 View Post
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?

Last edited by Tinkster; 10-28-2010 at 03:55 PM.
 
Old 10-28-2010, 02:35 PM   #13
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
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.
 
Old 10-28-2010, 02:46 PM   #14
PehJota
Member
 
Registered: Aug 2010
Distribution: Desktop and netbook: Debian Squeeze; Router: DD-WRT
Posts: 43

Rep: Reputation: 5
Quote:
Originally Posted by Tinkster View Post
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 View Post
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...

Last edited by PehJota; 10-28-2010 at 02:55 PM. Reason: I was reading output from my terminal emulator, which separated tabs into spaces.
 
Old 10-28-2010, 03:19 PM   #15
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
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?

Last edited by Tinkster; 10-28-2010 at 03:54 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash variable parsing! bashprog501 Programming 13 03-10-2011 09:34 PM
Parsing columns in bash seb357 Linux - Newbie 9 09-28-2009 03:40 PM
PHP not parsing snares Linux - Server 2 02-12-2009 12:17 AM
PHP Parsing ToothlessRebel Programming 20 10-25-2006 08:35 PM
php not parsing chens_83 Linux - General 9 02-19-2003 05:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration