select * from table where で条件を記載します。
$code = empty($_GET["code"])? 'null' : $_GET["code"]; $dsn = "mysql:dbname=equity;host=localhost"; $user = "hoge"; $password = "hogehoge"; try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e){ print('connection failed:'.$e->getMessage()); } $sql = "select * from lists where code = $code"; $stmt = $dbh->query($sql); $result = $stmt->fetch(PDO::FETCH_ASSOC); $sector = $result['sector']; $market = $result['market']; echo "<b>".$result['name']. "</b>(".$code.") "; echo $result['sector'] ." ".$result['market']. "<br><br>"; echo "同業種の銘柄(".$sector.")"."<br>"; $sql2 = "select * from lists where market = '$market' AND sector = '$sector' and code != $code"; $stmt2 = $dbh->query($sql2); while($result = $stmt2->fetch(PDO::FETCH_ASSOC)){ echo $result["code"]." ".$result["name"]. "<br>"; }