0

This is a very strange problem I have never experienced before.

I perform these following stataments

//Grabs the currency from the bank account
$stmt = $db->prepare("SELECT * FROM accounts WHERE account_id = '$accountid'");
        $stmt->execute();
        $result = $stmt->get_result();
        if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $bankcurrency = $row['currency'];
            }
        }
        $stmt->close();

//Finds the symbol of the speicific currency
        $stmt = $db->prepare("SELECT * FROM currencies WHERE currency = ?");
        $stmt->bind_param("s", $bankcurrency);
        $stmt->execute();
        $result = $stmt->get_result();
        if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                echo $symbol = $row['symbol'];
            }
        }
        $stmt->close();

The output of the symbol from these statements are � The output should have been $

You would think I have something wrong with my database input, but no, because if I try:

$stmt = $db->prepare("SELECT * FROM currencies WHERE currency = 'USD'");
        //$stmt->bind_param("s", $bankcurrency);
        $stmt->execute();
        $result = $stmt->get_result();
        if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                echo $symbol = $row['symbol'];
            }
        }
        $stmt->close();

This then outputs $ This makes no sense as the data and statements are exactly the same.

You don't need to declare strings or variables in PHP which is why it makes no sense to me?

My database structure is simply:

accounts database:

name: currency
Type: TEXT
Value: USD

currencies database:

name: symbol
Type: TEXT
Value: $

Of course I have other rows, but these are the affected ones

You could say my database is the problem, but since the second statement works fine out outputs $ that is clearly not the problem.

I can't understand what is wrong

EDIT: If I add in $bankcurrency = 'USD'; it also displays USD as you would expect, so there is a problem with my first statement

Teppy
  • 21
  • 1
  • 5
  • 1
    do you set the encoding of your connection? – user3783243 Dec 22 '19 at 22:57
  • Like I say, it's not an encoding issue as if I just input $bankcurrency = 'USD'; or set the statement to that it outputs $ so there must be an issue with my first statement selecting from the accounts database – Teppy Dec 22 '19 at 22:58
  • The statement **echo $symbol = $row['symbol'];** should be **echo $row['symbol'];** As written, you are setting the variable $symbol to the value of $row['symbol], and then outputting the symbol with the echo. – Sloan Thrasher Dec 22 '19 at 23:00
  • @SloanThrasher That isn't the issue :( I just tried that it made no difference – Teppy Dec 22 '19 at 23:02
  • This could be greatly simplified and probable faster by using a join between the two tables. That way you only have to execute a single query. Without knowing the character code returned by the first statement, why you are getting different results. – Sloan Thrasher Dec 22 '19 at 23:04
  • 1
    This `'$accountid'` must be parameterized and bound to the statement using `bind_param()` – Dharman Dec 22 '19 at 23:26

1 Answers1

-2

The following should output what your code is trying to do with less code and a faster result.

Also, this might help diagnose the problem.

$stmt = $db->prepare("SELECT a.currency as src_currency,b.currency,b.symbol FROM accounts a JOIN currencies b ON b.currency = a.currency WHERE account_id = '$accountid'");
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "(" . $row['currency'] . ") " . $row['currency'] . " = " . $row['symbol'];
    }
}
$stmt->close();
Sloan Thrasher
  • 4,953
  • 3
  • 22
  • 40