Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts

    Need help with query to array

    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

    PHP Code:
        $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>");
                }
            } 

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • Users who have thanked Keleth for this post:

    MaDmiX (07-11-2012)

  • #3
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    $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>"); 
                } 
            } 

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    That looks absolutely right.

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

  • #5
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    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

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    $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?

  • #7
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    $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.
    Last edited by MaDmiX; 07-11-2012 at 07:06 PM.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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

    PHP Code:
    $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).
    Last edited by Keleth; 07-11-2012 at 08:05 PM.

  • #9
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts
    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

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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

    PHP Code:
    $emailEng mysql_fetch_row($qryEng); 
    do this

    PHP Code:
    $emails = array();
    while (
    $emailEng mysql_fetch_row($qryEng)) $emails[] = $emailEng[0]; 
    Then implode as you had at first:

    PHP Code:
    $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.

  • Users who have thanked Keleth for this post:

    MaDmiX (07-11-2012)

  • #11
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts

    SOLVED: Using query results as an array for multiple mail() senders

    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

    PHP Code:
        $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); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •