Advertisement
rakeshr

working code for listing unique values of custom field

Feb 14th, 2012
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1.     <h1>Judges</h1>          
  2.          
  3.           <?php
  4.  
  5. // The variables have not been adequately sanitized to protect against SQL Injection attacks: http://us3.php.net/mysql_real_escape_string
  6.  
  7.     $hostname = "localhost";
  8.     $username = "wp_test";
  9.     $password = "45455c45hbg";
  10.     $database = "wp_test";
  11.  
  12.     $link = mysql_connect("$hostname", "$username", "$password");
  13.         if (!$link) {
  14.             echo "<p>Could not connect to the server '" . $hostname . "'</p>\n";
  15.             echo mysql_error();
  16.         }else{
  17. //          printf("MySQL client info: %s\n", mysql_get_client_info());
  18. //          printf("MySQL host info: %s\n", mysql_get_host_info());
  19. //          printf("MySQL server version: %s\n", mysql_get_server_info());
  20. //          printf("MySQL protocol version: %s\n", mysql_get_proto_info());
  21.         }
  22.     if ($link && !$database) {
  23.         echo "<p>No database name was given. Available databases:</p>\n";
  24.         $db_list = mysql_list_dbs($link);
  25.         echo "<pre>\n";
  26.         while ($row = mysql_fetch_array($db_list)) {
  27.             echo $row['Database'] . "\n";
  28.         }
  29.         echo "</pre>\n";
  30.     }
  31.     if ($database) {
  32.     $dbcheck = mysql_select_db("$database");
  33.         if (!$dbcheck) {
  34.             echo mysql_error();
  35.         }else{
  36.             // Check tables
  37.             $sql = "SELECT Distinct `meta_value` FROM `wp_postmeta` WHERE `meta_key` LIKE 'judge' LIMIT 0, 30 ";
  38.             $result = mysql_query($sql);
  39.             if (mysql_num_rows($result) > 0) {
  40.                 echo "<ul>";
  41.                 while ($row = mysql_fetch_row($result)) { ?>
  42.                     <li><a href="<?php echo esc_url( home_url( '/' ) ); ?>judge/<?php echo "{$row[0]}"; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
  43.                     <?php echo "{$row[0]}\n";
  44.                     echo "</a>";
  45.                     echo "</li>";
  46.                 }
  47. echo "</ul>";
  48.             } else {
  49.                 echo "<p>The database '" . $database . "' contains no tables.</p>\n";
  50.                 echo mysql_error();
  51.             }
  52.         }
  53.     }
  54.  
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement