My code:
<?php
session_start();
?>
<?php
$username = $_SESSION['username'];
$connect = mysql_connect("localhost", "root","") or die("Couldn't connect!");
mysql_select_db("phplogin")or die("Couldn't find db");
//$id = $_POST['studentList']; <--I'm suppose to pass this from my other page, but for testing purpose I disable this and change it to B1101 below..
$to = mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'");
$subject = $_POST['subject'];
$message = $_POST['message'];
$send_contact=mail($to,$subject,$message);
if($send_contact){
echo "E-Mail is sent!";
}
else {
echo "ERROR";
}
?>
<?php
mysql_close();
?>
The problem is, it doesn't work and it sends this error message:
Warning: mail() expects parameter 1 to be string, resource given in C:\xampp\htdocs\HTPP\send_contact.php on line 23 ERROR
I tried to hard-code the $to
with my email and it works...now the problem is retrieving the email address from the database; the email datatype is varchar(65)
.
$result = mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'");
$row = mysql_fetch_array($result);
$to=return $row[0];
mysql_query returns resource not string..thats why error was there..
Replace this here:
$to = mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'");
$subject = $_POST['subject'];
$message = $_POST['message'];
$send_contact=mail($to,$subject,$message);
With this:
$res = mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'");
$to = mysql_result($res,0);
$subject = $_POST['subject'];
$message = $_POST['message'];
$send_contact=mail($to,$subject,$message);
When dealing with singular results returned from the database, mysql_result
is the function you should call to get the data from the query.
Note the reason you were getting the error is that $to
was a mysql resource, not a string.
mysql_result
gets the string from the mysql resource.
Just to be very clear on why it doesn't work like this, mysql_fetch_array
returns an array object with its properties named after the columns in the database, so in your case, from this code:
$result = mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'");
$row = mysql_fetch_array($result);
the $row variable would contain this (if you try to display it using print_r):
$row => Array(
[0] => [the address stored in the db],
[parentEmail] => [the address stored in the db]
)
That is why, as Rajat sugested, you would need to pass to the $to
variable the $row[0]
value.
I hope this was helpful. Have a great day.
this line
$to = mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'");
will get you a mysql-resource as the error says, and not a string. This is just not how a query works.
Just take a quick look over at the manual : you have to call something like
mysql_fetch_assoc($result)
first to get the information.
$to = mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'");
Because its not a string I think you forgot to fetch data
Try This:
$email = mysql_fetch_array(mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'"));
$to = $email['parentEmail'];
$to = mysql_query("SELECT `parentEmail` FROM `parent` WHERE parent.studentID = 'B1101'");
$subject = $_POST['subject'];
$message = $_POST['message'];
$send_contact=mail($to,$subject,$message);
In this area of your code please check it properly. You provide resource , you have to use mysql_fetch_assoc function to get your email
You cant send email on localhost without smart host.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.