Jump to content


Photo

$_SERVER['argv'][1] is breaking script in query ?


  • Please log in to reply
9 replies to this topic

#1 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 03 July 2006 - 05:13 PM

I have a mass mailing program that lets a user write a e-mail message into a textbox and hit submit.  The script then saves the contents in a database and "forks" (sort of) using exec() to run the script that goes through each member in the database and send them the message.

$getID = mysql_query("SELECT MAX(id) AS id FROM bulkMail");
$id = mysql_result($getID,0,0);

//execute distribution script
exec("php /www/htdocs/newsletter/distribution.php $id > /dev/null &");

this allows the user to go about their life while the script in "distribution.php" runs the e-mail program and sends the message w/ the id of $id.

On the distribution.php page, this $id is passed in array $_SERVER['argv'][1];

The id IS BEING PASSED, if I use the script and have it output the passed variable, it shows it to me.  The problem is when I do this:
$getMessage = mysql_query("SELECT * FROM bulkMail WHERE id='$id'");
The script wont work.

if I overwrite $id to an integer of a known table row ($id=5;) the script works.
but if I use the passed variable ($id= $_SERVER['argv'][1]) it doesn't work.

What am I missing?  I'm pulling my hair out trying to figure this out.  ???
"Confidence in the face of risk."

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 03 July 2006 - 05:31 PM

Are you trying to use $_SERVER['argv'][1] within the string? If so, you need to use braces to disambiguate. Otherwise, I don't understand how the variable is passed, but you can't use it. What does this show you?

echo '<pre>', print_r($_SERVER, true), '</pre>';

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 July 2006 - 05:36 PM

Please post the code of your "distribution.php" script. We need to see that code before we can help you.

Ken

#4 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 03 July 2006 - 05:50 PM

I don't understand how the variable is passed, but you can't use it. What does this show you?

Code:

echo '<pre>', print_r($_SERVER, true), '</pre>';


the $_SERVER array passes:
Array
(
    [PWD] => /var/www/html
    [JTOOLS_CONF_USER] => .jtools
    [JTOOLS_HOME] => /usr
    [LC_MESSAGES] => C
    [LD_PRELOAD] => 
    [HOSTNAME] => sd32.vds2000.com
    [LD_LIBRARY_PATH] => /lib:/usr/lib:/usr/local/lib:/usr/local/hostdir/lib
    [SPH_CONFIG] => /usr/local/hostdir/conf/hostdir.xml
    [LESSOPEN] => |/usr/bin/lesspipe.sh %s
    [ENV] => /root/.bashrc
    [USER] => root
    [LS_COLORS] => no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jpg=01;35:*.png=01;35:*.gif=01;35:*.bmp=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.png=01;35:*.mpg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:
    [MACHTYPE] => i386-redhat-linux-gnu
    [SPH_HOME] => /usr/local/hostdir
    [LC_ALL] => C
    [MAIL] => /var/spool/mail/root
    [INPUTRC] => /etc/inputrc
    [BASH_ENV] => /root/.bashrc
    [SPH_ACCOUNTS] => /sphera/accounts
    [LANG] => C
    [LC_NUMERIC] => C
    [LOGNAME] => root
    [SHLVL] => 7
    [JTOOLS_LOGLEVEL] => notice
    [LC_CTYPE] => C
    [_] => /bin/php
    [SHELL] => /bin/bash
    [HOSTTYPE] => i386
    [OSTYPE] => linux-gnu
    [HISTSIZE] => 1000
    [TERM] => xterm
    [HOME] => /root
    [PATH] => /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
    [JTOOLS_CONF_SYS] => /etc/jtools
    [LC_MONETARY] => C
    [LC_COLLATE] => C
    [SPHERA_monitor] => xxxx
    [SPHERA_service] => xxxx
    [SPHERA_fifoPath] => xxxxxxxxxxxxxxxxxxxxxx
    [SPHERA_execName] => xxx
    [SPHERA_pIOr] => x
    [SPHERA_pIOw] => x
    [SPHERA_pNETr] => x
    [SPHERA_pNETw] => x
    [SPHERA_fd0] => xxxx
    [SPHERA_fd1] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    [SPHERA_fd2] => xxxxxxxxxxxxxxxxxxxxxxxxxx
    [SPHERA_fd4] => xxxxxxxxxxxxxxxxxxxxx
    [SPHERA_fd5] => xxxxxxxxxxxxxxxx
    [SPHERA_fd6] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    [SPHERA_fd7] => xxxxxx
    [SPHERA_fd15] => 
    [SPHERA_fd16] => xxxxxxxxxxxxxxxxxxxxxxxxxxx
    [SPHERA_fd17] => xxxxxxxxxxxxxxxxxxxxxx
    [SPHERA_config] => x
    [PHP_SELF] => /www/htdocs/newsletter/distribution.php
    [SCRIPT_NAME] => /www/htdocs/newsletter/distribution.php
    [SCRIPT_FILENAME] => /www/htdocs/newsletter/distribution.php
    [PATH_TRANSLATED] => /www/htdocs/newsletter/distribution.php
    [DOCUMENT_ROOT] => 
    [argv] => Array
        (
            [0] => /www/htdocs/newsletter/distribution.php
            [1] => 7
        )

    [argc] => 2
)

