スポンサードリンク

データベースから特定の変数だけを取り出すphpファイル

 

phpMyAdminのデータベースから特定の変数だけを取り出すサンプル。データベース ASOBIBA から変数の categoryが’kouen’だけを抽出。 dbinfo.phpについてはこちら参照下さい。

 

<?php
require("dbinfo.php");
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
$connection=mysql_connect($hostname, $username, $password);
mysql_query("SET NAMES utf8");
if (!$connection) {
  die('Not connected :'.mysql_error());
}
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {die ('Can\'t use db:'.mysql_error());}
$query = "SELECT * FROM ASOBIBA WHERE category ='kouen'";
$result = mysql_query($query);
if (!$result) {
die('Invalid query:'.mysql_error());}
header("Content-type: text/xml");
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendchild($node);
  $newnode->setattribute("linkurl", $row['linkurl']);
  $newnode->setattribute("linkda", $row['linkda']);
  $newnode->setattribute("explanation", $row['explanation']);
  $newnode->setattribute("lat", $row['lat']);
  $newnode->setattribute("lng", $row['lng']);
  $newnode->setattribute("category", $row['category']);
  $newnode->setattribute("name", $row['name']);
}
echo $dom->saveXML();
?>

 

 

スポンサードリンク

Related Posts

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <img localsrc="" alt="">