Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,611 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 918 people online right now. Registration is fast and FREE... Join Now!




MySql is returning an error from my PHP code

 
Reply to this topicStart new topic

MySql is returning an error from my PHP code, email forgotten password

dilemmalurve
post 24 Jun, 2008 - 06:59 PM
Post #1


New D.I.C Head

*
Joined: 22 Jun, 2008
Posts: 16


My Contributions


i'm trying to write a script to send users their password if forgotten but i seem to have a problem...can someone help me...

i get this error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/...... on line 16

and line 16 is:
CODE
  $count=mysql_num_rows($result);


this is my codes:

CODE
     <?

include('db.php'); // database connection details stored here


// value sent from form
$email_to=$_REQUEST['email_to'];


// retrieve password from table where e-mail = $email_to
$result= mysql_query ("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '$email_to' ");


// if found this e-mail address, row must be 1 row
// keep value in variable name "$count"
$count=mysql_num_rows($result);

// compare if $count =1 row
if($count==1){

$rows=mysql_fetch_array($result);

// keep password in $your_password
$your_password=$rows['PASSWORD'];
$your_username = $rows['USER_NAME'];

// ---------------- SEND MAIL FORM ----------------

// send e-mail to ...
$to=$email_to;

// Your subject
$subject="Your password here";

// From
$header="from: PayPerConest <paypercontest.com>";

// Your message
$messages= "Hello $your_username\r\n";
$messages.= "Your password for login to our website \r\n";
$messages.="Your password is $your_password \r\n";
//$messages.="more message... \r\n";

// send email
$sentmail = mail($to,$subject,$messages,$header);

}

// else if $count not equal 1
else {
echo "Not found your email in our database";
}

// if your email succesfully sent
if($sentmail){
echo "Your Password Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send password to your e-mail address";
}

?>


if there are any mistakes please point them out to me...and if possible the solution i smuch appreciated

This post has been edited by dilemmalurve: 24 Jun, 2008 - 07:34 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 24 Jun, 2008 - 08:05 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,351



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(dilemmalurve @ 24 Jun, 2008 - 10:59 PM) *

if there are any mistakes please point them out to me...and if possible the solution i smuch appreciated

You have not made any mistakes, just poor coding. Never ever ever trust user results! What is happening is you are passing mysql_num_rows the value of the variable $result, & it has no value.

Always error check your code when you work with mysql. Poor code leads to passwords getting stolen, or databases getting erased.

CODE

if($_REQUEST['email_to']) $email_to=$_REQUEST['email_to'];
else die('Email does not have a value');

// retrieve password from table where e-mail = $email_to
$result= mysql_query ("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '$email_to' ");

if($result) $count=mysql_num_rows($result);
else die('No result was returned');
User is online!Profile CardPM

Go to the top of the page

dilemmalurve
post 25 Jun, 2008 - 05:26 PM
Post #3


New D.I.C Head

*
Joined: 22 Jun, 2008
Posts: 16


My Contributions


tongue.gif i'm sorry but i'm new at this so therefor the poor coding...was hoping that someone would help me out in that area... : D

This post has been edited by dilemmalurve: 25 Jun, 2008 - 05:33 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 25 Jun, 2008 - 05:32 PM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,351



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


Sorry, I didn't mean it like that.

Did the offered example help?

It is extremely important to always check the value when using $_GET, $_POST, or $_REQUEST, before processing it. This will keep things simple & secure, as well as handle errors correctly.
User is online!Profile CardPM

Go to the top of the page

dilemmalurve
post 25 Jun, 2008 - 05:40 PM
Post #5


New D.I.C Head

*
Joined: 22 Jun, 2008
Posts: 16


My Contributions


i'm trying it out now...actually more of figuring out how all this works...and thank you for your help...i will keep the advice in mind....thanks a million... icon_up.gif wub.gif

This post has been edited by dilemmalurve: 25 Jun, 2008 - 05:41 PM
User is offlineProfile CardPM

Go to the top of the page

dilemmalurve
post 25 Jun, 2008 - 06:48 PM
Post #6


New D.I.C Head

*
Joined: 22 Jun, 2008
Posts: 16


My Contributions


crazy.gif ok i cant seem to get it to work..where do i place the codes that you mentioned...

i'm sorry for the inconvenience..... biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

nadzree
post 26 Jun, 2008 - 12:03 AM
Post #7


New D.I.C Head

*
Joined: 25 Jun, 2008
Posts: 10



Thanked 1 times
My Contributions


QUOTE(dilemmalurve @ 24 Jun, 2008 - 07:59 PM) *

