Friday, December 26, 2008

Write PHP and MySQL Code on Drupal

Drupal give us ability to write PHP and MySQL script on the content or when we create block. For me this is killer facility by drupal. Until now I don't find the facility like this on other content management system or blog script.

With this ability you can write your PHP with MySQL from web without scripting. You don't need do hardcode on drupal's script file.

So this is little example on how to write PHP and MySQL code on drupal. This script will call latest submitted stories that have been publish.

<ul>
<?php
$query = "select nid, title from node where status = 1 order by nid desc limit 10";
$result = mysql_query($query) or die("Query failed");
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$url = "node/".$line['nid'];
echo "<li><a href=\"".url($url)."\" title=\"".$line['title']."\">".$line['title']."</a></li>";
}
?>
</ul>


If you want to test it or create another PHP and MySQL code, make sure that on Input format you've choose PHP Code and remember you should include tags on every PHP and MySQL that you've writen.

Have a nice try!

1 comment: