-1

I have a small function that shows the output from Database values

but the error is I put the condition that if the function output value is empty then show N/A and if it is not empty then sow the database value here is the function

function ClientCompDesignation($ClientDesignation,$ClientCompany){
    include "config/dbcon.php";
    $SessionClientID=$_SESSION['uid'];
    $ClientCDQuery="SELECT ClientCompany,ClientDesignation FROM ClientLogin WHERE ClientID='$SessionClientID'";
    $CLaCDQueryRun=mysqli_query($con,$ClientCDQuery);
    $CLCDRecordFetch=mysqli_fetch_assoc($CLaCDQueryRun);
    $ClientDesignation=$CLCDRecordFetch['ClientDesignation'];
    $ClientCompany=$CLCDRecordFetch['ClientCompany'];
    echo $ClientDesignation. ' - ' .$ClientCompany;
}

Here is the how i am displaying in HTML

<p class="mb-0 text-secondary"><?php if(ClientAddress($clientCity,$ClientCountry)==""){
    echo "N/A";
}
else {
    echo ClientAddress($clientCity,$ClientCountry);
}
?>
</p>

the problem is even when there is value in database it shows the N/A

this is the output

Sr. UI/UX Designer - IntraDevelopersN/A

please help in this

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The function doesn't return anything, it echoes the output itself. Change `echo` in the client to `return`. – Barmar Jul 29 '23 at 19:35
  • The function can never return an empty string. Even if both variables are empty, it returns `' - '` – Barmar Jul 29 '23 at 19:36
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jul 30 '23 at 17:56

1 Answers1

2

I see several issues here:

  1. the function you have shown is ClientCompDesignation, not ClientAddress
  2. assuming the same code pattern, you're echoing information in the called function, not returning it.
  3. ClientAddress sometimes gets called twice - call your function once and assign the result to a variable that is tested in the if branch.
Alnitak
  • 334,560
  • 70
  • 407
  • 495