...

View Full Version : Need help with query to array



MaDmiX
07-11-2012, 04:28 PM
Hi All,

I have the following code to query a table and create an array from the results. What I want to do is take this and use it in the $to variable of the PHP mail() function. I am getting the error:

Warning: mail() [function.mail]: SMTP server response: 501 5.1.3 Invalid address

When I echo back my $emailEng variable I get this:
Resource id #6

And the $qryEng variable is echoing back as:
Array

Clearly I am using the mysql_fetch_row() function improperly (or at least I think so). Can someone help with this?

Thanks in advance,

Ken


$qryEng = mysql_query("SELECT userName FROM tblusers WHERE emailGroup LIKE 'engineering'");
echo($qryEng);

$emailEng = mysql_fetch_row($qryEng);
echo($emailEng);

if($DiscrepType = "Equipment issue") {
$to="'".implode(",", $emailEng)."'";
$subject = 'An equipment issue has been logged';
$message='The details of this entry can be found at'.' http://myHyperlink';
$from='me@domain.com';
if(mail($to, $subject, $message)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}

Keleth
07-11-2012, 04:37 PM
You should review the documentation for both the functions. I'm assuming that you mean that $emailEng is returning Array, not $qryEng.

The $qryEng should return a resource, as defined by the function. A query returns a query resource, which is useless to you or I, but can be interpreted by the various functions like mysql_fetch_row.

$emailEng should return an array, as defined by the function, as its purpose is to take the next row in the resource, and return an array of the columns. Even if you select just one column, it will return an array, with only one element within. Try to var_dump it or print_r it and you'll see.

You're using it fine, but should review the documentation.

MaDmiX
07-11-2012, 04:53 PM
Hi Keleth,

Thanks for your help. BTW, I actually meant that I am getting the string "Array" when I echo the $qryEng variable.

I looked over the documentation and I think I see where I am going wrong. I can't get back to my desk to test but am I on the right track by modifying the $to variable as follows:


$qryEng = mysql_query("SELECT userName FROM tblusers WHERE emailGroup LIKE 'engineering'");
echo($qryEng);

$emailEng = mysql_fetch_row($qryEng);
echo($emailEng[0]);

if($DiscrepType = "Equipment issue") {
$to="'".implode(",", $emailEng[0])."'"; //This is changed.
$subject = 'An equipment issue has been logged';
$message='The details of this entry can be found at'.' http://myHyperlink';
$from='me@domain.com';
if(mail($to, $subject, $message)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}

Keleth
07-11-2012, 05:36 PM
That looks absolutely right.

Though getting 'Array' when echoing $qryEng makes no sense... are you sure you didn't mean $emailEng?

MaDmiX
07-11-2012, 06:22 PM
Though getting 'Array' when echoing $qryEng makes no sense... are you sure you didn't mean $emailEng?

You were correct.

echo($qryEng); //Resource id #7
echo($emailEng); //This is working now, thanks!

I am still having trouble with the implode function. I am getting the error:
Warning: implode() [function.implode]: Invalid arguments passed

Can I not use $emailEng[]? This is an array, right?

Kind regards,

Ken

Keleth
07-11-2012, 06:39 PM
$emailEng is an array, $emailEng[0] is NOT. That's what you're trying to implode. But why implode it at all? Why not just concatenate it?

MaDmiX
07-11-2012, 07:03 PM
$emailEng is an array, $emailEng[0] is NOT. That's what you're trying to implode. But why implode it at all? Why not just concatenate it?

Would that look something like this?:

$to="'".$emailEng[0].","."'";

What about the encasing quotes?

What if there is only one value in the array? Then the comma wouldn't be needed.

Keleth
07-11-2012, 07:18 PM
I'm confused....

The array will only ever have one value, as you're only selecting one column from the database.... So you really only need


$to= $emailEng[0];

You don't need the initial double quotes (you'd joining nothing to the start of the string, so why bother?) and same thing for the final set of quotes (again, joining nothing to the end).

MaDmiX
07-11-2012, 07:49 PM
Hmmm. OK now I'm confused.

When I query my table it will return X number of records for the column "userName". I am using the array to store all the returned values for that column.

Is the mysql_fetch_row() function not what I need to use here?

Eventually I will have other queries filtering for different emailGroups and that is why there is the possibility that a given group will have only one record returned (thus not needing a comma separating multiple email addresses).

So the $to variable needs to be able to handle both cases. That is why I opted for the implode() function because I can use a comma as my glue.

Was I just way off base :-) ? And if so, how would I handle this?

Thanks,

Ken

Keleth
07-11-2012, 08:19 PM
OOOOOH.

Ok, you have the right idea, but not the right understanding.

So when you do a query, you're given back a resource. That resource is not readable; it doesn't have anything you can use directly. Instead, there are a number of functions that take that resource and give you back the data within.

mysql_fetch_row takes that resource and returns the next row in the resource. So to get all the rows in the resource, you have to loop through them. If you have one row returned, it'll only go through once, if 0 rows returns, it won't loop at all.

Rather then


$emailEng = mysql_fetch_row($qryEng);

do this



$emails = array();
while ($emailEng = mysql_fetch_row($qryEng)) $emails[] = $emailEng[0];


Then implode as you had at first:


$to="'".implode(",", $emailEng)."'";


I strongly recommend you look into PDO though. Its a nice way to access MySQL and prepared statements are nice and secure. It might be a bit harder to grasp if you don't know OOP, but better to learn right then have to relearn later.

MaDmiX
07-11-2012, 08:50 PM
SUCCESS!

I had to tweak the code a bit and use the $emails variable in the implode function (as well as drop the concatenated quotes) but it's working. I added two additional email addresses to the engineering group and all three addresses received the email alert.

Thank you!

Best regards,

Ken


$qryEng = mysql_query("SELECT userName FROM tblusers WHERE emailGroup LIKE 'engineering'");

$emails = array();
while ($emailEng = mysql_fetch_row($qryEng)) {
$emails[] = $emailEng[0];
}
if($DiscrepType = "Equipment issue") {
$to=implode(",", $emails);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum