在 Drupal MySQL 查询语法 摘录

论坛: 

一个简单例子
<?php
$query = 'SELECT org, dept, addr, addr2, city_st_zip, phone, url, email FROM {resources}';
$result =  db_query($query);
  while ($row = db_fetch_object($result)) {
  $org="{$row->org}";
  $dept="{$row->dept}";
  $addr="{$row->addr}";
  $addr2="{$row->addr2}";
  $city_st_zip="{$row->city_st_zip}";
  $phone="{$row->phone}";
  $url="{$row->url}"; 
  $email="{$row->email}";
  $url_pr=str_replace("http://","",$url);
  echo "<strong>".$org."</strong><br>\n";
    if($dept!=""){ echo $dept."<br>\n"; }
    if($addr!=""){ echo $addr."<br>\n"; }
    if($addr2=""){ echo $addr2."<br>\n"; }
    if($city_st_zip!=""){ echo $city_st_zip."<br>\n"; }
    if($phone!=""){ echo $phone."<br>\n"; }
    if($url!=""){ echo "<A href="/.$url.">".$url_pr."</A><br>\n"; }
    if($email!=""){ echo "Email: <A href=mailto:".$email.">".$email."</A><br>\n"; }
  echo "<br>\n";                
  }
?>

<?php
$sql = 'SELECT title,url FROM {links}';  // Add more fields here or use * for all
$results = db_query($sql); // Run the query
while ($fields = db_fetch_array($results)) { // Get the next result as an associative array
  echo "<pre>"; // Wrap each record in a pre tag for clarity
  foreach($fields as $key => $value) { // Iterate over all of the fields in this row
    echo "$key = $value\n";
  }
  echo "</pre>";
}
?>

dashan 答复于