简体   繁体   English

在PHP中将3列与MySQL串联

[英]concatenate 3 columns with MySQL in PHP

I have an Android app that will POST the category variable to my PHP. 我有一个Android应用程序,该应用程序将category变量发布到我的PHP中。 In my PHP i wand to find the category (column) the buyer is interested in and then in that row concatenate the 3 other columns from that same row. 在我的PHP中,我想要找到买家感兴趣的类别(列),然后在该行中将同一行中的其他3列连接起来。 Then save that into a variable which i will echo out. 然后将其保存到一个变量中,我会回声。

i realize there are multiple products listed in the same category but that is a different question. 我意识到同一类别中列出了多种产品,但这是一个不同的问题。

PHP code: PHP代码:

<?php

$conn = mysqli_connect("phpmyadmin.cvw71h2krjrb.us-east-1.rds.amazonaws.com", "phpmyadmin", "phpmyadmin", "Products");

$Category = $_POST["Category"];

//$sql_query = "select Product_Category from Products where Product_Category     like '$Category'";

$sql_query = "select concat(Product_Owner_Email,'_',Product_Name,'_',     Product_Key_Code) as productkeyword from table where <Product_Category like     '$Category'>";

$result = mysqli_query($conn, $sql_query);

if(mysqli_num_rows($result) > 0 ){

$row = mysqli_fetch_assoc($result);
$Product_Owner_Email =  $row['Product_Owner_Email'];
$Product_Name =  $row['Product_Name'];
$Product_Key_Code =  $row['Product_Key_Code'];

}

mysqli_close($conn);

echo $ProductKeyWord;
?>

How can i concatenate 3 columns with MySQL in PHP? 如何在PHP中用MySQL连接3列?

also

On my app the echo back is "" 在我的应用上,回显为“”

...................................................................... ANSWER BELOW ............................................................ .................................................. .................... 下方的答案 ..................................... ................................

<?php

 ini_set('display_errors',1); ini_set('display_startup_errors',1);   error_reporting(E_ALL);//This code is used so if i test the PHP in the browser itll tell me any errors or warnings!! 

 $conn = mysqli_connect("XXXX", "XXXX", "XXXX", "Products");

 $Category = $_POST["Category"];

 $sql = "SELECT Product_Owner_Email, Product_Name, Product_Key_Code FROM     Product_Details WHERE Product_Category LIKE '$Category'
 LIMIT 0 , 30";
 $result = $conn->query($sql);

 if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo $row["Product_Owner_Email"]. "_" . $row["Product_Name"]. "_" .    $row["Product_Key_Code"] . " ";
     }
 } else {
     echo "0 results";
 }
 $conn->close();
 ?>

the answer actually returns every "product" in the $Category 答案实际上返回$ Category中的每个“产品”

You can use the below query: 您可以使用以下查询:

select concat(Product_Owner_Email,'_',Product_Name,'_', Product_Key_Code) as productkeyword from table where <where condition>

I have updated your code, now try this. 我已经更新了您的代码,现在尝试一下。

$conn = mysqli_connect("phpmyadmin.cvw71h2krjrb.us-east-1.rds.amazonaws.com", "phpmyadmin", "phpmyadmin", "Products");

$Category = $_POST["Category"];

//$sql_query = "select Product_Category from Products where Product_Category     like '$Category'";

$sql_query = "select Product_Owner_Email,Product_Name,concat(Product_Owner_Email,'_',Product_Name,'_',Product_Key_Code) as productkeyword from Products where Product_Category  like '".$Category."'";

$result = mysqli_query($conn, $sql_query);

if(mysqli_num_rows($result) > 0 ){

$row = mysqli_fetch_assoc($result);
$Product_Owner_Email =  $row['Product_Owner_Email'];
$Product_Name =  $row['Product_Name'];
$Product_Key_Code =  $row['productkeyword'];

}

mysqli_close($conn);

echo $Product_Key_Code;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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