php/mysql Query Question.

php/mysql Query Question.

am 15.09.2009 20:34:12 von Administrator

Before most of you go on a rampage of how to please read below...

As most of you already know when using MySQL from the shell you can write y=
our queries in html format in an out file.

Example: shell>mysql -uyourmom -plovesme --html
This now will return all results in an html format from all queries.

Now I could â€=9Cteeâ€=9D this to a file and save the results retur=
ned if I so choose to save the result of the display .

Letâ€=99s say I want to be lazy and write a php MySQL query to do the s=
ame so that any result I queried for would return the html results in a tab=
le without actually writing the table tags in the results.

Is there a mysql_connect or select_db or mysql_query tag option to do that =
since mysql can display it from the shell?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: php/mysql Query Question.

am 15.09.2009 20:41:23 von Robert Cummings

admin@buskirkgraphics.com wrote:
> Before most of you go on a rampage of how to please read below...
>
> As most of you already know when using MySQL from the shell you can write your queries in html format in an out file.
>
> Example: shell>mysql -uyourmom -plovesme --html
> This now will return all results in an html format from all queries.
>
> Now I could “tee” this to a file and save the results returned if I so choose to save the result of the display .
>
> Let’s say I want to be lazy and write a php MySQL query to do the same so that any result I queried for would return the html results in a table without actually writing the table tags in the results.
>
> Is there a mysql_connect or select_db or mysql_query tag option to do that since mysql can display it from the shell?

echo "Select * from my_table" | mysql --html -ufoo -pfee database_name

However, this allows for your database password to be visible in the
process list for a brief moment of time. You might be better served by
finer grained process control where you can check the output and provide
input as needed. Or a simple expect script might suffice.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: php/mysql Query Question.

am 16.09.2009 03:17:24 von Robert Cummings

admin@buskirkgraphics.com wrote:
> Would you mind giving me an example of this that i can stick right into a blank php file and run.
>
> I get what you are saying but i cant seem to make that even echo out the data. php 5.2 mysql 5.1.3 Apache 2.2


$db = 'db';
$host = 'host';
$user = 'user';
$pass = 'pass';

$query = "select * from my_table";

$db = escapeShellArg( $db );
$host = escapeShellArg( $host );
$user = escapeShellArg( $user );
$pass = escapeShellArg( $pass );

$query = escapeShellArg( $query );

$command = "echo $query | mysql --html -h$host -u$user -p$pass $db";

echo 'Command: '.$command."\n";
$html = `$command`;
echo $html."\n";

?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: php/mysql Query Question.

am 16.09.2009 10:05:41 von Peter Ford

admin@buskirkgraphics.com wrote:
> Before most of you go on a rampage of how to please read below...
>
> As most of you already know when using MySQL from the shell you can write your queries in html format in an out file.
>
> Example: shell>mysql -uyourmom -plovesme --html
> This now will return all results in an html format from all queries.
>
> Now I could “tee” this to a file and save the results returned if I so choose to save the result of the display .
>
> Let’s say I want to be lazy and write a php MySQL query to do the same so that any result I queried for would return the html results in a table without actually writing the table tags in the results.
>
> Is there a mysql_connect or select_db or mysql_query tag option to do that since mysql can display it from the shell?

I think you'll find that the HTML output is a function of the mysql command line
program (I tend to use PostgreSQL, where 'psql' is a similar program) so you can
only access that functionality by calling the command line.

I suspect that, since PHP is a HTML processing language (originally), the
creators of the mysql_ functions figured that the user could sort out making
HTML from the data returned...

It's should be a simple operation to write a wrapper function to put HTML around
the results. There might even be a PEAR extension or PHPClasses class to do it
(I haven't looked yet)

--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: php/mysql Query Question.

am 16.09.2009 16:00:14 von Robert Cummings

admin@buskirkgraphics.com wrote:
> I tend to do this robert,
> while looking at your example i thought to myself since i am trying to mimick a shell command why not run one.
>
> Result:
> > $db = 'db';
> $host = 'host';
> $user = 'user';
> $pass = 'pass';
> $query = "select * from $db.my_table";
> $ddvery = shell_exec("mysql -u$user -p$pass --html --execute=$query");
> echo "

$ddvery
";
> ?>
>
> Not are the results safe but the unlimited possibilites are amazing. Thanks so much for the kick starter

This presumes your information is all safe and that there are no special
shell characters in any of the configuration settings (now and in the
future). Also, the shell_exec() function is "identical" to the backtick
operator that I used in my example (see the help). You've essentially
done what I did, but made it less robust... except for the use of the
--execute parameter which I wasn't aware existed since it's just as easy
to pipe :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: php/mysql Query Question.

am 16.09.2009 18:53:23 von List Manager

admin@buskirkgraphics.com wrote:
> Before most of you go on a rampage of how to please read below...
>
> As most of you already know when using MySQL from the shell you can write your queries in html format in an out file.
>
> Example: shell>mysql -uyourmom -plovesme --html
> This now will return all results in an html format from all queries.
>
> Now I could “tee” this to a file and save the results returned if I so choose to save the result of the display .
>
> Let’s say I want to be lazy and write a php MySQL query to do the same so that any result I queried for would return the html results in a table without actually writing the table tags in the results.
>
> Is there a mysql_connect or select_db or mysql_query tag option to do that since mysql can display it from the shell?
>

Here is my rendition of an result to HTML table output function.

This is normally used within my db class

but I have modified it to work with a MySQLi result object

function debug($result) {
/* get column metadata */
$html = '

';
foreach ( $result->fetch_fields() AS $val ) {
$html .= '';
}
$html .= '';
foreach ( $result->fetch_row() AS $row ) {
$html .= '';
foreach ( $row AS $value ) {
$html .= '';
}
$html .= '';
}
$html .= '
'.$val->name.'
 '.$value.'
';
return $html;
}

Let us know if that works for you.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php