简体   繁体   中英

CONCAT function in MySQL using PHP Undefined Variable

I need help on how to do this correctly. I need to execute this command:

SELECT concat(branchname, -->, itemtype, '(, quantity, ')') from monitoring
order by itemtype;

the syntax works in MySQL console. However, im having trouble with implementing it on php. I always get "Undefined index: branchname" "Undefined index: itemtype" "Undefined index: quantity"

using this code:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dex_test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT concat(branchname,itemtype,quantity) from monitoring order by itemtype";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);

The error says it's in this line

echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";

Im confused because I basically ran the same code that worked that lets me see the itemtype in the table:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dex_test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT itemtype FROM monitoring";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "itemtype: " . $row["itemtype"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);

Help anyone?

Just define the alias for the concatenated columns. Use this -

SELECT concat(branchname,itemtype,quantity) as branchname from monitoring order by itemtype

Or if you want them seperately then -

SELECT branchname, itemtype, quantityfrom monitoring order by itemtype

It seems your query needs update

"SELECT concat(branchname,itemtype,quantity) from monitoring order by itemtype";

It should be

"SELECT branchname,itemtype,quantity from monitoring order by itemtype";

I have posted this answer in reference of how you were calling your fields in while loop

echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";

and if you need to show the concat value within one field than it should be something like

$sql = "SELECT concat(branchname,' ',itemtype,' ',quantity) as branch from monitoring order by itemtype";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["branch"]."<br>";
    }
} else {
    echo "0 results";
}

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