
working code for listing unique values of custom field
By:
rakeshr on
Feb 14th, 2012 | syntax:
PHP | size: 1.78 KB | hits: 107 | expires: Never
<h1>Judges</h1>
<?php
// The variables have not been adequately sanitized to protect against SQL Injection attacks: http://us3.php.net/mysql_real_escape_string
$hostname = "localhost";
$username = "wp_test";
$password = "45455c45hbg";
$database = "wp_test";
$link = mysql_connect("$hostname", "$username", "$password");
if (!$link) {
echo "<p>Could not connect to the server '" . $hostname . "'</p>\n";
echo mysql_error();
}else{
// printf("MySQL client info: %s\n", mysql_get_client_info());
// printf("MySQL host info: %s\n", mysql_get_host_info());
// printf("MySQL server version: %s\n", mysql_get_server_info());
// printf("MySQL protocol version: %s\n", mysql_get_proto_info());
}
if ($link && !$database) {
echo "<p>No database name was given. Available databases:</p>\n";
$db_list = mysql_list_dbs($link);
echo "<pre>\n";
while ($row = mysql_fetch_array($db_list)) {
echo $row['Database'] . "\n";
}
echo "</pre>\n";
}
if ($database) {
$dbcheck = mysql_select_db("$database");
if (!$dbcheck) {
echo mysql_error();
}else{
// Check tables
$sql = "SELECT Distinct `meta_value` FROM `wp_postmeta` WHERE `meta_key` LIKE 'judge' LIMIT 0, 30 ";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
echo "<ul>";
while ($row = mysql_fetch_row($result)) { ?>
<li><a href="<?php echo esc_url( home_url( '/' ) ); ?>judge/<?php echo "{$row[0]}"; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php echo "{$row[0]}\n";
echo "</a>";
echo "</li>";
}
echo "</ul>";
} else {
echo "<p>The database '" . $database . "' contains no tables.</p>\n";
echo mysql_error();
}
}
}
?>