• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

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!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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:

Dynomico

New Member
Joined
Jun 14, 2018
Messages
5 (0.00/day)
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?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 

Dynomico

New Member
Joined
Jun 14, 2018
Messages
5 (0.00/day)
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?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 

Dynomico

New Member
Joined
Jun 14, 2018
Messages
5 (0.00/day)
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.
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,174 (2.77/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
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:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 
Top