Reputation: 2846
I am looping through a MySQL result but I am running into an issue. The issue is that only my first if
statement is being executed. The rest of my if
statements are returning empty strings. What is confusing me is this: When I remove the first if statement in the while loop, the next if statement will return data but all other statements below it will not.
Here is my relevant code:
// Get Addresses
$queryAddresses = "SELECT * FROM contact_addresses WHERE contact_id = $contactID";
$resultAddresses = mysqli_query($dbc, $queryAddresses);
$numAddresses = mysqli_num_rows($resultAddresses);
if ($numAddresses >= 1) {
$address_labels = '';
$addresses = '';
while ($address = mysqli_fetch_array($resultAddresses, MYSQLI_ASSOC)) {
$address_labels .= $address['label'].'<br>';
if (!empty($address['address'])) {
$address = $address['address'].', ';
} else {
$address = '';
}
/* ALL OF THE IF STATEMENTS BELOW THIS LINE RETURN EMPTY STRINGS EVEN THOUGH THE INFORMATION IS STORED IN THE DATABASE. */
if (!empty($address['address_two'])) {
$address_two = $address['address_two'].', ';
} else {
$address_two = '';
}
if (!empty($address['city'])) {
$city = $address['city'].', ';
} else {
$city = '';
}
if (!empty($address['state'])) {
$state = $address['state'].', ';
} else {
$state = '';
}
if (!empty($address['zip'])) {
$zip = $address['zip'].', ';
} else {
$zip = '';
}
if (!empty($address['country'])) {
$country = $address['country'].', ';
} else {
$country = '';
}
$addresses .= $address.''.$address_two.''.$city.''.$state.''.$zip.''.$country.'<br>';
}
}
Upvotes: 0
Views: 79
Reputation: 100
In the following code change the variable, as it is replacing the values of the array by assigning new value.
if (!empty($address['address'])) {
$address = $address['address'].', ';//Chang variable name here
} else {
$address = '';//Chang variable name here
}
Upvotes: 0
Reputation: 51
Plz paste following Code in your editor:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "databasename";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "<br>";
$fnanem ="";
$lnanem ="";
if(!empty($row["first_name"])){
$fnanem = $row["first_name"];
}else{
$fnanem = "";
}
if(!empty($row["last_name"])){
$lnanem = $row["last_name"];
}else{
$lnanem = "";
}
echo $fnanem."---->".$lnanem."<br>";
}
} else {
echo "0 results";
}
$conn->close();
Upvotes: 0
Reputation: 830
while ($address = mysqli_fetch_array($resultAddresses, MYSQLI_ASSOC)) {
$address_labels .= $address['label'].'<br>';
if (!empty($address['address'])) {
$address = $address['address'].', ';
} else {
$address = '';
}
}
What I found while going through your code is you are using the same variable $address
for the while loop as well as the first if statement. Try changing the loop variable, say $result
and try
Upvotes: 0
Reputation: 456
With the line $address = $address['address'].', ';
you are replacing the $address
array with the new string and wiping out the array. Try changing the string variable name:
if (!empty($address['address'])) {
$address_one = $address['address'].', ';
} else {
$address_one = '';
}
if (!empty($address['address_two'])) {
$address_two = $address['address_two'].', ';
} else {
$address_two = '';
}
if (!empty($address['city'])) {
$city = $address['city'].', ';
} else {
$city = '';
}
if (!empty($address['state'])) {
$state = $address['state'].', ';
} else {
$state = '';
}
if (!empty($address['zip'])) {
$zip = $address['zip'].', ';
} else {
$zip = '';
}
if (!empty($address['country'])) {
$country = $address['country'].', ';
} else {
$country = '';
}
$addresses .= $address_one.''.$address_two.''.$city.''.$state.''.$zip.''.$country.'<br>';
Upvotes: 1