ianhaney50 Posted October 30, 2015 Share Posted October 30, 2015 I could not get the last repair_id outputting so am tackling this issue from another angle, I am going to do away with repair_id and just use id which is AI and primary key I am going to display the whole row instead but limit it to 1 and order by ID DESC so shows the highest id number but the issue I got is it shows all records but the second I put LIMIT 1 in it only shows the id number and no other data, am getting bit fed up with to be honest but want to get it done I have the following coding at the moment <?php $servername = ""; $username = ""; $password = ""; $dbname = ""; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //run the query $sql = "SELECT * FROM repairs ORDER BY id DESC"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { //fetch the results while($row = mysqli_fetch_assoc($result)) { echo $row['id'] . $row['repair_id'] . " " . $row['customer_name'] . " " . $row['customer_email'] . " " . $row['customer_phone']; } } else { echo "0 results"; } echo $sql; mysqli_close($conn); ?> This is outputting the following 17 16 15 14 13 12004 Ian Haney ianhaney@broadwaymediadesigns.co.uk 01268 92843011004 Ian Haney ianhaney@broadwaymediadesigns.co.uk 01268 92843010 9 8003 Ian Haney ianhaney@broadwaymediadesigns.co.uk 01268 9284307003 Ian Haney ianhaney@broadwaymediadesigns.co.uk 01268 9284306003 Ian Haney ianhaney@broadwaymediadesigns.co.uk 01268 9284305002 Ian Haney ianhaney@broadwaymediadesigns.co.uk 01268 9284304001 Ian Haney ianhaney@broadwaymediadesigns.co.uk 01268 9284303 2 SELECT * FROM repairs ORDER BY id DESC Please help me and see where I am going wrong with it Quote Link to comment https://forums.phpfreaks.com/topic/298930-display-records/ Share on other sites More sharing options...
ginerjm Posted October 30, 2015 Share Posted October 30, 2015 Your output seems to be exactly what you are echo-ing. If you want it formatted, do it with your echo or simply add another echo to output a line break. (inside the loop). As for missing items and the limit 1 points - show us that code if you want to discuss it. Quote Link to comment https://forums.phpfreaks.com/topic/298930-display-records/#findComment-1524824 Share on other sites More sharing options...
ianhaney50 Posted October 30, 2015 Author Share Posted October 30, 2015 Thank you for the reply, appreciate it Below is the code with the LIMIT in the query <?php $servername = ""; $username = ""; $password = ""; $dbname = ""; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //run the query $sql = "SELECT id, customer_name, customer_email, customer_phone FROM repairs ORDER BY id DESC LIMIT 1"; $result = $conn->query($sql); if ($result->num_rows > 0) { //fetch the results while($row = $result->fetch_assoc()) { echo $row['id'] . " " . $row['customer_name'] . " " . $row['customer_email'] . " " . $row['customer_phone']; } } else { echo "0 results"; } echo $sql; $conn->close(); ?> All that code does is output the following 17 SELECT id, customer_name, customer_email, customer_phone FROM repairs ORDER BY id DESC LIMIT 1 It seems to stop at the id number and not output the name, email and phone Quote Link to comment https://forums.phpfreaks.com/topic/298930-display-records/#findComment-1524830 Share on other sites More sharing options...
ginerjm Posted October 30, 2015 Share Posted October 30, 2015 Very puzzling. Please try this in your code: //fetch the results while(list($id,$name,$email,$phone) = $result->fetch_row()) { echo "id $id name $name email $email phone $phone<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/298930-display-records/#findComment-1524831 Share on other sites More sharing options...
ianhaney50 Posted October 30, 2015 Author Share Posted October 30, 2015 Very strange isn't it I altered the coding to what you provided and now outputs the following id 17 name email phone SELECT id, customer_name, customer_email, customer_phone FROM repairs ORDER BY id DESC LIMIT 1 is very weird that it outputs the id number but not the customer name, email and phone Quote Link to comment https://forums.phpfreaks.com/topic/298930-display-records/#findComment-1524833 Share on other sites More sharing options...
ginerjm Posted October 30, 2015 Share Posted October 30, 2015 What is weird is that you have a record with no values in it other than the id. Why haven't you browsed your table (phpadmin?) to confirm that there is data in there? Quote Link to comment https://forums.phpfreaks.com/topic/298930-display-records/#findComment-1524835 Share on other sites More sharing options...
ianhaney50 Posted October 30, 2015 Author Share Posted October 30, 2015 So sorry it is my fault, I am so stupid, there was no data for id 17 but was sure there was, so sorry, let me just slap myself, so sorry Quote Link to comment https://forums.phpfreaks.com/topic/298930-display-records/#findComment-1524836 Share on other sites More sharing options...
ginerjm Posted October 30, 2015 Share Posted October 30, 2015 beginning programmer error. Always check things. Never assume. 1 Quote Link to comment https://forums.phpfreaks.com/topic/298930-display-records/#findComment-1524837 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.