×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

我把$query2改成$query2="SELECT `table1` FROM `table2`, `table1` WHERE `$link_id`=$l"; 还是不work....

说$name=mysql_result($result2, $i, "link_name");这一行



Warning: mysql_result(): supplied argument is not a valid MySQL result resource


source:

----------------------------------
<?

mysql_connect($host,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM `table1`";

$result=mysql_query($query);

$num=mysql_numrows($result);


$i=0;

while ($i < 15) {

$l=mysql_result($result, $i, "id");

echo $l;

$query2="SELECT `table1` FROM `table2`, `table1` WHERE `$link_id`=$l";

$result2=mysql_query($query2);

$name=mysql_result($result2, $i, "name");

echo '<br/>';

echo $name;


$i++;
}


mysql_close();
?>
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 问个php/mysql的问题, 同一个裤, 两个table, 需要从第一个table里的id 列取一个值(5) 然后找到第2个table里id列(5) 对应的另一个列(name)里的值.......................... 如何?
    <?

    mysql_connect($host,$username,$password);

    @mysql_select_db($database) or die( "Unable to select database");

    $query="SELECT * FROM `table1`";

    $result=mysql_query($query);

    $num=mysql_numrows($result);


    $i=0;

    while ($i < 15) {

    $l=mysql_result($result, $i, "id");

    echo $l;

    $query2="SELECT * FROM `table2` WHERE `$link_id`=$l";

    $result2=mysql_query($query2);

    $name=mysql_result($result2, $i, "name");

    echo '<br/>';

    echo $name;


    $i++;
    }


    mysql_close();
    ?>
    • use UPDATE
      • update will mess up the existing data in tables... i just want to have the results displayed.
        • Sorry, 没仔细看题。呵呵
          SELECT table2.name FROM table2,table1 WHERE table2.link_id = table1.id

          or, if not all link_id map to id, then use

          SELECT table2.name FROM table2 LEFT JOIN table1 ON table2.link_id=table1.id
          • 我把$query2改成$query2="SELECT `table1` FROM `table2`, `table1` WHERE `$link_id`=$l"; 还是不work....
            说$name=mysql_result($result2, $i, "link_name");这一行



            Warning: mysql_result(): supplied argument is not a valid MySQL result resource


            source:

            ----------------------------------
            <?

            mysql_connect($host,$username,$password);

            @mysql_select_db($database) or die( "Unable to select database");

            $query="SELECT * FROM `table1`";

            $result=mysql_query($query);

            $num=mysql_numrows($result);


            $i=0;

            while ($i < 15) {

            $l=mysql_result($result, $i, "id");

            echo $l;

            $query2="SELECT `table1` FROM `table2`, `table1` WHERE `$link_id`=$l";

            $result2=mysql_query($query2);

            $name=mysql_result($result2, $i, "name");

            echo '<br/>';

            echo $name;


            $i++;
            }


            mysql_close();
            ?>
            • try this one, got to move my car, be back later...
              <?

              mysql_connect($host,$username,$password);

              @mysql_select_db($database) or die( "Unable to select database");

              $query="SELECT table2.name FROM table1, table2 WHERE table2.link_id=table1.id AND table1.id<15";

              $result=mysql_query($query);

              $num=mysql_numrows($result);


              for($i=0; $i<$num; $i++) {

              $name=mysql_result($result, $i);

              echo $name."<br/>";

              }

              mysql_close();
              ?>
              • thanks!!
                thanks!! 还是同样的结果... 我单独query tb2, 也是这样的结果, 难道tb2有问题? 对数据类型有要求?

                第一个表的第一列叫id, 第2个表也一样, 第一个表的id 是有一个顺序的 比如 3, 5 ,11, 2 ..... 第二个表的id 也是乱的, 比如4, 22,23,325,234,.... 想读出第一个表的前3位(3,5,11)找到对应表2 id为3,5,11的 name值.....


                但是anyway... 即便我 select * from table2 where id=5 还是同样的错误...

                table2 的name Type: varchar Collation :latin1_swedish_ci

                这些有关系吗?
                • OK, how about this
                  SELECT table2.name FROM table2 WHERE table2.link_id = (SELECT id FROM table1 ORDER BY id LIMIT 3)
                  • Should we add `` around the table name? such as `table1`?
                    • Yes you can but you don't have to unless you use keyword or special characters as field name.
                      • umm... then another thought is, are the table names case-sensitive?
                        • Oh, if you are considering the LZ's post, there is already a final solution (#2942527@0). I discussed with LZ it under the table, sorry about that, :-p
    • The final corect one ...
      <?php

      $link = mysql_connect($server,$username,$password);

      mysql_select_db($database);

      $query = "SELECT table2.name, table2.link_id FROM table2 INNER JOIN (SELECT link_id FROM table1 ORDER BY table1.date DESC LIMIT 15) AS table2_alias USING (link_id) ";

      $result=mysql_query($query, $link);

      if( $result ) {

      $num=mysql_numrows($result);

      for($i=0; $i<$num; $i++) {

      $name=mysql_result($result, $i, "name");
      $link_id=mysql_result($result, $i, "link_id");


      echo '<a href="index.php?option=com_mtree&task=viewlink&link_id='.$link_id.'&Itemid=26">'.$name.'</a><br/>';

      }
      } else {
      echo "Result is not valid<br/>";
      }


      mysql_close();
      ?>
      • 感謝輪胎店.... 全部問題解決... everything is running!!!