显示值而不是ID mysqli

时间:2016-01-17 18:37:52

标签: php mysqli

当我添加$elan_category时,我得到category_id,而不是category_title

试图申请"离开加入"但没有成功。我在数据库中有以下表格:

enter image description here

function getElanDetail()
{
    global $con;

    if (isset($_GET['elan_id'])) {

        $elan_id = $_GET['elan_id'];
        $get_elan = "select * from elan where elan_id='$elan_id'";
        $run_elan = mysqli_query($con, $get_elan);

        while ($row_elan = mysqli_fetch_array($run_elan)) {

            $elan_id = $row_elan['elan_id'];
            $elan_category = $row_elan['elan_category'];
            $elan_title = $row_elan['elan_title'];
            $elan_description = $row_elan['elan_description'];
            $elan_image = $row_elan['elan_image'];
            $elan_contact = $row_elan['elan_contact'];

            echo "
                $elan_category //Getting ID of category instead Title :(
                $elan_title
                $elan_description
                $elan_image
                $elan_contact
";

        }
    }
}

2 个答案:

答案 0 :(得分:1)

使用join,您可以执行以下操作:

$elan_id = $_GET['elan_id'];
$get_elan = "SELECT * FROM `elan` 
    JOIN `categories` ON `categories`.category_id = `elan`.elan_category 
    WHERE `elan`.elan_id='$elan_id'";
$run_elan = mysqli_query($con, $get_elan);
while ($row_elan=mysqli_fetch_array($run_elan)){
    print_r($row_elan);
    // see the keys in $row_elan and use them accordingly
}

对于子类别,请尝试以下查询:

SELECT * FROM `elan` 
JOIN `categories` ON `categories`.category_id = `elan`.elan_category        
JOIN `subcategories` ON `subcategories`.subcategory_id = `elan`.elan_subcategory 
WHERE `elan`.elan_id='$elan_id'

答案 1 :(得分:0)

$elan_category = $row_elan['elan_category'];

在上面的代码

之后添加这两行
$cat = mysqli_fetch_row(mysqli_query($con,"SELECT category_title FROM categories WHERE category_id = $elan_category"));
$cat_name = $cat[0];

$ cat_name是您的类别名称

相关问题