2013-07-23 33 views
1

我是Php和mysql的新手。我的項目就像生物「詞典」。我想找到一個WORD,我會得到它的定義和更多的列,其中有關於我尋找的WORD的相關信息。我想獲得漂亮的表格中的字典數據。我如何在PHP中使用Jquery時編寫查詢代碼

我已經寫了查詢來從我的mysql數據庫中檢索與我的單詞有關的數據。我將從4個不同的表中獲取數據。

我寫了下面發佈的Index.php和data.php代碼。問題>>>>>>

1)當我運行inded.php文件I m如果我有在data.php文件寫入代碼難於錯誤

function get() { $.post ('data.php', { word: form.name.value }, function (output) { $('#defination') .html(output).show() ; }); } " 

2),請給我建議。我從多個表中檢索數據,所以它很混亂。

請幫助我,我真的很掙扎,我會感謝您的幫助。

謝謝

的index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd" 

<html lang="en"> 

<head> 
    <title></title> 

    <script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript"></script> 
      function get() { 
        $.post ('data.php', { word: form.name.value }, 
          function (output) { 

         $('#defination') .html(output).show() ; 

        }); 
      } 
</head> 

<body> 

<p> 
<form name ="form"> 
    <input type="search" name="name" size = "30" placeholder="Please enter the Word"> 
    <input type="button" value="Get" onClick="get();"> 

</form> 

<div id="defination"> </div> 
</p> 


</body> 
</html> 

data.php

<table border="2" cellpadding="5" cellspacing="1" bordercolor="#000000"> 
    <tr> 
    <!--<td width="120"><b>Normalized Word</b></td> 
    <td width="120"><b>Normalized String</b></td>--> 
    <td width="120"><b>Word</b></td> 
    <td width="120"><b>Defination</b></td> 
    <td width="120"><b>Type of Sementic</b></td> 
    <td width="120"><b>Source</b></td> 

    </tr> 
    <tr> 
    <td> 

    <?php 
    $username = "root"; 
    $password = ""; 
    $hostname = "127.0.0.1"; 

    //connection to the database 
    $dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 


    //select a database to work with 
    $selected = mysql_select_db("umls",$dbhandle) 
     or die("Could not select examples"); 


     // $_Post 

     $name = mysql_real_escape_string($_POST['word']); 

    //Name is NULL 
    if ($word==NULL) 
     echo "Please enter a Word" ; 
    else { 
    // Do Query 

    $result = mysql_query("SELECT DISTINCT * FROM nString WHERE Normalized_String= '$word' GROUP by string, defination, Source"); 
    $result_num_rows = mysql_num_rows($result); 
    //fetch tha data from the database 



     while ($row = @mysql_fetch_array($result)) 
     { 
     $variable1=$row["Normalized_Word"]; 
     $variable2=$row["Normalized_String"]; 
     $variable3=$row["String"]; 
      $variable4=$row["Defination"]; 
     $variable5=$row["Semantic_type"]; 
     $variable6=$row["source"]; 

     //table layout for results 

     print ("<tr>"); 
     //print ("<td>$variable1</td>"); 
     //print ("<td>$variable2</td>"); 
     print ("<td>$variable3</td>"); 
      print ("<td>$variable4</td>"); 
     print ("<td>$variable5</td>"); 
     print ("<td>$variable6</td>"); 

     print ("</tr>"); 
     } 
     } 


     // while ($row = mysql_fetch_array($result)) { 
    // echo $row{'CUI'}.$row{'SUI'}.$row{'AUI'}."<br>"; 

    //close the connection 
    mysql_close($dbhandle); 
    ?> 

    </table> 
+0

是一個PHP錯誤信息? – 2013-07-23 20:47:17

回答

3

你所得到的第一個錯誤,因爲JavaScript代碼應該是<script>標籤內,而不是外面..

試試這個..

<script type="text/javascript"> //notice the </script> tag is shifted to the end of javascript code 

     function get() { 
       $.post ('data.php', { word: form.name.value }, 
         function (output) { 

        $('#defination') .html(output).show() ; 

       }); 
     } 

</script>  

和其他一些錯誤在data.php

$name = mysql_real_escape_string($_POST['word']); 


if ($name==NULL) //<----here $word = $name 
    echo "Please enter a Word </td></tr>"; //close the tr and td 
else { 

,你可以結束和開始的PHP標籤再次

<?php 
    $username = "root"; 
    $password = ""; 
    $hostname = "127.0.0.1"; 
    ..... 
    ..... 
    $variable5=$row["Semantic_type"]; 
    $variable6=$row["source"]; 

    ?> 

    <tr> 

     <td><?php echo $variable3; ?></td> 
     <td><?php echo $variable4; ?></td> 
     <td><?php echo $variable5; ?></td> 
     <td><?php echo $variable6; ?></td> 

    </tr> 

注:MySQL是不贊成使用的mysqli PDO ...學習OOP創建類而不是基於程序,並尋找一些PHP框架... CODEIGNITER,ZEND,YII等等

+0

最後謝謝你的工作 –

+0

weocme ..很高興它幫助..快樂編碼.. :) – bipen