LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PHP script to retrieve records from MYSQL db and pass them to an external program (http://www.linuxquestions.org/questions/programming-9/php-script-to-retrieve-records-from-mysql-db-and-pass-them-to-an-external-program-470220/)

Julianus 08-02-2006 05:56 PM

PHP script to retrieve records from MYSQL db and pass them to an external program
 
I'm trying to write a small shell script that access my database, retrieves some records and use them to edit the tags of my ogg files. I use exec to execute vobiscomment. If the title is not a variable, vorbiscomment renames the tag:

Code:

#!/usr/local/bin/php

<?php

  $link = mysql_connect("localhost","root","");

  mysql_select_db("Eliza");

  $query = "select * from Eliza order by Title_ID limit 5";
  $result = mysql_query($query) or die("Could not: $query");

  while ($row = mysql_fetch_array($result))

  {

print $row["Title"];
print "\n";

echo exec ('vorbiscomment -w -t title=Symphony 01.ogg');
print "\n";
print "\n";

  }

?>

The result is as follows:

Code:

$ vorbiscomment -l 01.ogg
title=Symphony
$

However, if I set the title to be the variable, it doesn't work:
Code:

echo exec ('vorbiscomment -w -t title=$row["Title"] 01.ogg');


$ vorbiscomment -l 01.ogg
title=[Title]
$

Also, it doesn't work like this:
Code:

$title=$row["Title"];
echo exec ('vorbiscomment -w -t title=$title 01.ogg');


$ vorbiscomment -l 01.ogg
title=
$

Apparently, the problem is how to declare the variable.
Any suggestions?

chrism01 08-02-2006 11:51 PM

Well, in shell and Perl single quotes mean don't interpolate vars ie don't cvt vars to values. double quotes do allow interpolation. could be the same for php?

smallville 08-03-2006 02:35 AM

try this:

echo exec ('vorbiscomment -w -t title='.$row["Title"].'01.ogg');

or

$code = exec ('vorbiscomment -w -t title='.$row["Title"].'01.ogg');
echo $code;

or if if still won't work, try this:

$com = 'vorbiscomment -w -t title='.$row["Title"].'01.ogg';
$code = exec($com);
echo $code;

Julianus 08-03-2006 02:09 PM

Thank you for your suggestion. This solved the problem:

$title=$row["Title"];
$composer=$row["Composer"];

$vorbis="vorbiscomment -w -t composer='$composer' -t title='$composer - $title' 01.ogg";
echo exec ($vorbis);


All times are GMT -5. The time now is 09:32 PM.