如何修复“解析错误:语法错误,意外的'回声'(T_ECHO)"在 PHP 中从 MySql 运行选择查询时 [英] How to fix "Parse error: syntax error, unexpected 'echo' (T_ECHO)" in PHP when running a select query from MySql
问题描述
我试图从 MySql 查询中填充一个 php,以便稍后在通过 php 发送电子邮件时使用.请参阅下面的代码以填充 veriable.我没有包括 mysql_connect 和 mysql_select_db,因为这是一个实时数据库,我知道连接有效.同样在您声明我应该使用 mysqli 或 POD 之前,我知道但服务器无法更新,因为有大量页面依赖于旧代码.
Im trying to populate a php veriable from a MySql query to use later on when sending an email via php. Please see below below code for populating the veriable. I have not included the mysql_connect and mysql_select_db as this this a live db and i know the connections work. Also before you state that i should be using mysqli or POD i know but the server cannot be updated as there is a large number of pages that rely on the old code.
错误 - 解析错误:语法错误,意外的回声"(T_ECHO)
Error - Parse error: syntax error, unexpected 'echo' (T_ECHO)
$emailaddress = "SELECT e_mail FROM frm_change_approver WHERE user_id LIKE '$approvingmanagername'";
$result = mysql_fetch_array($emailaddress);
$approveremail = echo $result['e_mail'];
我需要通过上述查询填充 $approveremail,因为我已经在数据库中拥有用户的电子邮件地址,并且不希望用户输入错误的地址,我只在表单中捕获用户 user_id,因为我没有希望有一个电子邮件地址字段.然后我将使用填充的验证将电子邮件发送给该人.
I need $approveremail to be populated via the above query as i already have the email address for the user in the database and dont want a user to type the wrong one, i only capture the users user_id in the form as i dont want there to be an email address field at all. I will then use the populated veriable to send the email to that person.
将不胜感激任何帮助.
推荐答案
不能将 echo 语句分配给变量.
You cannot assign an echo statement to a variable.
改变这个:
$approveremail = echo $result['e_mail'];
为此:
$approveremail = $result['e_mail'];
echo $approveremail;
甚至:
echo $result['e_mail'];
此外,请考虑使用 mysqli 或 PDO 代替 mysql_ 函数.mysql_ 函数已弃用,PHP 7.0 及更高版本不再支持.
Furthermore, please consider using mysqli or PDO instead of mysql_ functions. mysql_ function are deprecated and no longer supported in PHP 7.0 and above.
看看这个页面https://www.php.net/manual/en/function.mysql-fetch-array.php
您需要运行查询然后获取结果
You need to run the query and then fetch the result
$emailaddress = "SELECT e_mail FROM frm_change_approver WHERE user_id = '$approvingmanagername'";
$result = mysql_query($emailaddress);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$approveremail = $row['e_mail'];
另外,请考虑使用 mysql_real_escape_string() 来清理您的输入 https://www.php.net/manual/en/function.mysql-real-escape-string.php
Also, please consider to use mysql_real_escape_string() to sanitize your inputs https://www.php.net/manual/en/function.mysql-real-escape-string.php
这篇关于如何修复“解析错误:语法错误,意外的'回声'(T_ECHO)"在 PHP 中从 MySql 运行选择查询时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!