LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to pass mysql query to a variable? (http://www.linuxquestions.org/questions/programming-9/how-to-pass-mysql-query-to-a-variable-226007/)

chynna_v 09-03-2004 12:48 AM

How to pass mysql query to a variable?
 
Hi can somene help me how to pass the output of an mysql query to a variable?

for ex.
==========================================================
#!/bin/sh
echo select count(*) from out where bfr_id=1 and status=0' | mysql -uroot
===========================================================
output is
count(*)
47568

i try to do this...
===============================================================
#!/bin/sh
DBS='echo select count(*) from out where bfr_id=1 and status=0' | mysql -uroot
echo $DBS
================================================================

nut nothing happens...what i want is to pass the value of "count(*)" to the variable of DBS and then prints it...

can you please help

thanks a lot...

Cedrik 09-03-2004 04:19 AM

You don't indicate the database name, and don't use the inversed quotes, try :

DBS=`"echo select count(*) from out where bfr_id=1 and status=0" | mysql -uroot <database name>`

I assume there is no password for user root and the mysql server is running on your local machine

chynna_v 09-03-2004 04:26 AM

i already tried that and got this error message:

found
Code:

./update_tempo.sh: line 1: echo select count(*) from out where bfr_id=1 and status=0: command not found

Cedrik 09-03-2004 04:38 AM

I am sorry ;)

try :
DBS=`echo "select count(*) from out where bfr_id=1 and status=0" | mysql -uroot <database name>`

chynna_v 09-03-2004 05:09 AM

it worked!..thanks a lot..i knew that i was having problem where to put the inversed quotes...


All times are GMT -5. The time now is 12:46 PM.