LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-22-2005, 10:12 AM   #1
petenyce
Member
 
Registered: Sep 2005
Posts: 39

Rep: Reputation: 15
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]
 
Old 11-22-2005, 12:02 PM   #2
msound
Member
 
Registered: Jun 2003
Location: SoCal
Distribution: CentOS
Posts: 465

Rep: Reputation: 30
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.
 
Old 11-22-2005, 12:30 PM   #3
petenyce
Member
 
Registered: Sep 2005
Posts: 39

Original Poster
Rep: Reputation: 15
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

Last edited by petenyce; 11-22-2005 at 01:04 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL restart script? Fuwex Debian 2 08-17-2005 02:23 PM
mysql script files richard22 Linux - General 1 08-16-2005 03:20 AM
Mysql in bash script acrors Red Hat 2 08-18-2004 09:11 PM
Bash script and mysql flashingcurser Programming 1 07-19-2004 03:35 AM
MySQL in a shell script?! zaubara Programming 2 05-31-2004 05:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration