php函数和mysqli

时间:2017-04-29 01:38:42

标签: php mysql

<?php
select1($conn); 
function select2 ($conn,$id ,$name)
{   
    $stmt = $conn->prepare("SELECT def FROM define WHERE wordkey = ?");
    mysqli_stmt_bind_param($stmt, 'i', $id);
    $stmt->execute(); 
    $stmt->bind_result($def);
    while($stmt->fetch()) {
        echo "here"; // for testing
        $wordArray += $def; 
        //I will make a call to the js function here sending id, name and wordArray
    }  
    //$stmt->close();
}

function select1 ($conn){
    $stmt2 = $conn->prepare("SELECT id , name FROM words");
    $stmt2->execute(); 
    $stmt2->bind_result($id, $name);
    while($stmt2->fetch()) {
        select2 ($conn,$id ,$name);
    }
}
?>

我想问问题我有单词表和def表。在def表中,我有相同单词的多个定义.id。是否可以在一个查询语句中选择它们?

单词表有

id , name 

def表有

id, wordkey , definition

我想检索名称,并为每个名称选择其所有定义,以便我将调用javascript函数(id,name,defArray)以显示每个名称及其定义数组。我想通过2个select语句来做,但问题是select2函数不能正常工作。请帮助!

1 个答案:

答案 0 :(得分:0)

你的功能应该是......

function select2 ($conn,$id ,$name)
{   
    $stmt = $conn->prepare("SELECT def FROM define WHERE wordkey = ?");
    mysqli_stmt_bind_param($stmt, 'i', $id);
    $stmt->execute(); 
    $stmt->bind_result($def);
    $wordArray = array();
    while($stmt->fetch()) {
        echo "here"; // for testing
        $wordArray[] = $def; 
    }  
    return wordArray();
}

这将分别构建一个包含值的数组,然后您可以

json_encode(['id' => $id, 'definitions' => $returnValueFromSelect2]);