By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,495 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,495 IT Pros & Developers. It's quick & easy.

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

P: 13
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,$username,$password);
@mysql_select_db("$db") or die('Unable to select database');


function GetWPData ($columnname, $limit) {
$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");
$num = mysql_numrows($q);
$i=0;
while ($i < $num) {
$rows = mysql_result($q,$i,"$columnname");
$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(GetWPData('post_date',$num));
$p_t = RearrangeTime(GetWPData('post_date',$num));
$p_title = GetWPData('post_title',$num);
$text = StringTogether($p_d, $p_t, $p_title);

date_default_timezone_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.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
Oct 3 '07 #1
Share this Question
Share on Google+
4 Replies


P: 13
bump.
has anyone got an answer?
Oct 3 '07 #2

dafodil
100+
P: 392
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.
Oct 3 '07 #3

P: 13
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.
Oct 4 '07 #4

dafodil
100+
P: 392
Replace line 15 and 16 with this code:
Expand|Select|Wrap|Line Numbers
  1. $con = mysql_connect($server,$username,$password);
  2. mysql_select_db($db, $con);
  3.  
Don't forget the line 82:
Expand|Select|Wrap|Line Numbers
  1. mysql_close($con);
  2.  
Oct 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.