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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sending email id with php and mysql.

    I have stored the email id in the database and want to sent a mail on that email id.
    PHP Code:
    <?php
    $username
    =$_POST['uname'];

    $con=mysql_connect("localhost","root","") or ("Cannot connect localhost");

    mysql_select_db('project',$con) or die("connection field");

    $q=mysql_query("select email from admin where username='$username'");

    $numrows=mysql_num_rows($q);

    if(
    $numrows!=0)
    {
        while( 
    $row=mysql_fetch_assoc($q))
        {
            
    $emailid $row['email'];
        }
            
    $to $emailid;
            
    $subject "Test mail";
            
    $message "Your security code is 5555.";
            
    $from "abc.sample@gmail.com";
            
    $headers "From:" $from;
            
    mail($to,$subject,$message,$headers);
            echo 
    "Mail Sent.";

    }
    else 
        die (
    "That username doesnot exist");
    ?>
    It sows the "Warning: mysql_num_rows() expects parameter 1 to be resource"
    I am new to php please help me out.Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    This means your SQL query failed. Presumably because you've not run $username through mysql_real_escape_string(). If you type in something into the username box (such as an ' for example) then it will break your SQL string and when you put it through mysql_query the mysql server will not parse it correctly, return a false instead of a resource result and consequently your call to mysql_num_rows() will return an error.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sir, I corrected the above error after removing error i again checked then it showed the following error "Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() on line 29 "
    The code on line 29 is" mail($to,$subject,$message,$headers); "
    The file code is as below :-

    PHP Code:
    <?php
    $username
    =$_POST['uname'];

    $con=mysql_connect("localhost","root","") or ("Cannot connect localhost");

    mysql_select_db('project',$con) or die("connection field");

    $q=mysql_query("select email from admin where uname='$username'");

    $numrows=mysql_num_rows($q);

    if(
    $numrows!=0)
    {
        while( 
    $row=mysql_fetch_assoc($q))
        {
            
    $emailid $row['email'];
        }
            
    $to $emailid;
            
    $subject "Test mail";
            
    $message "Your security code is .";
            
    $from "abc.sample@gmail.com";
            
    $headers "From:" $from;
                
    mail($to,$subject,$message,$headers);
            echo 
    "Mail Sent.";

    }
    else 
        die (
    "That username doesnot exist");
    ?>
    Please help me out.
    Waiting for your answer sir.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by pdloan View Post
    Sir, I corrected the above error
    So why does the code look identical? - You have not used mysql_real_escape_string() and nothing else looks different either.

    In other words, you have made no changes

    How do you expect me to help you if you won't make changes and post the same code?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry sir but i m not getting where to use the following statement mysql_real_escape_string(). Previously i used username instead of using uname hence it was showing that error. Then i changed it to uname the error shown was "Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() on line 29 "
    The code on line 29 is" mail($to,$subject,$message,$headers); " .
    Sorry but please explain me where to use and how to use (mysql_real_escape_string() statement) i dont know much about php .
    Please help me out.
    Last edited by vinyl-junkie; 07-24-2012 at 01:22 PM. Reason: advertising/spam links removed

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    FYI:

    $username = mysql_real_escape_string($_POST['uname']);

    The function description is available on www.php.net

    As for your email problem, thats a server configuration issue. We can't help you with that, you will need to show the php.ini settings.
    Last edited by vinyl-junkie; 07-24-2012 at 01:23 PM. Reason: removed quote containing spam links
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sir can you tell, while installation weather we have to make any changes in php.ini file. I tried to attach the php.ini file but it is showing upload file error.

  • #8
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    something like this maybe?

    PHP Code:
     <?php
    $username 
    mysql_real_escape_string($_POST['uname']);

    $con=mysql_connect("localhost","root","") or ("Cannot connect localhost");

    mysql_select_db('project',$con) or die("connection field");

    $q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = '".$username);

    $numrows=mysql_num_rows($q);

    if(
    $numrows!=0)
    {
        while( 
    $row=mysql_fetch_assoc($q))
        {
            
    $emailid $row['email'];
        }
            
    $to $emailid;
            
    $subject "Test mail";
            
    $message "Your security code is .";
            
    $from "abc.sample@gmail.com";
            
    $headers "From:" $from;
                
    mail($to,$subject,$message,$headers);
            echo 
    "Mail Sent.";

    }
    else 
        die (
    "That username doesnot exist");
    ?>

  • #9
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I made these changes but it is showing there is some problem related to php.ini file.
    Last edited by pdloan; 07-24-2012 at 02:15 PM.

  • #10
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    you could inject it into a mail handler:

    download php mailer script from here: http://phpmailer.sourceforge.net

    put class.phpmailer.php in the same folder as your functions.php

    if you dont yet have one make one and add the following or add this to your existing one

    functions.php
    PHP Code:
    <?php
        
    function Email($To,$Code) {
            include (
    "class.phpmailer.php");
        
            
    $mail = new PHPMailer();
            
            
    $mail->IsSMTP();      // set mailer to use SMTP
            
    $mail->SMTPDebug 1;
            
    $mail->SMTPAuth true;     // turn on SMTP authentication
            
    $mail->SMTPSecure 'ssl'
            
    $mail->Host "smtp.gmail.com";  // specify main server
            
    $mail->Port 465
            
            
    $mail->Username "abc.sample@gmail.com";  // SMTP username
            
    $mail->Password "pass"// SMTP password

            
    $mail->From "abc.sample@gmail.com";
            
    $mail->FromName "Admin - ".$From;
            
    $mail->AddAddress($To);

            
    $mail->WordWrap 50;                                 // set word wrap to 50 characters
            
    $mail->IsHTML(true);                                  // set email format to HTML

            
    $mail->Subject "Test mail";
            
    $mail->Body    "Your security code is ".$Code;

            if(!
    $mail->Send()) {
               echo 
    "Mailer Error: " $mail->ErrorInfo;
               exit;
            }
        }
    ?>
    then use this to send your mail

    PHP Code:
     <?php
    include ("functions.php");
    $username mysql_real_escape_string($_POST['uname']);

    $con=mysql_connect("localhost","root","") or ("Cannot connect localhost");

    mysql_select_db('project',$con) or die("connection field");

    $q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = '".$username);

    $numrows=mysql_num_rows($q);

    if(
    $numrows!=0)
    {
        while( 
    $row=mysql_fetch_assoc($q))
        {
            
    $emailid $row['email'];
        }
            if (
    Email($emailid$row['code']) == 'Sent'){
               echo 
    "Mail Sent.";
            }
    }
    else 
        die (
    "That username doesnot exist");
    ?>

  • #11
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    actually you should never have more than 1 username the same so you could use this:

    PHP Code:
    <?php
    include ("functions.php");
    $con=mysql_connect("localhost","root","") or die("Cannot connect localhost");
    mysql_select_db('project',$con) or die("connection field");
    $q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = '".mysql_real_escape_string($_POST['uname']));
    if (
    mysql_num_rows($q) != 0){
        
    $row mysql_fetch_assoc($q);
        if (
    Email($row['email'], $row['code']) == 'Sent'){
            echo 
    "Mail Sent.";
        }
    }else{die (
    "That username doesnot exist");}
    ?>

  • #12
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the help i checked it, now mail is sent. All the problem was i was running on the localhost. But when all the files are uploaded on the server it is directly sending the email. Thank you very much Arcticwarrio for helping.

  • #13
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, if you don't have a mail server setup and running on your local system, of course you can't send mails from it

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Arcticwarrio View Post
    PHP Code:
    $q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = '".mysql_real_escape_string($_POST['uname'])); 
    You've missed something on that line and in your previous versions of it in your previous posts:

    $q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = 'whatever'

    That last apostrophe is rather important
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #15
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    Well, if you don't have a mail server setup and running on your local system, of course you can't send mails from it
    I totally agree with you sir, mail server setup is essential for sending email from localhost.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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