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>"; ?> Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.