我正在尝试使用php将mysql数据库中的项目传递到xml文件中。我有php代码创建xml文件。但是传递给它的值不是来自mysql数据库的值。该数据库有13个cols,有62行。我的foreach语句中有五个项目,当它们显示在Web屏幕上时,值输出如下:
找到的物业数量:62
1 1 1 1 1 1 1 1 1 1 1 1 1 1 R R R R R E E E E W W W W A A A A A h h h h h 1 1 1 1 1< < < < < 2 2 2 2 2 1 1 1 1 1 1 1 1 1
上面一行中有65个项目,这是我的foreach语句中的5个项目乘以数据库中的13个cols。我认为这与它有关。
以下是我的代码:
<?php
@$db = new mysqli('localhost', 'root', '', 'siamsatire');
if (mysqli_connect_errno()) {
echo 'error connecting to db';
exit;
}
$query = "SELECT * from events";
$result = $db->query($query);
$num_results = $result->num_rows;
echo 'Number of properties found : <strong>' . $num_results . '</strong><br><br>';
for ($i=0; $i < $num_results; $i++) {
$row = $result->fetch_object();
$name = $row->name;
$subtitle = $row->sub_title;
$date = $row->display_date;
$description = $row->slug;
$photo= $row->photo;
$thumb= $row->thumb;
/*echo '<tr>';
echo "<td>$name</td>";
echo "<td>$subtitle</td>";
echo "<td>$date</td>";
echo "<td>$description</td>";
echo "<td>$photo</td>";
echo "<td>$thumb</td>";1+0
echo '<tr>';*/
}
$doc = new DOMDocument("1.0");
$doc->formatOutput = true;
$r = $doc->createElement("events");
$doc->appendChild( $r );
foreach($row as $fieldvalue)
{
$b = $doc->createElement( "event" );
$name1 = $doc->createElement( "title" );
$name1->appendChild( $doc->createTextNode( $fieldvalue['title'] ));
$b->appendChild( $name1 );
$subtitle1 = $doc->createElement( "subtitle" );
$subtitle1->appendChild($doc->createTextNode( $fieldvalue['subtitle'] ));
$b->appendChild( $subtitle1 );
$date1 = $doc->createElement( "display_date" );
$date1->appendChild($doc->createTextNode( $fieldvalue['display_date'] ));
$b->appendChild( $date1 );
$description1 = $doc->createElement( "slug" );
$description1->appendChild( $doc->createTextNode( $fieldvalue['slug'] ));
$b->appendChild( $description1 );
$photo1 = $doc->createElement( "photo" );
$photo1->appendChild( $doc->createTextNode( $fieldvalue['photo'] ) );
$b->appendChild( $photo1 );
$thumb1 = $doc->createElement( "thumb" );
$thumb1->appendChild( $doc->createTextNode( $fieldvalue['thumb'] ) );
$b->appendChild( $thumb1 );
$r->appendChild( $b );
}
echo $doc->saveXML();
$doc->save("write.xml");
$result->free();
$db->close();
?>
有没有人对我做错了什么有任何想法?
@starx - 我根据您的代码更改了我的代码,这就是现在的样子。
<?php
@$db = new mysqli( 'localhost', 'root', '', 'siamsatire');
if (mysqli_connect_errno()) {
echo 'error connecting to db';
exit;
}
$query = "SELECT * from events";
$result = mysql_query($query);
if(mysql_num_rows($result)) {
$doc = new DOMDocument("1.0");
$doc->formatOutput = true;
while($row = mysql_fetch_assoc($result)) {
$r = $doc->createElement( "events" );
foreach($row as $field=>$value) {
$tChild = $doc->createElement( $field );
$tChild->appendChild( $doc->createTextNode($value) );
$r->appendChild( $tChild );
}
$doc->appendChild($r);
}
$doc->appendChild( $r );
echo $doc->saveXML();
$doc->save("write.xml");
}
//$result->free();
//$db->close();
?>
这些是我得到的错误。'
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\siamsatire1.php on line 12
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\siamsatire1.php on line 12
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\siamsatire1.php on line 14'
你知道我为什么得到它们吗?
然后我将mysql_query更改为mysqli_query,将错误减少到:
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\siamsatire1.php on line 12
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\siamsatire1.php on line 14
答案 0 :(得分:1)
这是一个更好,更正确的解决方案
$query = "SELECT * from events";
$result = mysql_query($query);
if(mysql_num_rows($result)) {
$doc = new DOMDocument("1.0");
$doc->formatOutput = true;
while($row = mysql_fetch_assoc($result)) {
$r = $doc->createElement( "events" );
foreach($row as $field=>$value) {
$tChild = $doc->createElement( $field );
$tChild->appendChild( $doc->createTextNode($value) );
$r->appendChild( $tChild );
}
$doc->appendChild($r);
}
$doc->appendChild( $r );
echo $doc->saveXML();
$doc->save("write.xml");
}
如果需要,您可以将上述代码与您的库集成。
以下是使用mysqli
的工作解决方案<?
@$db = new mysqli( 'localhost', 'root', '', 'siamsatire');
if (mysqli_connect_errno()) {
echo 'error connecting to db';
exit;
}
$query = "SELECT * from events";
$result = mysqli_query($db,$query);
if(mysqli_num_rows($result)) {
$doc = new DOMDocument("1.0");
$doc->formatOutput = true;
while($row = mysqli_fetch_assoc($result)) {
$r = $doc->createElement( "events" );
foreach($row as $field=>$value) {
$tChild = $doc->createElement( $field );
$tChild->appendChild( $doc->createTextNode($value) );
$r->appendChild( $tChild );
}
$doc->appendChild($r);
}
$doc->appendChild( $r );
echo $doc->saveXML();
$doc->save("write.xml");
}
//$result->free();
//$db->close();
?>