2014-10-20 90 views
0

獲取數據我有兩個表,學生和stusub,
學生有snamesuni_roll_no
stusubsuni_roll_nosub_code
我請求sub_code這個代碼不從數據庫中顯示的結果,這個代碼不從學生表

<?php 
include("db.php"); 

$sub_code =$_REQUEST['sub_code']; 

$query = @mysql_query("SELECT student.sname, student.sroll_no 
FROM student INNER JOIN stusub 
ON student.suni_roll_no=stusub.suni_roll_no where sub_code = '$sub_code';"); 
while($test = @mysql_fetch_array($query)) 
     { 
      $sub_code = $test['sub_code']; 
      echo "<tr align='center'>"; 

      echo"<td><font color='black'>" .$test['sname']."</font></td>"; 
      echo"<td><font color='black'>" .$test['sroll_no']."</font></td>"; 


      echo "</tr>"; 
     } 
     mysql_close($conn); 
     ?> 
+0

不會使用@來抑制錯誤消息。它們爲調試提供有用的錯誤消息。 – 2014-10-20 08:22:38

+0

使用'mysql_error()'從MySQL服務器獲取錯誤消息。最後,但不是租約,'mysql_ *'函數已被棄用,並且將從未來版本的PHP中移除。改用'mysqli_ *'或PDO。 – 2014-10-20 08:23:32

+0

我刪除了他們,但沒有結果 – 2014-10-20 08:24:03

回答

0

不應該放在裏面查詢;

try it 

    <?php 
    include("db.php"); 

    $sub_code =$_REQUEST['sub_code']; 

    $query =mysql_query("SELECT student.sname, student.sroll_no 
    FROM student INNER JOIN stusub 
    ON student.suni_roll_no=stusub.suni_roll_no where sub_code = '$sub_code'"); 
    while($test =mysql_fetch_array($query)) 
      { 
       $sub_code = $test['sub_code']; 
       echo "<tr align='center'>"; 

       echo"<td><font color='black'>" .$test['sname']."</font></td>"; 
       echo"<td><font color='black'>" .$test['sroll_no']."</font></td>"; 


       echo "</tr>"; 
      } 
      mysql_close($conn); 
      ?> 
+0

感謝夥計這段代碼工作 上帝保佑你。 – 2014-10-20 08:46:26

0

嘗試。它使用PDO。 Jusrt記得要將數據庫連接的細節更改爲你的

<?php 

define('DB_TYPE', 'mysql'); 
define('DB_HOST', '127.0.0.1'); 
define('DB_NAME', 'dbname'); 
define('DB_USER', 'root'); 
define('DB_PASS', 'password'); 

try { 
    // create a new instance of a PDO connection 
    $db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} 
catch(PDOException $e) { 
    // if the connection fails, display an error message 
    echo 'ERROR: ' . $e->getMessage(); 
} 

$sub_code =$_REQUEST['sub_code']; 

$sql = "SELECT student.sname, student.sroll_no FROM student LEFT JOIN stusub ON student.suni_roll_no = stusub.suni_roll_no WHERE stusub.sub_code = :sub_code"; 

$stmt = $db->prepare($sql); 
$stmt->bindValue(':sub_code', $sub_code); 
$stmt->execute(); 

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 

foreach($rows as $row): 
?> 
<tr align="center"> 
    <td><font color='black'><?php echo $row['sname']; ?></font></td> 
    <td><font color='black'><?php echo $row['sroll_no']; ?></font></td> 
</tr> 
+0

謝謝先生。回覆。 此代碼也起作用。 – 2014-10-20 08:46:58

0

嘗試此查詢

$查詢= @mysql_query(「SELECT student.sname,student.sroll_no FROM學生INNER JOIN stusub ON student.suni_roll_no = stusub.suni_roll_no其中stusub.sub_code =' 」。$ sub_code。 「'」);

在哪裏使用tablename.sub_code條件。

相關問題