472,126 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

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,$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
4 5005
bump.
has anyone got an answer?
Oct 3 '07 #2
dafodil
392 256MB
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
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
392 256MB
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.

Similar topics

5 posts views Thread by Andreas Paasch | last post: by
4 posts views Thread by Bruce A. Julseth | last post: by
6 posts views Thread by Rob | last post: by
5 posts views Thread by lkrubner | last post: by
2 posts views Thread by The.Relinator | last post: by
18 posts views Thread by NoWhereMan | last post: by
6 posts views Thread by tokcy | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.