php
  • mysql
  • arrays
  • select
  • for-loop
  • 2013-03-18 101 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

    相关问题