Jump to content
ianhaney50

display records

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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>";
 }

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

beginning programmer error.  Always check things.  Never assume.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.