LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Mysql script (https://www.linuxquestions.org/questions/linux-newbie-8/mysql-script-385511/)

petenyce 11-22-2005 10:12 AM

Mysql script
 
I have a linux script that wirtes a csv file for my mysql database table. Now its writing the csv file even if the tbale is empty? is their a way to check to see if theirs data first, if so write the file if not kill this part of the script?


Script

[script]
select * into outfile '/tmp/file2'
FIELDS TERMINATED BY ';' LINES TERMINATED BY '\N' FROM DIRECT_REFERRAL
[/script]

msound 11-22-2005 12:02 PM

Using PHP you could do something like this:
PHP Code:

$Host "your-mysql-host";
$User "mysql-username";
$Password "mysql-password";
$DBName "your-db-name";
$TableName "your-db-table";
$Link mysql_connect ($Host$User$Password);

$Query "SELECT * from $TableName LIMIT 1";
$Result mysql_db_query ($DBName$Query$Link);
$intResults mysql_num_rows($Result);
if (
$intResults)
{
   
select into outfile '/tmp/file2'
   
FIELDS TERMINATED BY ';' LINES TERMINATED BY '\N' FROM DIRECT_REFERRAL
}
mysql_close ($Link); 

This would basically do a general select from the DB table. Set the number or results found to a variable called $intResults. Then use an if statement that says if your query generated at least 1 result, then create the csv file.

petenyce 11-22-2005 12:30 PM

ok
 
Hers is my full bin bash script. I only want to run the this script if their is data in my table? if not do nothing?

thanks

#!/bin/bash
date=`date +%Y%m%d%H%M%S`
file="ReferralAuth$date.txt"
cat /usr/etc/extractreferral.sql | sed -e "s/file/$file/g" |/usr/bin/mysql -u root XXXXX


extractreferral.sql
SELECT * INTO OUTFILE '/tmp/file'
FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' FROM Referral_Authorization


All times are GMT -5. The time now is 06:13 AM.