Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-23-2012, 10:30 AM   PM User | #1
pdloan
New Coder

 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
pdloan is an unknown quantity at this point
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.
pdloan is offline   Reply With Quote
Old 07-23-2012, 12:22 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-23-2012, 01:48 PM   PM User | #3
pdloan
New Coder

 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
pdloan is an unknown quantity at this point
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.
pdloan is offline   Reply With Quote
Old 07-24-2012, 02:17 AM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-24-2012, 09:25 AM   PM User | #5
pdloan
New Coder

 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
pdloan is an unknown quantity at this point
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
pdloan is offline   Reply With Quote
Old 07-24-2012, 11:09 AM   PM User | #6
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by vinyl-junkie; 07-24-2012 at 01:23 PM.. Reason: removed quote containing spam links
tangoforce is offline   Reply With Quote
Old 07-24-2012, 12:09 PM   PM User | #7
pdloan
New Coder

 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
pdloan is an unknown quantity at this point
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.
pdloan is offline   Reply With Quote
Old 07-24-2012, 01:31 PM   PM User | #8
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 583
Thanks: 15
Thanked 65 Times in 65 Posts
Arcticwarrio is on a distinguished road
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");
?>
Arcticwarrio is offline   Reply With Quote
Old 07-24-2012, 02:04 PM   PM User | #9
pdloan
New Coder

 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
pdloan is an unknown quantity at this point
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..
pdloan is offline   Reply With Quote
Old 07-24-2012, 02:42 PM   PM User | #10
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 583
Thanks: 15
Thanked 65 Times in 65 Posts
Arcticwarrio is on a distinguished road
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");
?>
Arcticwarrio is offline   Reply With Quote
Old 07-24-2012, 02:47 PM   PM User | #11
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 583
Thanks: 15
Thanked 65 Times in 65 Posts
Arcticwarrio is on a distinguished road
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");}
?>
Arcticwarrio is offline   Reply With Quote
Old 07-24-2012, 03:24 PM   PM User | #12
pdloan
New Coder

 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
pdloan is an unknown quantity at this point
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.
pdloan is offline   Reply With Quote
Old 07-24-2012, 03:38 PM   PM User | #13
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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
Keleth is offline   Reply With Quote
Old 07-25-2012, 02:03 AM   PM User | #14
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-25-2012, 10:02 AM   PM User | #15
pdloan
New Coder

 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
pdloan is an unknown quantity at this point
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.
pdloan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:58 PM.


Advertisement
Log in to turn off these ads.