- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 PHP 页面,它从 MySQL 查询创建 XML 输出,当它返回一组记录时,它会以 XML 格式很好地显示它。
但是,当它返回多于一组记录时,XML 格式将被删除。但所请求的两个记录集均被返回。
我编辑数据库记录以触发返回一组或多组记录“WHERE wp_hud_display.user_id = 1”,每条记录单独运行时,都会以XML形式清晰显示,因此它不能是一些奇怪的字符在数据库中导致 XML 格式被删除。
PHP 页面
<?php
// create a dom document with encoding utf8
$domtree = new DOMDocument('1.0', 'UTF-8');
//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "";
$config['db_name'] = "tasmanhud";
//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");
//select all items in table
$sql = "SELECT
wp_hud_display.id,
wp_hud_display.name,
wp_hud_display.resolution,
wp_hud_display.user_id,
wp_hud_display.instrument_id_1,
wp_hud_display.x_loc_1,
wp_hud_display.y_loc_1,
wp_hud_display.layer_1,
wp_hud_display.instrument_id_2,
wp_hud_display.x_loc_2,
wp_hud_display.y_loc_2,
wp_hud_display.layer_2,
wp_hud_display.instrument_id_3,
wp_hud_display.x_loc_3,
wp_hud_display.y_loc_3,
wp_hud_display.layer_3,
wp_hud_display.instrument_id_4,
wp_hud_display.x_loc_4,
wp_hud_display.y_loc_4,
wp_hud_display.layer_4,
wp_hud_instrument.inst_id AS inst1_id,
wp_hud_instrument.name AS inst1_name,
wp_hud_instrument.image_file AS inst1_image,
wp_hud_instrument.font_type AS inst1_ft,
wp_hud_instrument.font_size AS inst1_fs,
wp_hud_instrument_1.inst_id AS inst2_id,
wp_hud_instrument_1.name AS inst2_name,
wp_hud_instrument_1.image_file AS inst2_image,
wp_hud_instrument_1.font_type AS inst2_ft,
wp_hud_instrument_1.font_size AS inst2_fs,
wp_hud_instrument_2.inst_id AS inst3_id,
wp_hud_instrument_2.name AS inst3_name,
wp_hud_instrument_2.image_file AS inst3_image,
wp_hud_instrument_2.font_type AS inst3_ft,
wp_hud_instrument_2.font_size AS inst3_fs,
wp_hud_instrument_3.inst_id AS inst4_id,
wp_hud_instrument_3.name AS inst4_name,
wp_hud_instrument_3.image_file AS inst4_image,
wp_hud_instrument_3.font_type AS inst4_ft,
wp_hud_instrument_3.font_size AS inst4_fs
FROM wp_hud_display
LEFT JOIN wp_hud_instrument
ON wp_hud_display.instrument_id_1 = wp_hud_instrument.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_1
ON wp_hud_display.instrument_id_2 = wp_hud_instrument_1.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_2
ON wp_hud_display.instrument_id_3 = wp_hud_instrument_2.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_3
ON wp_hud_display.instrument_id_4 = wp_hud_instrument_3.inst_id
WHERE wp_hud_display.user_id = 1
GROUP BY wp_hud_display.id";
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("wp_hud_displays");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$hud_display_group = $domtree->createElement("hud_display_group");
$hud_display_group = $xmlRoot->appendChild($hud_display_group);
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
$hud_display = $domtree->createElement("hud_display");
$hud_display = $hud_display_group->appendChild($hud_display);
$instrument_id_1 = $domtree->createElement("instrument_id_1");
$instrument_id_1 = $hud_display->appendChild($instrument_id_1);
$instrument_id_2 = $domtree->createElement("instrument_id_2");
$instrument_id_2 = $hud_display->appendChild($instrument_id_2);
$instrument_id_3 = $domtree->createElement("instrument_id_3");
$instrument_id_3 = $hud_display->appendChild($instrument_id_3);
$instrument_id_4 = $domtree->createElement("instrument_id_4");
$instrument_id_4 = $hud_display->appendChild($instrument_id_4);
/* you should enclose the following two lines in a cicle */
$hud_display->appendChild($domtree->createElement('id',$result_array['id']));
$hud_display->appendChild($domtree->createElement('name',$result_array['name']));
$hud_display->appendChild($domtree->createElement('resolution',$result_array['resolution']));
$hud_display->appendChild($domtree->createElement('user_id',$result_array['user_id']));
// Instrument 1
$hud_display->appendChild($domtree->createElement('instrument_id_1',$result_array['instrument_id_1']));
$instrument_id_1->appendChild($domtree->createElement('inst_id',$result_array['inst1_id']));
$instrument_id_1->appendChild($domtree->createElement('name',$result_array['inst1_name']));
$instrument_id_1->appendChild($domtree->createElement('image_name',$result_array['inst1_image']));
$instrument_id_1->appendChild($domtree->createElement('font_type',$result_array['inst1_ft']));
$instrument_id_1->appendChild($domtree->createElement('font_size',$result_array['inst1_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_1',$result_array['x_loc_1']));
$hud_display->appendChild($domtree->createElement('y_loc_1',$result_array['y_loc_1']));
$hud_display->appendChild($domtree->createElement('layer_1',$result_array['layer_1']));
// Instrument 2
$hud_display->appendChild($domtree->createElement('instrument_id_2',$result_array['instrument_id_2']));
$instrument_id_2->appendChild($domtree->createElement('inst_id',$result_array['inst2_id']));
$instrument_id_2->appendChild($domtree->createElement('name',$result_array['inst2_name']));
$instrument_id_2->appendChild($domtree->createElement('image_name',$result_array['inst2_image']));
$instrument_id_2->appendChild($domtree->createElement('font_type',$result_array['inst2_ft']));
$instrument_id_2->appendChild($domtree->createElement('font_size',$result_array['inst2_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_2',$result_array['x_loc_2']));
$hud_display->appendChild($domtree->createElement('y_loc_2',$result_array['y_loc_2']));
$hud_display->appendChild($domtree->createElement('layer_2',$result_array['layer_2']));
// Instrument 3
$hud_display->appendChild($domtree->createElement('instrument_id_3',$result_array['instrument_id_3']));
$instrument_id_3->appendChild($domtree->createElement('inst_id',$result_array['inst3_id'])); $instrument_id_3->appendChild($domtree->createElement('name',$result_array['inst3_name']));
$instrument_id_3->appendChild($domtree->createElement('image_name',$result_array['inst3_image']));
$instrument_id_3->appendChild($domtree->createElement('font_type',$result_array['inst3_ft'])); $instrument_id_3->appendChild($domtree->createElement('font_size',$result_array['inst3_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_3',$result_array['x_loc_3']));
$hud_display->appendChild($domtree->createElement('y_loc_3',$result_array['y_loc_3']));
$hud_display->appendChild($domtree->createElement('layer_3',$result_array['layer_3']));
// Instrument 4
$hud_display->appendChild($domtree->createElement('instrument_id_4',$result_array['instrument_id_4']));
$instrument_id_4->appendChild($domtree->createElement('inst_id',$result_array['inst4_id']));
$instrument_id_4->appendChild($domtree->createElement('name',$result_array['inst4_name']));
$instrument_id_4->appendChild($domtree->createElement('image_name',$result_array['inst4_image']));
$instrument_id_4->appendChild($domtree->createElement('font_type',$result_array['inst4_ft']));
$instrument_id_4->appendChild($domtree->createElement('font_size',$result_array['inst4_fs'])); $hud_display->appendChild($domtree->createElement('x_loc_4',$result_array['x_loc_4']));
$hud_display->appendChild($domtree->createElement('y_loc_4',$result_array['y_loc_4']));
$hud_display->appendChild($domtree->createElement('layer_4',$result_array['layer_4']));
}
}
// get the xml printed
echo $domtree->saveXML();
//send the xml header to the browser
header ("Content-Type:text/xml");
?>
一条记录 XML 输出
<wp_hud_displays>
<hud_display_group>
<hud_display>
<instrument_id_1></instrument_id_1>
<instrument_id_2></instrument_id_2>
<instrument_id_3></instrument_id_3>
<instrument_id_4></instrument_id_4>
<id>1</id>
<name>test display</name>
<resolution>1080p</resolution>
<user_id>1</user_id>
<instrument_id_1>1</instrument_id_1>
<x_loc_1>89</x_loc_1>
<y_loc_1>79</y_loc_1>
<layer_1>1</layer_1>
<instrument_id_2>2</instrument_id_2>
<x_loc_2>988</x_loc_2>
<y_loc_2>98</y_loc_2>
<layer_2>1</layer_2>
<instrument_id_3>3</instrument_id_3>
<x_loc_3>89</x_loc_3>
<y_loc_3>90</y_loc_3>
<layer_3>1</layer_3>
<instrument_id_4>4</instrument_id_4>
<x_loc_4>67</x_loc_4>
<y_loc_4>76</y_loc_4>
<layer_4>1</layer_4>
</hud_display>
</hud_display_group>
</wp_hud_displays>
多条记录输出
1test1_gauge001-2_zpsdbe86ca1f.jpg11PID_DISTANCE30100002500101223dfd34412sdf34534056Arail122test1_needle11PID_ENGINE_LOAD211000025200160112025000000002test1_needle11PID_ENGINE_LOAD211000025200160112025000000003new gaugedfsgsdfgfd435430PID_COOLANT_TEMP2434534541023324000000003new gaugedfsgsdfgfd435430PID_COOLANT_TEMP2434534541023324000000004Bla100.jpg2702334395910299000000004Bla100.jpg2702334395910299000000002test1_needle11PID_ENGINE_LOAD211000025200160112025000000001test display1080p118979129889813899014677612dsfdsfsd720p12321342323213224232123143423444342
是否与WHILE循环有关?无法创建另一组“wp_hud_display.id”记录
最佳答案
剥离 XML 标记并仅在第一个之后的记录上留下值会产生非常奇怪的结果。可能是由于 WordPress 可能不再支持已弃用的 mysql()
函数。
考虑使用MySQL PDO在连接上使用 try/catch,迭代 STH 对象。我还添加了一个文档输出供您查看屏幕和文件之间的内容是否不同
此外,您可以利用跨元素的 for 循环
,因为标签名称仅由 1-4 个工具指示器更改:
<?php
// create a dom document with encoding utf8
$domtree = new DOMDocument('1.0', 'UTF-8');
//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "";
$config['db_name'] = "tasmanhud";
//connect to host
try {
$dbh = new PDO("mysql:host=$config['mysql_host'];dbname=$config['db_name']",$config['mysql_user'],$config['mysql_pass']);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//select all items in table
$sql = "SELECT
wp_hud_display.id,
wp_hud_display.name,
wp_hud_display.resolution,
wp_hud_display.user_id,
wp_hud_display.instrument_id_1,
wp_hud_display.x_loc_1,
wp_hud_display.y_loc_1,
wp_hud_display.layer_1,
wp_hud_display.instrument_id_2,
wp_hud_display.x_loc_2,
wp_hud_display.y_loc_2,
wp_hud_display.layer_2,
wp_hud_display.instrument_id_3,
wp_hud_display.x_loc_3,
wp_hud_display.y_loc_3,
wp_hud_display.layer_3,
wp_hud_display.instrument_id_4,
wp_hud_display.x_loc_4,
wp_hud_display.y_loc_4,
wp_hud_display.layer_4,
wp_hud_instrument.inst_id AS inst1_id,
wp_hud_instrument.name AS inst1_name,
wp_hud_instrument.image_file AS inst1_image,
wp_hud_instrument.font_type AS inst1_ft,
wp_hud_instrument.font_size AS inst1_fs,
wp_hud_instrument_1.inst_id AS inst2_id,
wp_hud_instrument_1.name AS inst2_name,
wp_hud_instrument_1.image_file AS inst2_image,
wp_hud_instrument_1.font_type AS inst2_ft,
wp_hud_instrument_1.font_size AS inst2_fs,
wp_hud_instrument_2.inst_id AS inst3_id,
wp_hud_instrument_2.name AS inst3_name,
wp_hud_instrument_2.image_file AS inst3_image,
wp_hud_instrument_2.font_type AS inst3_ft,
wp_hud_instrument_2.font_size AS inst3_fs,
wp_hud_instrument_3.inst_id AS inst4_id,
wp_hud_instrument_3.name AS inst4_name,
wp_hud_instrument_3.image_file AS inst4_image,
wp_hud_instrument_3.font_type AS inst4_ft,
wp_hud_instrument_3.font_size AS inst4_fs
FROM wp_hud_display
LEFT JOIN wp_hud_instrument
ON wp_hud_display.instrument_id_1 = wp_hud_instrument.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_1
ON wp_hud_display.instrument_id_2 = wp_hud_instrument_1.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_2
ON wp_hud_display.instrument_id_3 = wp_hud_instrument_2.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_3
ON wp_hud_display.instrument_id_4 = wp_hud_instrument_3.inst_id
WHERE wp_hud_display.user_id = 1
GROUP BY wp_hud_display.id";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage();
}
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("wp_hud_displays");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$hud_display_group = $domtree->createElement("hud_display_group");
$hud_display_group = $xmlRoot->appendChild($hud_display_group);
while($row = $STH->fetch()) {
$hud_display = $domtree->createElement("hud_display");
$hud_display = $hud_display_group->appendChild($hud_display);
$instrument_id_1 = $domtree->createElement("instrument_id_1");
$instrument_id_1 = $hud_display->appendChild($instrument_id_1);
$instrument_id_2 = $domtree->createElement("instrument_id_2");
$instrument_id_2 = $hud_display->appendChild($instrument_id_2);
$instrument_id_3 = $domtree->createElement("instrument_id_3");
$instrument_id_3 = $hud_display->appendChild($instrument_id_3);
$instrument_id_4 = $domtree->createElement("instrument_id_4");
$instrument_id_4 = $hud_display->appendChild($instrument_id_4);
/* you should enclose the following two lines in a cicle */
$id = $domtree->createElement('id',$row['id']);
$hud_display->appendChild($id);
$name = $domtree->createElement('name',$row['name']);
$hud_display->appendChild($name);
$resolution = $domtree->createElement('resolution',$row['resolution']);
$hud_display->appendChild($resolution);
$user_id = $domtree->createElement('user_id',$row['user_id']);
$hud_display->appendChild($user_id);
// Instruments 1 - 4
for($i = 1; $i <= 4; $i++) {
${'instrument_id_'.$i} = $domtree->createElement('instrument_id_'.$i,$row['instrument_id_'.$i]);
$hud_display->appendChild(${'instrument_id_'.$i});
${'inst'.$i.'_id'} = $domtree->createElement('inst_id',$row['inst'.$i.'_id']);
${'instrument_id_'.$i}->appendChild(${'inst'.$i.'_id');
${'inst'.$i.'_name'} = $domtree->createElement('name',$row['inst'.$i.'_name']);
${'instrument_id_'.$i}->appendChild(${'inst'.$i.'_name'});
${'inst'.$i.'_image'} = $domtree->createElement('image_name',$row['inst'.$i.'_image']);
${'instrument_id_'.$i}->appendChild(${'inst'.$i.'_image'});
${'inst'.$i.'_ft'} = $domtree->createElement('font_type',$row['inst'.$i.'_ft']);
${'instrument_id_'.$i}->appendChild(${'inst'.$i.'_ft'});
${'inst'.$i.'_fs'} = $domtree->createElement('font_size',$row['inst'.$i.'_fs']);
${'instrument_id_'.$i}->appendChild(${'inst'.$i.'_fs'});
${'x_loc_'.$i} = $domtree->createElement('x_loc_'.$i,$row['x_loc_'.$i]);
$hud_display->appendChild(${'x_loc_'.$i});
${'y_loc_'.$i} = $domtree->createElement('y_loc_'.$i,$row['y_loc_'.$i]);
$hud_display->appendChild(${'y_loc_'.$i});
${'layer_'.$i} = $domtree->createElement('layer_'.$i,$row['layer_'.$i]);
$hud_display->appendChild(${'layer_'.$i});
}
}
// get the xml printed
echo $domtree->saveXML();
// saving xml to document
file_put_contents("XML_WP_Output.xml", $domtree->saveXML());
//send the xml header to the browser
header ("Content-Type:text/xml");
// closing db connection
$dbh = null;
?>
关于php - 当返回超过 1 条记录时,xml 格式会被丢弃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32213418/
我使用这个 cmd 应用程序 https://github.com/tokland/youtube-upload 上传 50 个视频后,我收到此错误: [RequestError] Server re
尝试将 docker 容器提交到镜像时出现错误: [root@dockerregistry /]# docker commit da4180ab1536 dockerregistry:5000/myi
我只是想知道这样做会更好吗: if((fd = open(filename, O_RDWR)) == -1) { fprintf(stderr, "open [ %s ]\n", strerror(e
我在我的开发机器(单个笔记本)中使用 Elasticsearch 1.4.4。一切都设置为默认值,因为我从未更改过任何设置。 当我启动它时,我通常会收到以下消息: [2015-10-27 09:38:
我收到错误 Lock wait timeout exceeded;尝试重新启动事务。这是什么原因,如何解决?仅供引用:MySQL 配置文件中的 innodb_lock_wait_timeout = 1
我对 Slack 中的 block 功能有疑问。有人设法构建 3 列而不是 2 列吗? 我凭直觉尝试了以下代码,但它不起作用: { "blocks": [ {
div 中的内容超过由 css 决定的固定大小。 #fixeddiv { position: fixed; margin: auto; max-height: 300px
我想将 EditText 字段限制为 150 个字符,我可以很容易地做到这一点。但是,当用户试图超过该限制时,我还需要通过键入(即第 151 个字符)或粘贴超过 150 个字符来提醒用户。 解决这个问
我目前正在使用此代码并排记录两个窗口: ffmpeg -f gdigrab -framerate 30 -i title="" -f gdigrab -framerate 30 -i title=""
我在几个包含长字符串的单元格上使用嵌套的 SUBSTITUE 函数,并定期更新 SUBSTITUE fx,这导致我将其复制并粘贴到所有需要它的单元格中。问题是,我的 SUBSTITUTE 列表会随着时
我创建了一个标题 div,如下所示:
Here is the demo link 您怎么看,页面中只有 8 个广告可见,但我有 14 个广告。我已阅读有关广告限制的信息 here但不明白是不是我的情况?有人可以给我确切的答案,为什么我看不
我的非常简单的算法 - C 中的快速排序有问题。它非常有效(随机化大约 0.1 秒并检查列表是否已排序)但是当我想要对超过 500k 的元素进行排序时它会崩溃。不幸的是,我需要对它们进行更多排序,因为
我成功解决了一个关于 Hackerrank 的问题,它通过了所有测试用例,但我得到了一个错误,超过了时间限制。我猜如果我优化我的代码它会工作,但我想不出任何方法来使我的代码更有效率。 问题是: 对大小
你会如何用 包围下面的文字3 个反引号 ```使用 tpope 的 Vim Surround . 我所能做的就是 1 个反引号 使用 S`在视觉模式下: 最佳答案 这不是你问的,但这可以在没有环绕的情
我目前有一个模拟账户。我正在尝试为我的雇主使用 SwifType 制作 POC。我们有一个非常大的数据库,每 1 小时索引一次,并创建一个 JSON 文件。我认为与 Elastic 的集成将非常容易,
我为一个大约有 100 到 120 名成员的小型组织维护网站。 每个月,我们都会发送一封电子邮件,通知我们的成员我们将在即将举行的 session 中讨论的主题。 我正在尝试使用我们的网站为我们提供一
这个问题已经有答案了: How to automatically input an array formula as string with more than 255 characters in l
我有一个在 JBoss 6.1 中运行的 JSF 应用程序,它使用内部Tomcat Servlet 容器。 我已经通过apache commons文件上传实现了上传。我想防止上传过大的文件并设置了属性
当我尝试在 PyMySQL 上执行查询时,出现以下错误: pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try rest
我是一名优秀的程序员,十分优秀!