Jump to content
Updating IPB tonight Read more... ×

Archived

This topic is now archived and is closed to further replies.

micah1701

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

Recommended Posts

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.

[code]$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 &");[/code]

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:[code]$getMessage = mysql_query("SELECT * FROM bulkMail WHERE id='$id'");[/code] 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.  ???

Share this post


Link to post
Share on other sites
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?

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

Share this post


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

Ken

Share this post


Link to post
Share on other sites
[quote]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>';[/quote]

the $_SERVER array passes:
[code]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
)
[/code]

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

[code]$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;
[/code]

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.

Share this post


Link to post
Share on other sites
what do you mean by "failing" do you mean it's not returning a result, or are you getting some kind of error?

Share this post


Link to post
Share on other sites
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:
[code]$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
[/code]

This sends me an e-mail with the message [quote]the id passed was: 7[/quote]

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:
[code]$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
[/code]

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

Share this post


Link to post
Share on other sites
maybe it's not the id but the query itself.  try changing this:

[code]
$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");
[/code]

to this:

[code]
$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()));
[/code]
maybe the query itself is failing and it will send you an email with a clue

Share this post


Link to post
Share on other sites
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:
[code=php:0]
<?php
$getMessage = mysql_query("SELECT * FROM bulkMail WHERE id='$id'");
?>
[/code]

to

[code=php:0]
<?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(); }
?>
[/code]

Ken

Share this post


Link to post
Share on other sites
[quote]maybe the query itself is failing and it will send you an email with a clue[/quote]'

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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.