2017-04-01 50 views
0

我有兩個存儲過程,幾乎完全相同,唯一的區別是第二個過程有一個參數。參數php中的存儲過程錯誤

沒有參數的sp工作正常會生成一個空表。我已經在本地主機服務器上測試了存儲過程,因此存儲過程沒有任何問題。

我也更正了一些代碼,我在mysqli中混合過程和oop編碼。感謝「gmc」,我糾正了這一點。 這是我的代碼:

//sp 

//This store proc without in params is working 
echo "<h4>Författartabellen hämtad från databasen med en store procedure</h4>"; 
    $result_sp = mysqli_query($conn, "CALL jokes"); 

    //loop the result set 
echo "<table><tr><th>Joke</th><th>AuthorName</th></tr>"; 
    // output data of each row 
    while($row = mysqli_fetch_assoc($result_sp)) { 
     echo "<tr><td>".$row["joketext"]."</td><td>".$row["name"]."</td></tr>"; 
    } 
    echo "</table>"; 

// This store proc with in param is not working, returns a empty table 
//run the store proc 
echo "<h4>Författartabellen hämtad från databasen med en store procedure</h4>"; 
    $result_sp_in = mysqli_query($conn, 'CALL jokes_author("Joan Smith")'); 

    //loop the result set 
echo "<table><tr><th>Joke</th><th>AuthorName</th></tr>"; 
    // output data of each row 
    while($row = mysqli_fetch_array($result_sp_in)) { 
     echo "<tr><td>".$row["joketext"]."</td><td>".$row["name"]."</td></tr>"; 
    } 
    echo "</table>"; 

感謝

+0

您正在使用mysqli'的'程序的風格,因此你無法使用其結果作爲對象 – gmc

+0

所以mysqli的過程風格永遠不能與帶參數的存儲過程一起使用,只能使用存儲過程而不帶參數呢? 因此,mysqli的OOP風格將與此一起工作? – user2371684

+0

你可以,但是你必須要做的事情也是使用產品風格:'$ row = mysqli_fetch_assoc($ result_sp_in)' – gmc

回答

0

更新:試試這個:

$result_sp_in = mysqli_query($mysqli, 'CALL jokes_author("Joan Smith", @joketext, @name)'); 
$select = mysqli_query($mysqli, 'SELECT @joketext, @name'); 
while($row = mysqli_fetch_array($select)) { 
    echo "<tr><td>".$row["joketext"]."</td><td>".$row["name"]."</td></tr>"; 
} 
echo "</table>"; 
+0

我以前已經更正了我的代碼,這是沒有參數的sp。 它的參數,顯示一個空表使用$ result_sp_in – user2371684

+0

所以sp而不是mysqli_fetch_assoc,我應該嘗試與mysqli_fetch_array? 如果它不是mysqli_fetch_array參數中的$ result_sp_in,我也試過,它仍然向我顯示一張空表 – user2371684

+0

您確定您的MySQL過程正常工作嗎? – gmc