| Coding Forum Problems with your code? Let's hear about it. |
04-11-2007, 02:37 PM
|
#1 (permalink)
|
|
Contributing Member
Join Date: 01-02-07
Location: PA, USA
Posts: 194
Latest Blog: None
|
PHP: How'd I mess up this time?
The page I'm debugging can be found here it's supposed to just display a list of posts that were made, I'll worry about ordering/prettiness later, just getting it to work for now. The code for this page is below. I was gonna start working on making the dates appear correctly and worry about how else I was gonna display everything first, so I went to see the default and got a nice surprise, it shows 2, 3, then nothing, and I have 1, 2, 3, as you can see on the main page. So I'm utterly confused as of right now.
PHP Code:
$path = $_SERVER['REQUEST_URI'];
list($b, $blog) = split('[/]', $path);
$blog = strtolower($blog);
$list = "select * from blogs,posts where blogs.username = MD5('$blog') and blogs.blog_num = posts.blog_num";
$tsil = mysql_query($list);
$lnum = mysql_num_rows($tsil);
$top = mysql_fetch_array($tsil);
echo "<h1>".$top[3]."</h1><h2>".$top[4]."</h2>";
for($i =0; $i < $lnum; $i++){
$mfa = mysql_fetch_array($tsil);
echo "<p class=\"all\"><a href=\"index.php?id=".$mfa[6]."\"> - ".$mfa[8]."</p>";
}
__________________
Need a page made? Draw a diagram, I suggest using Paint, show the picture with your post, it'll help a lot more than you think. Other questions? Draw a diagram for that too!
|
|
|
04-11-2007, 02:54 PM
|
#2 (permalink)
|
|
Inactive
Join Date: 09-22-06
Location: Los Angeles
Posts: 678
Latest Blog: None
|
It's an interesting way of getting a query result. How about something like:
Code:
$path = $_SERVER['REQUEST_URI'];
list($b, $blog) = split('[/]', $path);
$blog = strtolower($blog);
$list = "select * from blogs,posts inner join blogs.blog_num where blogs.username = MD5('$blog') and blogs.blog_num = posts.blog_num";
$tsil = mysql_query($list);
$top = mysql_fetch_array($tsil);
echo "<h1>".$top[3]."</h1><h2>".$top[4]."</h2>";
while($mfa=$top) {
echo "<p class=\"all\"><a href=\"index.php?id=".$mfa[6]."\"> - ".$mfa[8]."</p>";
}
?
Also note that mysql_num_rows is one larger than you want. For example, the query returns $row[0], $row[1], $row[2] which equals mysql_num_rows=3 ... so if you stuck with the "for" loop, you'd want to use "for ($i=0;$i< ($lnum-1);$i++)"
|
|
|
04-11-2007, 02:59 PM
|
#3 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 310
Latest Blog: None
|
Try this:
PHP Code:
$blog = md5 (strtolower (str_replace ('/', '', $_SERVER['REQUEST_URI'])));
$query = "select * from blogs, posts where blogs.username = '$blog' and blogs.blog_num = posts.blog_num";
$result = mysql_query($query);
while ($result && $post = mysql_fetch_array ($result)) {
echo '<hr>';
echo "<h1>".$post[3]."</h1><h2>".$post[4]."</h2>";
echo "<p class=\"all\"><a href=\"index.php?id=".$post[6]."\"> - ".$post[8]."</p>";
}
If you want more help, post what your blogs and posts tables look like. I'd also use mysql_fetch_assoc instead of mysql_fetch_array, so that you can use the names of the fields instead of their indices as it's more readable.
|
|
|
04-11-2007, 03:02 PM
|
#4 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 310
Latest Blog: None
|
PHP Code:
while($mfa=$top) { echo "<p class=\"all\"><a href=\"index.php?id=".$mfa[6]."\"> - ".$mfa[8]."</p>"; }
That loop won't stop running until script execution is aborted. :/
|
|
|
04-11-2007, 03:08 PM
|
#5 (permalink)
|
|
Inactive
Join Date: 09-22-06
Location: Los Angeles
Posts: 678
Latest Blog: None
|
Quote:
|
That loop won't stop running until script execution is aborted.
|
Quite right. I mistakenly used the resulting array instead of the mysql resource. Good call.
<edit>
Also ... I think the issue here is with the useage of mysql_fetch_array twice. The first time, it correctly grabs the array. By the time it's called the second time, the array pointer has already been incremented by 1, so it starts on the 2nd row, excluding the first. In order to avoid this, the query must be made a second time, or use: "mysql_data_seek($tsil)" in between invocations of fetch_array to reset the pointer.
</edit>
Last edited by StupidScript : 04-11-2007 at 03:19 PM.
|
|
|
04-11-2007, 03:16 PM
|
#6 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 310
Latest Blog: None
|
Quote:
Originally Posted by StupidScript
It should stop when it reaches the end of array $top. Why wouldn't it?
|
On every loop iteration $top gets reassigned to $mfa, but $top never changes within the loop.
|
|
|
04-11-2007, 03:23 PM
|
#7 (permalink)
|
|
v7n Mentor
Join Date: 11-22-06
Location: Phoenix, AZ
Posts: 1,788
Latest Blog: None
|
Actually, I think exam is right because it is the assignment = rather than the comparison == in the while loop expression. That messes me up all the time as I work in too many different languages and mix and match syntax. Most of the current ones are like that but I have worked in a lot of older languages that didn't differentiate between assignment and comparison when it came to equal signs. 
__________________
Experimenting
|
|
|
04-11-2007, 03:26 PM
|
#8 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 310
Latest Blog: None
|
Quote:
Originally Posted by Taltos
Actually, I think exam is right because it is the assignment = rather than the comparison == in the while loop expression. That messes me up all the time as I work in too many different languages and mix and match syntax. Most of the current ones are like that but I have worked in a lot of older languages that didn't differentiate between assignment and comparison when it came to equal signs. 
|
If you changed it to the comparison operator (==) then the loop would never run, since $top contains an array with the values of the first row returned from the database, and $mfa (made for adsense??) is undefined.
|
|
|
04-11-2007, 03:28 PM
|
#9 (permalink)
|
|
Inactive
Join Date: 09-22-06
Location: Los Angeles
Posts: 678
Latest Blog: None
|
exam is definitely right. I tested it. Since we've moved past my last edit, here it is again:
Also ... I think the issue here is with the useage of mysql_fetch_array twice. The first time, it correctly grabs the array. By the time it's called the second time, the array pointer has already been incremented by 1, so it starts on the 2nd row, excluding the first. In order to avoid this, the query must be made a second time, or use: "mysql_data_seek($tsil)" in between invocations of fetch_array to reset the pointer.
An example using the original code:
Code:
$top = mysql_fetch_array($tsil);
echo "<h1>".$top[3]."</h1><h2>".$top[4]."</h2>";
mysql_data_seek($tsil);
for($i =0; $i < $lnum; $i++){
$mfa = mysql_fetch_array($tsil);
echo "<p class=\"all\"><a href=\"index.php?id=".$mfa[6]."\"> - ".$mfa[8]."</p>";
}
It's okay if the array pointer keeps advancing during the loop, but it needed to be reset between the first element grab and the loop in order to pick up the first array element.
|
|
|
04-11-2007, 03:33 PM
|
#10 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 310
Latest Blog: None
|
It doesn't have to be that complicated.
PHP Code:
$blog = md5 (strtolower (str_replace ('/', '', $_SERVER['REQUEST_URI'])));
$query = "select * from blogs, posts where blogs.username = '$blog' and blogs.blog_num = posts.blog_num";
$result = mysql_query($query);
$addtitle = true;
while ($result && $post = mysql_fetch_array ($result)) {
if ($addtitle) {
echo "<h1>".$post[3]."</h1><h2>".$post[4]."</h2>";
$addtitle = false;
}
echo "<p class=\"all\"><a href=\"index.php?id=".$post[6]."\"> - ".$post[8]."</p>";
}
|
|
|
04-11-2007, 03:45 PM
|
#11 (permalink)
|
|
Inactive
Join Date: 09-22-06
Location: Los Angeles
Posts: 678
Latest Blog: None
|
Right again, exam.
I would have done something simliar, myself. It was curious how the original loop was being performed, but what the heck. I'm glad to have had a reason to look up what was happening with the array pointer, tho'.
Nice result! 
|
|
|
04-11-2007, 03:49 PM
|
#12 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 310
Latest Blog: None
|
Good job StupidScript! You should change your name to SmartScript 
|
|
|
04-11-2007, 03:52 PM
|
#13 (permalink)
|
|
v7n Mentor
Join Date: 11-22-06
Location: Phoenix, AZ
Posts: 1,788
Latest Blog: None
|
Nice job both of you!
Although I'm still confused about an issue. And maybe it is just the way it came up that creates the confusion. I though the assignment operator used in a conditional spot like on the while loop would always result in a true condition? Is that not the case?
__________________
Experimenting
|
|
|
04-11-2007, 03:59 PM
|
#14 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 310
Latest Blog: None
|
Quote:
Originally Posted by Taltos
Nice job both of you!
Although I'm still confused about an issue. And maybe it is just the way it came up that creates the confusion. I though the assignment operator used in a conditional spot like on the while loop would always result in a true condition? Is that not the case?
|
It depends on the value getting assigned.
PHP Code:
while ($x = 1) { // evalutes to 1, which is true, so the while loop will run
}
while ($x = 0) { // evalutes to 0, which is false, so the while loop will not run
}
while ($x = false) { // evalutes to false, which is true, so the while loop will not run
}
while ($x = 'happy') { // evalutes to 'happy', which is true, so the while loop will run
}
When outputting MySQL results, I usually use something like this (If I'm not using a db wrapper class)
PHP Code:
while ($result && $row = mysql_fetch_assoc ($result)) { // }
If mysql_query had an error or didn't return any rows, it will return false, so $result will be false and the loop won't run, additionally, mysql_fetch_assoc returns the row, and when there aren't any more, it return false, exiting the while loop.
|
|
|
04-11-2007, 05:21 PM
|
#15 (permalink)
|
|
Contributing Member
Join Date: 01-02-07
Location: PA, USA
Posts: 194
Latest Blog: None
|
To soothe the curiosity that seems to abound at times here:
Code:
-- phpMyAdmin SQL Dump
-- version 2.8.0.3
-- http://www.phpmyadmin.net
--
-- Host: ******
-- Generation Time: Apr 12, 2007 at 12:12 AM
-- Server version: 5.0.27
-- PHP Version: 5.2.0
--
-- Database: `******`
--
-- --------------------------------------------------------
--
-- Table structure for table `blogs`
--
CREATE TABLE `blogs` (
`blog_num` int(10) unsigned NOT NULL auto_increment,
`username` varchar(32) NOT NULL,
`password` varchar(40) NOT NULL,
`blog_name` varchar(32) NOT NULL,
`blog_descript` text NOT NULL,
`email` text NOT NULL,
PRIMARY KEY (`blog_num`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `blogs`
--
INSERT INTO `blogs` (`blog_num`, `username`, `password`, `blog_name`, `blog_descript`, `email`) VALUES (1, '68674ee5a02f7e90114f6176d1ca7685', 'c54f8a9785eee6505e3a6b0f5bd0bb3cd9315c15', 'Test', 'Test2', 'admin@arenblogs.com');
-- --------------------------------------------------------
--
-- Table structure for table `posts`
--
CREATE TABLE `posts` (
`post_num` bigint(20) unsigned NOT NULL auto_increment,
`blog_num` int(11) NOT NULL default '0',
`post_date` datetime NOT NULL default '0000-00-00 00:00:00',
`post_title` text NOT NULL,
`post_data` longtext NOT NULL,
PRIMARY KEY (`post_num`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
--
-- Dumping data for table `posts`
--
INSERT INTO `posts` (`post_num`, `blog_num`, `post_date`, `post_title`, `post_data`) VALUES (1, 1, '2007-03-22 20:44:39', 'Example Post', 'This is an example post, delete or edit.'),
(2, 1, '2007-03-22 23:01:11', 'Test Post', 'This is just a test post to see how things go. It is long and long and long and mainly full of air. Air and air and more air and yet more air and even more air. Fluff is a word. My friends all prefer jailbait don''t know why. My female friends prefer BEING jailbait, don''t know why that either. This should be long enough to wrap so I can see what it looks like, but if not then I''ll have to add more later on.'),
(3, 1, '2007-04-10 07:23:13', 'Another Test', 'Mess with Canadians, play with them too, get to have lots of fun. Have fun with the other one too. Hehehe all secret of mine and mine alone. Seriously, not telling.');
That's my mysql DB.
mysql_data_seek($tsil); failed to produce any results at all, Errors are not turned on though.
$mfa also seems to have made you interested in a clearer meaning, mfa stands for none other than mysql_fetch_array. Bet you didn't see THAT one coming.
I'll try just pulling two queries.
__________________
Need a page made? Draw a diagram, I suggest using Paint, show the picture with your post, it'll help a lot more than you think. Other questions? Draw a diagram for that too!
|
|
|
04-11-2007, 07:34 PM
|
#16 (permalink)
|
|
Inactive
Join Date: 09-22-06
Location: Los Angeles
Posts: 678
Latest Blog: None
|
With any MySQL call you can grab errors:
Code:
mysql_data_seek($tsil) or die ("No seek: ".mysql_error());
If it's a MySQL error, you'll see it. For the record:
1) I like exam's final solution.
2) If you're gonna stick with the original code, I like your note about trying 2 queries.
|
|
|
04-11-2007, 07:47 PM
|
#17 (permalink)
|
|
Contributing Member
Join Date: 01-02-07
Location: PA, USA
Posts: 194
Latest Blog: None
| |