简体   繁体   中英

How to make my PHP output two values from database

Ive included a block of code below detailling how im placing sections of data from a table in my database in their own divs. However, im new to PHP and cant find out how to also output the "cup_id" from the database into their respective divs here: echo $cup["cup_name"] . "<br />"; echo $cup["cup_name"] . "<br />"; Thanks for all the help in advance!

So in short how do i get this to work: echo $cup["cup_id", "cup_name"] . "<br />"; echo $cup["cup_id", "cup_name"] . "<br />";

<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);

if (!$show_cup) {
    echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}

while ($cup = mysqli_fetch_assoc($show_cup)) {
    echo '<div class="cup-info">';
    echo $cup["cup_name"] . "<br />";
    echo '</div>';
}
?>

Do you want to concatenate strings? use the dot operator:

echo $cup["cup_id"] . $cup["cup_name"];

And if you want to print it in another div, make this:

echo '<div class="cup-info">';
echo $cup["cup_id"] . "<br />";
echo '</div>';
echo '<div class="cup-info">';
echo $cup["cup_name"] . "<br />";
echo '</div>';

if you want the name and id to be in the same raw then you can follow this method

<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);

if (!$show_cup) {
    echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}

while ($cup = mysqli_fetch_assoc($show_cup)) {
    echo '<div class="cup-info">';
    echo $cup["cup_id"] . $cup["cup_name"]."<br />";
    echo '</div>';
}
?>

if you want the name and id to be in different raw then you can follow this method

<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);

if (!$show_cup) {
    echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}

while ($cup = mysqli_fetch_assoc($show_cup)) {
    echo '<div class="cup-info">';
    echo $cup["cup_id"]."<br />";
    echo '</div>';
    echo '<div class="cup-info">';
    echo $cup["cup_name"]."<br />";
    echo '</div>';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM