php - 使用 PHP 在 if 语句后启动函数时出错

标签 php mysql

之前没有真正创建我自己的函数,所以我正在尝试在 function NewsUpdate 行下启动脚本,但我希望它们是函数的一部分。在 if 语句中,当脚本在“电子邮件消息”中找到字符串“News Update”时,它将启动这些脚本,如果没有(else 语句一直在底部),它将失败并显示“News Update not found在电子邮件中"

但是我得到了错误:

Parse error: syntax error, unexpected T_FUNCTION

我做错了什么?

// Test code for email message.
$open_email_msg =  file_get_contents('emailmessage.html');

// A script that searches the e-mail for the string News Update,
// and if it is found it will start the function NewsUpdate

if(strpos($open_email_msg,"News Update"))
    function NewsUpdate ($open_email_msg) {
        // Login to MySQL Datebase
        $hostname = "localhost";
        $db_user = "user";
        $db_password = "pass";
        $database = "tablename";
        $db_table = "bx_news_entries";
        $db = mysql_connect($hostname, $db_user, $db_password);
        mysql_select_db($database,$db);
        $subject = 'Test News Article';
        $tags = str_replace(' ',',',$subject); // DDIE
        $uri =  str_replace(' ','-',$subject); // DDIE
        $when = strtotime("now");    // date article was posted
        $categories = 'Events';
        $content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.';
        $snippet = 'Lorem ipsum dolor sit amet.';
        // $snippet = explode(".", $content, -1);
        # THIS CODE WILL TELL MYSQL TO INSERT THE DATA FROM THE EMAIL INTO YOUR MYSQL TABLE
        $sql = "INSERT INTO $db_table(`caption`, `snippet`, `content`, `when`, `uri`, `tags`, `categories`, `DATE`) values ('$subject', '$snippet', '$content', '$when', '$uri', '$tags', '$categories', '$when')";
        if($result = mysql_query($sql ,$db)) {
        } else {
            echo "ERROR: ".mysql_error();
        }
        echo "<h1>News Article added!</h1>";
    }

else {
    echo "<h3>'News Update</h3>not found in e-mail message!";
}

最佳答案

将你的函数移到上面并调用它:

function NewsUpdate ($open_email_msg) {
    //function declaration
}

//Test code for email message.

$open_email_msg =  file_get_contents('emailmessage.html');

//A script that searches the e-mail for the string News Update, and if it is found it will start the function NewsUpdate

if(strpos($open_email_msg,"News Update"))
    NewsUpdate ($open_email_msg);
else {

    echo "<h3>'News Update</h3>not found in e-mail message!";

}

关于php - 使用 PHP 在 if 语句后启动函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598912/

相关文章:

php - 使用 knpmenu bundle 在 symfony 2.1 中创建面包屑

php - 是否可以使用 PHP CLI 获取 *.sh 文件并在 PHP 脚本中访问导出的环境变量?

Mysql:仅连接表左表的第一行

sql - 如何在 MySQL 中执行 FULL OUTER JOIN?

php - 如何使用 PHP 和 MYSQL 从表中获取信息到另一个页面?

PHP:exiftool 的基本用法

php - 使用链接从外部源将数据发布到 mysql DB

php - 使用 .csv 文件替换值

php - 重置 PDO 结果中的数组指针

mysql - 我有一个 mysql 查询问题