Note at the very bottom, argv[1] is the value 7.  That is the value I want in my query.

my code in distribution.php is

$id = $_SERVER['argv'][1];

$getMessage = mysql_query("SELECT * FROM bulkMail WHERE id='$id'") or die(mysql_error());
$messageRow = mysql_fetch_array($getMessage);
$subject = $messageRow['subject'];
$message = $messageRow['body'];

echo $subject."<br>";
echo $message;

its failing at $getMessage = mysql_query.....
however, if I change the line that defines $id to an integer, ie:
$id = 7;

then, the script will work fine.

"Confidence in the face of risk."

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 July 2006 - 05:57 PM

what do you mean by "failing" do you mean it's not returning a result, or are you getting some kind of error?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 03 July 2006 - 06:04 PM

since the script is running in the background, I can't exactly echo or print error messages to the screen.

what I have been doing is inserting a quick code to e-mail what I want outputted.

in the previously posted code, I added:
$id = $_SERVER['argv'][1];

mail("micah.murray@mywebsite.com","distribution script ran","the id passed was: $id");

$getMessage = mysql_query("SELECT * FROM bulkMail WHERE id='$id'");
$messageRow = mysql_fetch_array($getMessage);
$subject = $messageRow['subject'];
$message = $messageRow['body'];

....
below is code that send my e-mail to all the users

This sends me an e-mail with the message

the id passed was: 7


So I know it passed the variable, but the rest of the script didn't work.

then I did the same thing but moved my mail() script down a line to right after the query:
$id = $_SERVER['argv'][1];

$getMessage = mysql_query("SELECT * FROM bulkMail WHERE id='$id'");

mail("micah.murray@mywebsite.com","distribution script ran","the id passed was: $id");

$messageRow = mysql_fetch_array($getMessage);
$subject = $messageRow['subject'];
$message = $messageRow['body'];

....
below is code that send my e-mail to all the users

After doing that, it wouldn't even send me my e-mail.  So it has to be that line w/ the query.

"Confidence in the face of risk."

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 July 2006 - 06:18 PM

maybe it's not the id but the query itself.  try changing this:

$id = $_SERVER['argv'][1];

$getMessage = mysql_query("SELECT * FROM bulkMail WHERE id='$id'");

mail("micah.murray@mywebsite.com","distribution script ran","the id passed was: $id");

to this:

$id = $_SERVER['argv'][1];

$sql = "SELECT * FROM bulkMail WHERE id='$id'";
$getMessage = mysql_query($sql) or die(mail("micah.murray@mywebsite.com","test","query string: $sql\n\r".mysql_error()));
maybe the query itself is failing and it will send you an email with a clue
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 July 2006 - 06:25 PM

When I have to debug these types of problems I either use mail like you are doing or write messages to a temporary file. If you use a file, make sure the file is writable by the webserver.

In your case change the line:
<?php
$getMessage = mysql_query("SELECT * FROM bulkMail WHERE id='$id'");
?>

to

<?php
$q = "SELECT * FROM bulkMail WHERE id='$id'";
$getMessage = mysql_query($q);
if (!$getMessage) {
    $mailmsg = "There was a problem in the query: $q\n".mysql_error();
   mail("micah.murray@mywebsite.com","error on query",$mailmsg);
   die(); }
?>

Ken

#9 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 03 July 2006 - 06:28 PM

maybe the query itself is failing and it will send you an email with a clue

'

Crayon Violent and Kenrbns, YOU WERE RIGHT!  I was looking at my error output the wrong way

THANK YOU!!!!  :D

yeah, I feel like an idiot. I was so sure it wasn't the query because I tested the page by running it straight from the browser (not using exec()) and added a mysql_error() line and it didn't report any errors.

when I ran it from the browser, the page had no problems accessing my connection string information.
I use include('configInfo.php');

but when the site tried to run the file, it could needed a differnet path, IE:
include('/www/htdocs/scripts/.../configInfo.php');

I didn't realize this until you had me check for errors through the e-mail outtput.

Problem solved.  Thanks!
"Confidence in the face of risk."

#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 July 2006 - 06:30 PM

sweet.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users