Tuesday, July 3, 2007

retrieve data from mysql using bash script

Besides from using perl scripts, phpmyadmin and customized php pages, here is a quick way to fetch data from mysql database using shell script.

Just make sure you are currently running mysql from your box with existing database and table inside.

First, launch your favorite linux editor and create a bash script and paste the following bash codes inside and save.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/bash
# july 03 2007 - vertito
# this script retrieves data from mysql using bash

dbase=`mysql -uMYSQLUSENAME -pMYSQLPASSWORD -e"use MYDATABASE; select * from MYTABLE where id = 1;"`
for data in $dbase ;
do
echo $data
done

echo Done.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#chmod 700 yourscript.sh
#./yourscript.sh

works like a charm!

Dont forget to change the following wordings:

MYSQLUSERNAME = your mysql username access with your mysql server
MYSQLPASSWORD = your mysql password
MYDATABASE = your running mysql database name
MYTABLE = your selected table name

works like a charm.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy