php
  • mysql
  • arrays
  • select
  • for-loop
  • 2013-03-18 103 views 2 likes 
    2

    我不能從MySQL數據庫中獲取多行到數組?我有代碼,但它不工作或不顯示所有行時,我回聲到文本框?我不能從MySQL數據庫獲取多行到數組?

    <?php 
    if(is_array($_SESSION['pid'])) 
    { 
        $pid = join(',',$_SESSION['pid']); 
        $result=mysql_query("SELECT id AS wid FROM mywishlist where pid='$pid'") 
          or die("Id Problem"."<br/><br/>".mysql_error()); 
        $results= array(); 
        $i=0; // add the new line 
        while($row=mysql_fetch_array($result)){ 
        $results[$i] = $row['wid']; 
        $i++; 
        } 
        $results; 
    } 
    $max=count($results); 
    for($j=0; $j<$max; $j++) 
    { 
    ?> 
    <input type="text" name="wid[]" value="<?php echo $results[$j]; ?>" /> 
    <?php } ?> 
    
    +0

    [**在新的代碼,請不要使用'mysql_ *'功能**](http://bit.ly/ phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://www.brightmeup.info/article.php?a_id=2)。 – 2013-03-18 09:54:18

    +0

    爲sql注入好的洞好運 – 2013-03-18 09:54:51

    +0

    爲什麼'結果;'後雖然lop – 2013-03-18 09:55:00

    回答

    2

    join(',',$_SESSION['pid'])讓我覺得,要通過自己的pid選擇多行。儘量讓IN運營商的使用:

    SELECT id AS wid FROM mywishlist 
    WHERE pid IN ($pid) 
    
    1

    嘗試

    $results= array(); 
    
    while($row=mysql_fetch_array($result)) 
    { 
        $results[] = $row['wid']; 
    } 
    

    而且for循環。

    $max = count($results); 
    for($j=0; $j<$max; $j++) 
    { 
    ?> 
        <input type="text" name="wid[]" value="<?php echo $results[$j]; ?>" /> 
    <?php 
    } 
    ?> 
    
    +0

    仍然相同的結果dispesh – 2013-03-18 10:01:59

    +0

    @ FarhanDharsi你有沒有嘗試查詢,如其他人所述。 – 2013-03-18 10:03:34

    1

    查詢是錯誤的,使用此查詢

    $result=mysql_query("SELECT id AS wid FROM mywishlist where pid IN($pid) ") 
    
    +1

    以上答案和你的有什麼區別? – 2013-03-18 09:59:57

    +0

    yogesh我使用了這個查詢,但結果仍然只是取一行? – 2013-03-18 10:00:32

    +0

    @FarhanDharsi在'phpmyadmin'中運行此查詢並檢查它返回的行數。在while循環之後還要刪除這行'$ results;'。 – 2013-03-18 10:04:03

    相關問題