简体   繁体   中英

Loop through and display results in PHP from SELECT WHERE in SQL

I have right now only 1 table, which contains people's names and addresses, and I wish to display data from the records where their country is GB.

I currently have the following code which I'm stumped doesn't work:

<?php
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('amazondb', $conn); 
$result = mysql_query("SELECT * FROM imported_orders WHERE ShipCountry='GB'");
    while($row = mysql_fetch_array ($result))
        {
            echo "<br><b>Current Order ".$row['ID']."</b><br>";
            echo "<b>Date Purchased: </b>".$row['PurchaseDate']."<br>";
            echo "<b>Buyer Details</b><br>";
            echo "<b>Name : </b>".$row['BuyerName']."<br><b> Phone Number: </b>".$row['BuyerPhoneNumber']."<br><b> Email: </b>".$row['BuyerEmail']."<br>";
            echo "<b>Delivery Address</b><br>";
            echo "<b>Address Line 1 : </b>".$row['ShipAddress1']."<br><b> Address Line 2: </b>".$row['ShipAddress2']."<br><b> Address Line 3: </b>".$row['ShipAddress3']."<br><b>Country: </b>".$row['ShipCountry'];
        }
?>

It is in a file on its own right now so there is not other code to interfere with it.

All this code does is output the data from 1 record to screen, this being the data from the *last record which has GB as the country. If I change it to "IE" it will echo nothing, even though there are records with IE as the country.

I have also tried doing the WHERE SQL in PHP using an IF to check the value of the country, this gives the results as the above code.

EDIT: I tried it with:

WHERE ShipCountry like '%GB%'

and this works, as apparently 'GB' and 'IE' etc has a space after it, even though I imported the data to the table from a file using tab deliberated, so I'm not sure why there was a blank space imported after it.

Thanks for any help -Tom

Try your code as below to see if you get more than one result. If yes: then your ShipCountry does not have anything along with 'IE'

<?php
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('amazondb', $conn); 
$result = mysql_query("SELECT * FROM imported_orders limit 30");
    while($row = mysql_fetch_array ($result))
        {
            echo "<br><b>Current Order ".$row['ID']."</b><br>";
            echo "<b>Date Purchased: </b>".$row['PurchaseDate']."<br>";
            echo "<b>Buyer Details</b><br>";
            echo "<b>Name : </b>".$row['BuyerName']."<br><b> Phone Number: </b>".$row['BuyerPhoneNumber']."<br><b> Email: </b>".$row['BuyerEmail']."<br>";
            echo "<b>Delivery Address</b><br>";
            echo "<b>Address Line 1 : </b>".$row['ShipAddress1']."<br><b> Address Line 2: </b>".$row['ShipAddress2']."<br><b> Address Line 3: </b>".$row['ShipAddress3']."<br><b>Country: </b>".$row['ShipCountry'];
        }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM