For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > January 2007 > Formatting MySQL Queries









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Formatting MySQL Queries
Brent Anderson

2007-01-24, 6:58 pm

Hello.

My team and I are looking for a way to return an entire table of
MySQL data with columns delimited by tabs and rows delimited by
carriage returns. How exactly would this work - I'm not a PHP expert
and, although I know MySQL, I don't know where to begin with
formatting a query pointer into an actual pile of text. We have an
engine ready to take tables in this format for processing, we just
need to parse the query data into a text result.

Thanks,
Brent Anderson
Jeremy Glover

2007-01-24, 6:58 pm

<?php
// Make sure you're already connected to the database...
=09
function return_mysql_table($table_name, $id_column)
{
$sql =3D "SELECT * FROM " . $table_name . " ORDER BY " .
$id_column . " ASC";
$result =3D mysql_query($sql) or die("Error: Invalid SQL:
" . $sql);
=09
$rows =3D array();
=09
while($current_row =3D mysql_fetch_array($result))
{
$rows[] =3D implode("\t", $current_row);
}
=09
$table =3D implode("\r\n", $rows);
=09
return $table;
}
?>=20

-----Original Message-----
From: Brent Anderson [mailto:brentj84062@gmail.com]=20
Sent: Wednesday, January 24, 2007 4:21 PM
To: php-db@lists.php.net
Subject: Formatting MySQL Queries

Hello.

My team and I are looking for a way to return an entire table of MySQL
data with columns delimited by tabs and rows delimited by carriage
returns. How exactly would this work - I'm not a PHP expert and,
although I know MySQL, I don't know where to begin with formatting a
query pointer into an actual pile of text. We have an engine ready to
take tables in this format for processing, we just need to parse the
query data into a text result.

Thanks,
Brent Anderson

~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jeremy Glover
Interactive Communication Developer, Trone
336.819.6934
jglover@trone.com


Confidentiality Notice: This e-mail communication and any attachments =
may contain confidential and privileged information for the use of the =
designated recipients named above. If you are not the intended =
recipient, you are hereby notified that you have received this =
communication in error and that any review, disclosure, dissemination, =
distribution or copying of it or its contents is prohibited. If you =
have received this communication in error, please notify me immediately =
by replying to this message and deleting it from your computer. Thank =
you.
Brent Anderson

2007-01-24, 6:58 pm

Hello.

Just as I posted this, I found a MySQL example on php.net and was
able to modify it to fit my needs. Here is the script:

<?php
// Connecting, selecting database
$link = mysql_connect('server', 'username', 'password')
or die('Could not connect: ' . mysql_error());

mysql_select_db('database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in Tab/Return delimited format

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
// echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "$col_value\t";
}
echo "\n";
}

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

Thanks to Steven Cruz and Jeremy Glover for their quick replies, but
this will be perfect.

Thanks,
Brent Anderson

On Jan 24, 2007, at 2:20 PM, Brent Anderson wrote:

> Hello.
>
> My team and I are looking for a way to return an entire table of
> MySQL data with columns delimited by tabs and rows delimited by
> carriage returns. How exactly would this work - I'm not a PHP
> expert and, although I know MySQL, I don't know where to begin with
> formatting a query pointer into an actual pile of text. We have an
> engine ready to take tables in this format for processing, we just
> need to parse the query data into a text result.
>
> Thanks,
> Brent Anderson

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com