advancedfuture Posted April 21, 2008 Share Posted April 21, 2008 I'm having a problem with my code. It queries the database then I use a while loop to print the rows out.. the problem is it is echoing each row 2x? What am I doing wrong? <? include 'connection.php'; //GET URL DATA FROM THE URL $city = htmlspecialchars($_GET['city']); $state = htmlspecialchars($_GET['state']); $query = "SELECT * FROM Listings WHERE city ='$city' AND state ='$state'"; $results = mysql_query($query); //START PAGE HTML HEADERS echo "<html><head>"; //STAR WHILE LOOP TO CYCLE THROUGH ALL THE RESULTS FROM THE QUERY. //SPIT OUT EACH RESULE ON THEIR OWN LINE. while($rows = mysql_fetch_array($results)) { $idnumber = $rows['idnumber']; $studioprice = $rows['studioprice']; $price3 = $rows['price3']; echo "Property Id Number ".$idnumber."<br>"; echo "1 Bedroom - 3 bedroom Ranging From".$studioprice." - ".$price3."<br><br>"; } //CLOSE THE HTML PAGE echo "</html></head>"; ?> Link to comment https://forums.phpfreaks.com/topic/102237-while-loop-echoing-data-twice/ Share on other sites More sharing options...
dptr1988 Posted April 21, 2008 Share Posted April 21, 2008 Are you sure that you don't have duplicate data in the database? I don't see any reason why the code would be printing out duplicate data. Link to comment https://forums.phpfreaks.com/topic/102237-while-loop-echoing-data-twice/#findComment-523438 Share on other sites More sharing options...
hitman6003 Posted April 22, 2008 Share Posted April 22, 2008 Gonna have to agree with dptr...check the number of results returned: //START PAGE HTML HEADERS echo "<html><head>"; echo mysql_num_results($results); //STAR WHILE LOOP TO CYCLE THROUGH ALL THE RESULTS FROM THE QUERY. Link to comment https://forums.phpfreaks.com/topic/102237-while-loop-echoing-data-twice/#findComment-523443 Share on other sites More sharing options...
kenrbnsn Posted April 22, 2008 Share Posted April 22, 2008 The function mysql_fetch_array() returns two rows for each row in your database. One is indexed numerically, the other is indexed associatively. You should use the mysql_fetch_assoc() function instead. Ken Link to comment https://forums.phpfreaks.com/topic/102237-while-loop-echoing-data-twice/#findComment-523483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.