使用 PHP 从 XML 到 MySQL [英] XML to MySQL using PHP

查看:46
本文介绍了使用 PHP 从 XML 到 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 XML 中获取这 4 个不同的属性并将它们转换为表数据.我已经设置了一个循环来输入信息.但是,当我尝试运行它时,出现了语法错误,即意外的 T_STRING.我错过了什么?

I'm trying to take these 4 different attributes from my XML and convert them into table data. I've set up a loop to feed the information in. However, I get a syntax error, unexpected T_STRING when I try to run this. What am I missing?

foreach ($xml->machine as $machine)
{
   "INSERT INTO 'table' (id, filePointer, amount, description) VALUES ('%s', '%s', '%s','%s')",
    mysql_real_escape_string('$machine->id'),
    mysql_real_escape_string('$machine->images->image->filePointer'),
    mysql_real_escape_string('$machine->advertised_price->amount'),
mysql_real_escape_string('$machine>description'))
}

推荐答案

让我们看看 mysqli 会是什么样子....

Let's see what this might look like with mysqli....

$db = new mysqli(hostname, username, password, database);
$stmt = $db->prepare("INSERT INTO table (id, filePointer, amount, description) VALUES (?, ?, ?, ?)");
) 
foreach ($xml->machine as $machine) {
    $stmt->bind_param('ssss', $machine->id, $machine->images->image->filePointer, $machine->advertised_price->amount, $machine->description);
    // Execute the query
    $stmt->execute();

}
$stmt->close();

这篇关于使用 PHP 从 XML 到 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