i'm trying to write a script to send users their password if forgotten but i seem to have a problem...can someone help me...

i get this error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/...... on line 16

and line 16 is:
CODE
  $count=mysql_num_rows($result);


this is my codes:

CODE
     <?

include('db.php'); // database connection details stored here


// value sent from form
$email_to=$_REQUEST['email_to'];


// retrieve password from table where e-mail = $email_to
$result= mysql_query ("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '$email_to' ");


// if found this e-mail address, row must be 1 row
// keep value in variable name "$count"
$count=mysql_num_rows($result);

// compare if $count =1 row
if($count==1){

$rows=mysql_fetch_array($result);

// keep password in $your_password
$your_password=$rows['PASSWORD'];
$your_username = $rows['USER_NAME'];

// ---------------- SEND MAIL FORM ----------------

// send e-mail to ...
$to=$email_to;

// Your subject
$subject="Your password here";

// From
$header="from: PayPerConest <paypercontest.com>";

// Your message
$messages= "Hello $your_username\r\n";
$messages.= "Your password for login to our website \r\n";
$messages.="Your password is $your_password \r\n";
//$messages.="more message... \r\n";

// send email
$sentmail = mail($to,$subject,$messages,$header);

}

// else if $count not equal 1
else {
echo "Not found your email in our database";
}

// if your email succesfully sent
if($sentmail){
echo "Your Password Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send password to your e-mail address";
}

?>


if there are any mistakes please point them out to me...and if possible the solution i smuch appreciated


CODE
$result= mysql_query ("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '$email_to' ");


Your mysql_query did not have connection, it should be like this

CODE

mysql_query($Query, $Connection) or die(mysql_error());


where connection hold this string

CODE

$Connection = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);


and other thing, it is best to exclude the php string from the SQL string

CODE

$result= mysql_query ("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '".$email_to."' ",$Connection) or die(mysql_error());


User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 26 Jun, 2008 - 08:34 AM
Post #8


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


QUOTE(nadzree @ 26 Jun, 2008 - 01:03 AM) *

CODE
$result= mysql_query ("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '$email_to' ");


Your mysql_query did not have connection, it should be like this

CODE

mysql_query($Query, $Connection) or die(mysql_error());


where connection hold this string

CODE

$Connection = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);


and other thing, it is best to exclude the php string from the SQL string

CODE

$result= mysql_query ("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '".$email_to."' ",$Connection) or die(mysql_error());



actually, you're wrong on a few things... a MySQL query doesn't need to have a connection in it, if you enable the connection at the beginning of the file...

when you exclude the php string, with the way you do it you're adding a LOT more then you need... instead of this:
php
$result= mysql_query ("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '".$email_to."' ",$Connection) or die(mysql_error());

do this
php
$result= mysql_query("SELECT USERNAME, EMAIL, PASSWORD  FROM Membership WHERE EMAIL= '$email_to'") or die(mysql_error());

you're ending the string, and calling another string... you don't need to end the string in the first place...

also, dil, try using the MySQL function COUNT... for more advanced apps it's more effective since MySQL stores results automatically..
User is offlineProfile CardPM

Go to the top of the page

nadzree
post 26 Jun, 2008 - 08:47 AM
Post #9


New D.I.C Head

*
Joined: 25 Jun, 2008
Posts: 10



Thanked 1 times
My Contributions


QUOTE
you're ending the string, and calling another string... you don't need to end the string in the first place...


Yes. Absolutely correct ya. e.g:

CODE
echo "I am nooby in".$php;
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 26 Jun, 2008 - 04:30 PM
Post #10


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


first, that's not even correct english... secondly, i AM correct >_> on MySQL Queries you can put variables directly into the sting as long as they have ' ' surrounding it

so you might wanna delete that and try being RIGHT next time before you start telling me i'm wrong
User is offlineProfile CardPM

Go to the top of the page

nadzree
post 27 Jun, 2008 - 03:33 AM
Post #11


New D.I.C Head

*
Joined: 25 Jun, 2008
Posts: 10



Thanked 1 times
My Contributions


QUOTE(JBrace1990 @ 26 Jun, 2008 - 05:30 PM) *

first, that's not even correct english... secondly, i AM correct >_> on MySQL Queries you can put variables directly into the sting as long as they have ' ' surrounding it

so you might wanna delete that and try being RIGHT next time before you start telling me i'm wrong



Dude..from my previous post, I'm telling you i'am wrong and you are rite..hehe...chill k..and English is my second language..give me a break will ya wink2.gif yess dear lord of PHP..you are rite..sorry..
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:33AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month