2x the results?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Zaroth
Forum Newbie
Posts: 5
Joined: Sat Jun 12, 2010 8:37 pm

2x the results?

Post by Zaroth »

Why is there 2x the results with this code? I only have 6 rows, it repeats them 2x.. look at the code and output.
Any idea what's going on and how to fix this?
Thanks in advance :)

Code:

Code: Select all

<?php // query.php
$db_hostname = '127.0.0.1';
$db_database = 'lab6';
$db_username = 'root';

require_once 'lab-exercise10-5.php';
$db_server = mysql_connect($db_hostname, $db_username);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database) or die("Unable to select database: " . mysql_error());

$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());

for ($j = 0 ; $j < $rows ; ++$j)//Why is it displaying double the results?...
{
	$row = mysql_fetch_row($result);
	echo 'Author: ' .	$row[0] .  '<br />';
	echo 'Title: ' .	$row[1] . '<br />';
	echo 'Category: ' .	$row[2] . '<br />';
	echo 'Year: ' .		$row[3] . '<br />';
	echo 'ISBN: ' .		$row[4] . '<br /><br />';
}
?>
Output:

Code: Select all

Author: Mark Twain
Title: The Adventures of Tom Sawyer
Type: Fiction
Year: 1876
Pages: 100
ISBN: 9781598184891

Author: Jane Austen
Title: Pride and Prejudice
Type: Fiction
Year: 1811
Pages: 124
ISBN: 9780582506206

Author: Charles Darwin
Title: The Origin of Species
Type: Non-Fiction
Year: 1856
Pages: 95
ISBN: 9780517123201

Author: Charles Dickens
Title: The Old Curiosity Shop
Type: Fiction
Year: 1841
Pages: 134
ISBN: 9780099533474

Author: William Shakespeare
Title: Romeo and Juliet
Type: Play
Year: 1594
Pages: 123
ISBN: 9780192814968

Author: Test Testing
Title: Another Test
Type: Test
Year: 1337
Pages: 666
ISBN: 9780192814967

Author: Mark Twain
Title: The Adventures of Tom Sawyer
Category: Fiction
Year: 100
ISBN: 1876

Author: Jane Austen
Title: Pride and Prejudice
Category: Fiction
Year: 124
ISBN: 1811

Author: Charles Darwin
Title: The Origin of Species
Category: Non-Fiction
Year: 95
ISBN: 1856

Author: Charles Dickens
Title: The Old Curiosity Shop
Category: Fiction
Year: 134
ISBN: 1841

Author: William Shakespeare
Title: Romeo and Juliet
Category: Play
Year: 123
ISBN: 1594

Author: Test Testing
Title: Another Test
Category: Test
Year: 666
ISBN: 1337

User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: 2x the results?

Post by AbraCadaver »

Given the code shown it shouldn't execute at all because $rows is not defined. Must be something in lab-exercise10-5.php.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: 2x the results?

Post by Phoenixheart »

Also, as mysql_fetch_ functions move the internal pointer ahaed, there's no need of using a for() loop. Try

Code: Select all

while ($row = mysql_fetch_row($result))
{
    // echo here
}
and see the output.
Post Reply