Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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 01:04 PM.
Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
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.
Distribution: Desktop and netbook: Debian Squeeze; Router: DD-WRT
Posts: 43
Rep:
Quote:
Originally Posted by eoc92866
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
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
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 01:05 PM.
Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
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 01:06 PM.
Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
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.
Last edited by Tinkster; 10-28-2010 at 01:06 PM.
Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
Distribution: Desktop and netbook: Debian Squeeze; Router: DD-WRT
Posts: 43
Rep:
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
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 01:07 PM.
Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
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 01:08 PM.
Reason: replaced PHP with CODE tags so people don't have to scroll sideways for hours to read the posts...
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 ...
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 01:13 PM.
Reason: [i]edit[/i]
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.
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?
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.
Distribution: Desktop and netbook: Debian Squeeze; Router: DD-WRT
Posts: 43
Rep:
Quote:
Originally Posted by Tinkster
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
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 01:55 PM.
Reason: I was reading output from my terminal emulator, which separated tabs into spaces.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.