carter90 Posted April 24, 2013 Share Posted April 24, 2013 (edited) I have this code below which is linked to my mysql database. For some reason it is only showing the price for the if part of the statement? <html> <body> <?php $mysqli = new mysqli("localhost", "root", "", "insuredcars"); if ($_POST['formcar'] == '1' && $_POST['formage'] == '18' && $_POST['formNCD'] == '0' && $_POST['formPoints'] == '0' ) { $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'"); while($row = mysqli_fetch_assoc($query)) { echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; } echo "</table>"; } elseif ($_POST['formcar'] == '2' && $_POST['formage'] == '18' && $_POST['formNCD'] == '0' && $_POST['formPoints'] == '0' ) { $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '2'"); echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; echo "</table>"; } elseif ($_POST['formcar'] == '3' && $_POST['formage'] == '18' && $_POST['formNCD'] == '0' && $_POST['formPoints'] == '0' ) { $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '5'"); echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; echo "</table>"; } elseif ($_POST['formcar'] == '4' && $_POST['formage'] == '18' && $_POST['formNCD'] == '0' && $_POST['formPoints'] == '0' ) { $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '6'"); echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; echo "</table>"; } ?> </body> </html> Edited April 24, 2013 by carter90 Quote Link to comment Share on other sites More sharing options...
Barand Posted April 24, 2013 Share Posted April 24, 2013 Why is that surprising? There are only if statements, not an else in sight. Quote Link to comment Share on other sites More sharing options...
carter90 Posted April 24, 2013 Author Share Posted April 24, 2013 Sorry I was messing around trying to see if it would work with just if statements forgot I had changed it. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted April 24, 2013 Share Posted April 24, 2013 You never retrieve the results of the query in any of the ELSEIF blocks. mysqli_query only executes the query, you need to use mysqli_fetch_assoc after that to get the data. And, by the way, if are only selecting one row and only processing one row, you do not need the while loop. You can just do the fetch. Quote Link to comment Share on other sites More sharing options...
carter90 Posted April 24, 2013 Author Share Posted April 24, 2013 Thanks for that I have added the code in below the elseif statements: I wasn't sure what put instead of while as it comes up with a syntax error if I just take it out. while ($row = mysql_fetch_assoc($query)) Unfortunately now it is coming up with an error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in search.php on line 9 Sorry if I am being an idiot. Quote Link to comment Share on other sites More sharing options...
Solution DavidAM Posted April 24, 2013 Solution Share Posted April 24, 2013 You are using mysqli in your original code. You can NOT mix mysql calls with that. You must use mysqli_fetch_assoc(). If you are expecting and wanting only ONE row. You just need the $row = mysqli_fetch_assoc($query); without the WHILE () { and, of course, remove the closing brace afterwards. $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'"); $row = mysqli_fetch_assoc($query); #MAD# Added #MAD# while($row = mysqli_fetch_assoc($query)) # Removed #MAD# { # Removed echo "<tr>"; echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>"; echo "</tr>"; #MAD# } # Removed echo "</table>"; Quote Link to comment Share on other sites More sharing options...
carter90 Posted April 24, 2013 Author Share Posted April 24, 2013 You Sir are a legend. So if I was to display two rows I would just stick 'while' back in. 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.