• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

How can I use PHP to echo a value from a mysql database?

Dynomico

New Member
Joined
Jun 14, 2018
Messages
5 (0.00/day)
So i'm trying to make something that will display an int value from the database for the user

but currently it will only display the number 0 even if I edit the database

PHP:
<p><center><img src="images/dynocoin.png" width="16" height="16" alt ="Dynocoins"> : </img><?php echo intval($dbRecord['coins'], ENT_QUOTES); ?></center></p>

any help even if minimal is appreciated!
 
What is ENT_QUOTES set as? That is the optional parameter for base so, assuming you're trying to convert a floating point decimal to a whole number, having it set to anything other than 10 (decimal base) is going to give you unexpected results.

Also, did you try echoing $dbRecord['coins'] as is to make sure it has a non-zero value?

What is the type of 'coins' as queried from the database?

Unrelated but <img /> cannot have any innerText (in your case " : "). Should be <image src="images/dynocoin.png" width="16" height="16" alt="Dynocoins" />
 
Last edited:
Maybe me trying to echo $dbRecord['coins'] would only use the default value set to 0 instead of the current users, so maybe $_SESSION['coins'] would work?
 
Here's examples how to use mysqli:
https://secure.php.net/manual/en/mysqli.examples-basic.php

Basically:
1) connect
2) query
3) fetch results
4) do something with the results
5) free the result
6) close the connection

I assume you have something like this (from the example above) encapsulating the code you provided:
PHP:
echo "<ul>\n";
while ($actor = $result->fetch_assoc()) {
    echo "<li><a href='" . $_SERVER['SCRIPT_FILENAME'] . "?aid=" . $actor['actor_id'] . "'>\n";
    echo $actor['first_name'] . ' ' . $actor['last_name'];
    echo "</a></li>\n";
}
echo "</ul>\n";
Instead of $actor, you called it $dbRecord. If not, then I'd focus on making sure you've successfully queried the data you're trying to get.

If there isn't a 'coins' column with a result to return, you can easily be getting zero because of that.
 
Well I was trying to do what I had done with the username being displayed when logged in, but Instead using an integer
PHP:
<p><center>currently logged in as: <?php echo htmlspecialchars($_SESSION['username'], ENT_QUOTES); ?></center></p>
but I assume for string values it's much simpler than int values?
 
Well, $_SESSION doesn't have anything to do with mysqli queries. If the data you want is in a database, you need to query the database.

I wouldn't worry so much about value type right now. Focus on getting the data. Once you have the data, you can modify it as needed. Most likely you won't have to unless you're trying to clean up a messy float (e.g. 0.100020142145152). In which case, you're probably going to want to use the round function over intval.
 
ahhh I see now, before I thought the video tutorials on this I were watching were using something different or were outdated, but it turns out they were doing the correct thing. I'll try some of the things the videos and you suggested and tell you how it turns out.
 
It's already an int coming out of the database. Even still, just echo it. There is no need to coerce it into an integer. I would try inspecting the record to get an idea for what's inside of it. Could it actually be zero or a floating point number smaller than 1.0 (intval floors doesn't it?)
 
Last edited:
intval floors doesn't it?
Not sure, they given example shows 4.2 -> 4; they don't give an example like 4.8.

No matter, I wouldn't recommend using intval anyway because it's purpose is to convert strings (especially binary, octal, or hexadecimal) into integers when echo is going to force it back to a string anyway. intval serves no purpose in this case.
 
Back
Top