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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • atyndall
    New Member
    • Sep 2007
    • 13

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

    OK, this is the relevant portion script:
    [PHP]<?php





    $username = '__'; // MySQL Database Username.
    $password = '__'; // MySQL Database Password.
    $server = '__'; // MySQL Database server (most likely localhost).
    $db = '__'; // MySQL Database Name.
    $num = 4; // Number of Wordpress Articles to show.
    $tz = '__'; // Your Timezone written using the list at http://php.net/timezones


    mysql_connect($ server,$usernam e,$password);
    @mysql_select_d b("$db") or die('Unable to select database');


    function GetWPData ($columnname, $limit) {
    $q = mysql_query("SE LECT `post_date`, `post_title` FROM `wp_posts` WHERE (post_type = 'post' AND post_status = 'publish') ORDER BY `wp_posts`.`pos t_date` DESC LIMIT 0,$limit");
    $num = mysql_numrows($ q);
    $i=0;
    while ($i < $num) {
    $rows = mysql_result($q ,$i,"$columnnam e");
    $array["$i"] = $rows;
    $i++;
    }
    return $array;
    }

    function RearrangeDate ($var) {
    $i=0;
    foreach ($var as $td) {
    $date = substr($td, 0, 10);
    $y = substr($date, 0, 4);
    $M = substr($date, 5, 2);
    $d = substr($date, 8, 2);

    $format = "$d/$M/$y";
    // $d = day, $M = month, $y = year.

    $array["$i"] = $format;
    $i++;
    }
    return $array;
    }

    function RearrangeTime ($var) {
    $i=0;
    foreach ($var as $td) {
    $time = substr($td, 11, 8);
    $h = substr($time, 0, 2);
    $m = substr($time, 3, 2);
    $s = substr($time, 6, 2);

    $format = "$h:$m";
    // $h = hours, $m = minutes, $s = seconds.

    $array["$i"] = $format;
    $i++;
    }
    return $array;
    }

    function StringTogether ($d_arr, $t_arr, $title_arr) {
    $i=0;
    while ($i < $num) {
    $d = $d_arr["$i"];
    $t = $t_arr["$i"];
    $title = $title_arr["$i"];

    $format = "$d $t - $title";
    // $d = date, $t = time, $title = title

    $array["$i"] = $format;
    $i++;
    }
    $text = implode('<br>', $array);
    return $text;
    }

    mysql_close();

    $p_d = RearrangeDate(G etWPData('post_ date',$num));
    $p_t = RearrangeTime(G etWPData('post_ date',$num));
    $p_title = GetWPData('post _title',$num);
    $text = StringTogether( $p_d, $p_t, $p_title);

    date_default_ti mezone_set("$tz ");
    $lt = localtime(time( ), true);
    $h = $localtime[tm_hour];
    $m = $localtime[tm_min];
    $s = $localtime[tm_sec];
    $ld = date($ldformat) ;

    $lt_format = "$h:$m"; // Local Time Format
    // $h = hours, $m = minutes, $s = seconds.
    $ld_format = "d/m/Y"; // Local Date Format
    // d = days, m = months, Y = years.
    $td_format = "$ld $lt"; // Time-Date Format
    // $ld = date, $lt = time.

    $td=$td_format;[/PHP]
    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.implod e]: 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
  • atyndall
    New Member
    • Sep 2007
    • 13

    #2
    bump.
    has anyone got an answer?

    Comment

    • dafodil
      Contributor
      • Jul 2007
      • 389

      #3
      You cannot connect to the database if your password or username is wrong. Try to remove the password and see if you can get rid of the password error. In that way you'll see if there are any errors in your code.

      Comment

      • atyndall
        New Member
        • Sep 2007
        • 13

        #4
        i can access mysql through phpmyadmin using th same user/pass combo I used in the script. I don't quite understand what you are trying to say.

        Comment

        • dafodil
          Contributor
          • Jul 2007
          • 389

          #5
          Replace line 15 and 16 with this code:
          [code=php]
          $con = mysql_connect($ server,$usernam e,$password);
          mysql_select_db ($db, $con);
          [/code]

          Don't forget the line 82:
          [code=php]
          mysql_close($co n);
          [/code]

          Comment

          Working...