Jump to content

mysql_query(), mysql_numrows(), foreach() and implode() problems


atyndall

Recommended Posts

OK, this is the relevant portion script:

 

   7.
      $username = '__'; // MySQL Database Username.
   8.
      $password = '__'; // MySQL Database Password.
   9.
      $server = '__'; // MySQL Database server (most likely localhost).
  10.
      $db = '__'; // MySQL Database Name.
  11.
      $num = 4; // Number of Wordpress Articles to show.
  12.
      $tz = '__'; // Your Timezone written using the list at http://php.net/timezones
  13.
       
  14.
       
  15.
      mysql_connect($server,$username,$password);
  16.
      @mysql_select_db("$db") or die('Unable to select database');
  17.
       
  18.
       
  19.
      function GetWPData ($columnname, $limit) {
  20.
      $q = mysql_query("SELECT `post_date`, `post_title` FROM `wp_posts` WHERE (post_type = 'post' AND post_status = 'publish') ORDER BY `wp_posts`.`post_date` DESC LIMIT 0,$limit");
  21.
      $num = mysql_numrows($q);
  22.
      $i=0;
  23.
      while ($i < $num) {
  24.
      $rows = mysql_result($q,$i,"$columnname");
  25.
      $array["$i"] = $rows;
  26.
      $i++;
  27.
      }
  28.
      return $array;
  29.
      }
  30.
       
  31.
      function RearrangeDate ($var) {
  32.
      $i=0;
  33.
      foreach ($var as $td) {
  34.
      $date = substr($td, 0, 10);
  35.
      $y = substr($date, 0, 4);
  36.
      $M = substr($date, 5, 2);
  37.
      $d = substr($date, 8, 2);
  38.
       
  39.
      $format = "$d/$M/$y";
  40.
      // $d = day, $M = month, $y = year.
  41.
       
  42.
      $array["$i"] = $format;
  43.
      $i++;
  44.
      }
  45.
      return $array;
  46.
      }
  47.
       
  48.
      function RearrangeTime ($var) {
  49.
      $i=0;
  50.
      foreach ($var as $td) {
  51.
      $time = substr($td, 11, ;
  52.
      $h = substr($time, 0, 2);
  53.
      $m = substr($time, 3, 2);
  54.
      $s = substr($time, 6, 2);
  55.
       
  56.
      $format = "$h:$m";
  57.
      // $h = hours, $m = minutes, $s = seconds.
  58.
       
  59.
      $array["$i"] = $format;
  60.
      $i++;
  61.
      }
  62.
      return $array;
  63.
      }
  64.
       
  65.
      function StringTogether ($d_arr, $t_arr, $title_arr) {
  66.
          $i=0;
  67.
          while ($i < $num) {
  68.
              $d = $d_arr["$i"];
  69.
              $t = $t_arr["$i"];
  70.
              $title = $title_arr["$i"];
  71.
             
  72.
              $format = "$d $t - $title";
  73.
              // $d = date, $t = time, $title = title
  74.
       
  75.
              $array["$i"] = $format;
  76.
              $i++;
  77.
          }
  78.
          $text = implode('<br>', $array);
  79.
          return $text;
  80.
      }
  81.
       
  82.
      mysql_close();
  83.
       
  84.
      $p_d = RearrangeDate(GetWPData('post_date',$num));
  85.
      $p_t = RearrangeTime(GetWPData('post_date',$num));
  86.
      $p_title = GetWPData('post_title',$num);
  87.
      $text = StringTogether($p_d, $p_t, $p_title);
  88.
       
  89.
      date_default_timezone_set("$tz");
  90.
      $lt = localtime(time(), true);
  91.
      $h = $localtime[tm_hour];
  92.
      $m = $localtime[tm_min];
  93.
      $s = $localtime[tm_sec];
  94.
      $ld = date($ldformat);
  95.
       
  96.
      $lt_format = "$h:$m"; // Local Time Format
  97.
      // $h = hours, $m = minutes, $s = seconds.
  98.
      $ld_format = "d/m/Y"; // Local Date Format
  99.
      // d = days, m = months, Y = years.
100.
      $td_format = "$ld $lt"; // Time-Date Format
101.
      // $ld = date, $lt = time.
102.
       
103.
      $td=$td_format;

 

 

when I run it I get the following errors:

 

Warning: mysql_query() [function.mysql-query]: Access denied for user '__'@'__' (using password: NO) in __ on line 20

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be __ on line 20

 

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in __ on line 21

 

Warning: Invalid argument supplied for foreach() in __ on line 33

 

Warning: mysql_query() [function.mysql-query]: Access denied for user '__'@'__' (using password: NO) in __ on line 20

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in __ on line 20

 

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in __ on line 21

 

Warning: Invalid argument supplied for foreach() in __ on line 50

 

Warning: mysql_query() [function.mysql-query]: Access denied for user '__'@'__' (using password: NO) in __ on line 20

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in __ on line 20

 

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in __ on line 21

 

Warning: implode() [function.implode]: Bad arguments. in __ on line 78

 

What needs fixing in my script to get everything working? The MySQL username and password were entered and correctly spelt despite what the error messages say and have only been removed for security reasons.

 

Can anyone help this newbie programmer?

Thanks :D

atyndall93

Link to comment
Share on other sites

Warning: mysql_query() [function.mysql-query]: Access denied for user '__'@'__' (using password: NO) in __ on line 20

 

Your not using a password when connecting..

 

also you have no varible set try this#

 

 

$link = mysql_connect($server,$username,$password);

mysql_select_db($db, $link) or die('Unable to select database');

 

I cant work out why your password isn't being sent tho.

 

Regards

Liam

Link to comment
Share on other sites

But I am using a password, thats whats weird, ive just removed it (for obvious reasons)

 

EDIT: I tried what you said, didnt change anything exact same.

 

EDIT: And when I enter in a random non-existant username but same password I get this:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ghjgjhg'@'localhost' (using password: YES) in __ on line 13

 

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in __ on line 14

Unable to select database

Link to comment
Share on other sites

have you tried connecting using mysql query browser or somthing else to test that your username and password are accepted? How have you checked that you can login to know username and password are correct?

 

I say the above but that still doesnt explain why your mysql error is saying that no password has been sent.

 

Regards

Liam

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.