Sponsored Content
Top Forums Shell Programming and Scripting Retrieve multiple rows from mysql and automatically create a table Post 302820805 by DGPickett on Thursday 13th of June 2013 01:09:53 PM
Old 06-13-2013
There are two obvious ways, either put the table HTML in the SQL string generation, or postprocess the SQL output with sed or the like. I usually do the latter:
Code:
echo '<TABLE>'
 
echo "SELECT 'dAtA' x, t.* from some_table t order by 1,2,3,4 ;" | mysql ... | sed '
  s/^| dAtA | /<TR><TD>/
  t good
  w logfile
  d
  :good
  s/  *|$//
  s/  *|  */<TD>/g
 '
 
echo '</TABLE>'

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

MySql: create table error

Hi, iam learning MySql. Iam trieing to create a table in the database "guestbook" at the command line in mysql heres what i type but i get a error mysql>create table guestbook ->( -> name varchar(40) null. -> url varchar(40) null. -> comments ... (3 Replies)
Discussion started by: perleo
3 Replies

2. Shell Programming and Scripting

How to create cron job automatically?

How do I write a perl script to get the cron jobs? I could do a perl -e ' system "crontab -l > jobs.txt " '; Is there a better way? Then I can use perl to make changes to jobs.txt. How can I submit the changes. I suppose I could use system "crontab jobs.txt", is there a better way? ... (0 Replies)
Discussion started by: siegfried
0 Replies

3. UNIX and Linux Applications

create table via stored procedure (passing the table name to it)

hi there, I am trying to create a stored procedure that i can pass the table name to and it will create a table with that name. but for some reason it creates with what i have defined as the variable name . In the case of the example below it creates a table called 'tname' for example ... (6 Replies)
Discussion started by: rethink
6 Replies

4. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

5. Linux

how to automatically create a file?

how can i automatically create a file on Linux? like a process that searches for the file and if the file does not exist, it automatically makes the file (3 Replies)
Discussion started by: roozis
3 Replies

6. Shell Programming and Scripting

connecting to table to extract multiple rows into file from unix script

I need to extract the data from oracle table and written the below code. But it is not working.There is some problem with the query and output is shown is No rows selected" . If I run the same query from sql developer there is my required output. And if I run the shell script with simple sql... (7 Replies)
Discussion started by: giridhar276
7 Replies

7. Programming

Getting Rows from a MySQL Table with max values?

I feel stupid for asking this because it seems that MYSQL code isn't working the way that I think it should work. Basically I wrote code like this: select * from `Test_DC_Trailer` HAVING max(DR_RefKey); Where the DR_RefKey is a unique numeric field that is auto iterated (like a primary key)... (7 Replies)
Discussion started by: Astrocloud
7 Replies

8. Shell Programming and Scripting

Filter rows from table

Hi , I need to filter input file according to following All rows with the following conditions should be removed 1) If in a row, the number of 'N's starting col 2 exceeds 2 (3 or more) OR 2) If a row is duplicated with the same value, starting col 2, A value 'N' is considered missing... (1 Reply)
Discussion started by: newbie83
1 Replies

9. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

10. Shell Programming and Scripting

Convert rows into columns and create table with awk

Hello I've four fields . They are First Name, Last Name, Age, Country. So when I run a Unix command, I get below output with these fields comes every time in different order as you can see. Some times first name is the first row and other time last name is first row in the output and etc etc..... (9 Replies)
Discussion started by: rprpr
9 Replies
DBX_QUERY(3)								 1							      DBX_QUERY(3)

dbx_query - Send a query and fetch all results (if any)

SYNOPSIS
mixed dbx_query (object $link_identifier, string $sql_statement, [int $flags]) DESCRIPTION
Sends a query and fetch all results. PARAMETERS
o $link_identifier - The DBX link object returned by dbx_connect(3) o $sql_statement - SQL statement. Data inside the query should be properly escaped. o $flags - The $flags parameter is used to control the amount of information that is returned. It may be any combination of the following constants with the bitwise OR operator (|). The DBX_COLNAMES_* flags override the dbx.colnames_case setting from php.ini. o DBX_RESULT_INDEX - It is always set, that is, the returned object has a data property which is a 2 dimensional array indexed numerically. For example, in the expression data[2][3] 2 stands for the row (or record) number and 3 stands for the column (or field) number. The first row and column are indexed at 0. If DBX_RESULT_ASSOC is also specified, the returning object contains the information related to DBX_RESULT_INFO too, even if it was not specified. o DBX_RESULT_INFO - It provides info about columns, such as field names and field types. o DBX_RESULT_ASSOC - It effects that the field values can be accessed with the respective column names used as keys to the returned object's data property. Associated results are actually references to the numerically indexed data, so modifying data[0][0] causes that data[0]['field_name_for_first_column'] is modified as well. o DBX_RESULT_UNBUFFERED - This flag will not create the data property, and the rows property will initially be 0. Use this flag for large datasets, and use dbx_fetch_row(3) to retrieve the results row by row. The dbx_fetch_row(3) function will return rows that are conformant to the flags set with this query. Incidentally, it will also update the rows each time it is called. o DBX_COLNAMES_UNCHANGED - The case of the returned column names will not be changed. o DBX_COLNAMES_UPPERCASE - The case of the returned column names will be changed to uppercase. o DBX_COLNAMES_LOWERCASE - The case of the returned column names will be changed to lowercase. Note that DBX_RESULT_INDEX is always used, regardless of the actual value of $flags parameter. This means that only the following combinations are effective: o DBX_RESULT_INDEX o DBX_RESULT_INDEX | DBX_RESULT_INFO o DBX_RESULT_INDEX | DBX_RESULT_INFO | DBX_RESULT_ASSOC - this is the default, if $flags is not specified. RETURN VALUES
dbx_query(3) returns an object or 1 on success, and 0 on failure. The result object is returned only if the query given in $sql_statement produces a result set (i.e. a SELECT query, even if the result set is empty). The returned object has four or five properties depending on $flags: o handle - It is a valid handle for the connected database, and as such it can be used in module specific functions (if required). <?php $result = dbx_query($link, "SELECT id FROM table"); mysql_field_len($result->handle, 0); ?> o cols and rows - These contain the number of columns (or fields) and rows (or records) respectively. <?php $result = dbx_query($link, 'SELECT id FROM table'); echo $result->rows; // number of records echo $result->cols; // number of fields ?> o info (optional) - It is returned only if either DBX_RESULT_INFO or DBX_RESULT_ASSOC is specified in the $flags parameter. It is a 2 dimensional array, that has two named rows ( name and type) to retrieve column information. Example #1 lists each field's name and type <?php $result = dbx_query($link, 'SELECT id FROM table', DBX_RESULT_INDEX | DBX_RESULT_INFO); for ($i = 0; $i < $result->cols; $i++ ) { echo $result->info['name'][$i] . " "; echo $result->info['type'][$i] . " "; } ?> o data - This property contains the actual resulting data, possibly associated with column names as well depending on $flags. If DBX_RESULT_ASSOC is set, it is possible to use $result->data[2]["field_name"]. Example #2 outputs the content of data property into HTML table <?php $result = dbx_query($link, 'SELECT id, parentid, description FROM table'); echo "<table> "; foreach ($result->data as $row) { echo "<tr> "; foreach ($row as $field) { echo "<td>$field</td>"; } echo "</tr> "; } echo "</table> "; ?> Example #3 How to handle UNBUFFERED queries <?php $result = dbx_query ($link, 'SELECT id, parentid, description FROM table', DBX_RESULT_UNBUFFERED); echo "<table> "; while ($row = dbx_fetch_row($result)) { echo "<tr> "; foreach ($row as $field) { echo "<td>$field</td>"; } echo "</tr> "; } echo "</table> "; ?> CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.0.0 | | | | | | | Introduced DBX_RESULT_UNBUFFERED. | | | | | 4.3.0 | | | | | | | Introduced DBX_COLNAMES_UNCHANGED, DBX_COL- | | | NAMES_UPPERCASE, and DBX_COLNAMES_LOWERCASE. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #4 How to handle the returned value <?php $link = dbx_connect(DBX_ODBC, "", "db", "username", "password") or die("Could not connect"); $result = dbx_query($link, 'SELECT id, parentid, description FROM table'); if (is_object($result) ) { // ... do some stuff here, see detailed examples below ... // first, print out field names and types // then, draw a table filled with the returned field values } else { exit("Query failed"); } dbx_close($link); ?> NOTES
Note Always refer to the module-specific documentation as well. Column names for queries on an Oracle database are returned in lowercase. SEE ALSO
dbx_escape_string(3), dbx_fetch_row(3), dbx_connect(3). PHP Documentation Group DBX_QUERY(3)
All times are GMT -4. The time now is 05:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy