0

I have a table named account.

Table: account

+---------+---------------------+-----------+
| user_id | email               | u_name    |
+---------+---------------------+-----------+
|      63 | [email protected]     | Pross     |
|      64 | [email protected]       | Pross     |
|      73 | [email protected]    | Victorss  |
|      74 | [email protected] | Ifecosss  |
+---------+---------------------+-----------+

But I want to change all the display u_name into "No" from my table whose u_name = "Pross" in my page like this:

+---------+---------------------+-----------+
| user_id | email               | u_name    |
+---------+---------------------+-----------+
|      63 | [email protected]     | No        |
|      64 | [email protected]       | No        |
|      73 | [email protected]    | Victorss  |
|      74 | [email protected] | Ifecosss  |
+---------+---------------------+-----------+

But my code here replaces all the rows with "No". Seems like the placement of my condition is wrong. Here's my code in php to fetch data from database.

<!--############# TABLE CONTENT ##############-->
<tbody>
<?php 
while($row = mysql_fetch_array($s1)) {
    # Columns from DB
    $db_id = $row['user_id'];
    $db_email = $row['email'];
    $db_uname = $row['u_name']; 

    # Condition to handle display of u_name row
    if($db_uname == "Pross") {
        $db_uname_edit = "No";
    }
?>

<tr>
    <td><?php echo $db_id ?></td>
    <td><?php echo $db_email ?></td>
    <td><?php echo $db_uname_edit ?></td>
</tr>
<?php } ?>

</tbody>
<!--##########################################-->

But the unwanted output of this is :

+---------+---------------------+-----------+
| user_id | email               | u_name    |
+---------+---------------------+-----------+
|      63 | [email protected]     | No        |
|      64 | [email protected]       | No        |
|      73 | [email protected]    | No        |
|      74 | [email protected] | No        |
+---------+---------------------+-----------+

Why ? Can anybody help me with my code above ? Thanks

4 Answers 4

4

You should set the value of $db_uname_edit to $db_uname in else part:

if($db_uname == "Pross") {
    $db_uname_edit = "No";
} else {
    $db_uname_edit = $db_uname;
}
Sign up to request clarification or add additional context in comments.

Comments

1
# Condition to handle display of u_name row
if($db_uname == "Pross") {
    $db_uname_edit = "No";
}
else {
    $db_uname_edit = $db_uname;
}

This should work.

And here is the shortened edition, you won't need an if clause:

<td><?php echo $db_uname == 'Pross' ? 'No' : $db_uname ?></td>

Comments

0

change your "if" statement to:

 if($db_uname === "Pross") {
        $db_uname_edit = "No";
    }

when compareing strings you should use the "===" operator instead of "=="

Comments

0

you need to add else part in if condition if condition not matched then code goto else condition

if($db_uname == "Pross") {
    $db_uname_edit = "No";
} else {
    $db_uname_edit = $db_uname;
}

OR better user ternary operator to reduce code

$db_uname_edit = $db_uname == "Pross" ? "No" : $db_uname;

And You can do this in DB query, that would be good for performance

select user_id, email, IF(u_name = 'Pross', 'No', u_name) AS u_name FROM account

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.