duanbi3385 2014-10-07 12:12 采纳率: 100%
浏览 21

尝试使用php,mysql和ajax获取数据时出错

I'm using the example from www.w3schools.com to learn about PHP AJAX and Mysql. I follow this very example here.

I've uploaded this company.html to my server:

<html>
<head>
<script>
function showCompany(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  } 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","get.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="Companies" onchange="showCompany(this.value)">
<option value="">Vælg en virksomhed:</option>
<option value="1">Lego</option>
</select>
</form>
<br>
<div id="txtHint"><b>Data kommer her.</b></div>

</body>
</html>

and this get.php also:

<?php
$q = intval($_GET['q']);

$con = mysql_connect('localhost','user','password','mydb');
if (!$con) {
  die('Could not connect: ' . mysql_error($con));
}

mysqli_select_db($con,"mydb");
$sql="SELECT * FROM Tabel WHERE id = '".$q."'";
$result = mysql_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Virksomhed</th>
<th>Logo/th>
<th>Fokus</th>
<th>Kontaktperson</th>
<th>Mobil</th>
<th>Email</th>
<th>Billede</th>
<th>Om Virksomhed</th>
</tr>";

while($row = mysql_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['CompanyName'] . "</td>";
  echo "<td>" . $row['CompanyLogo'] . "</td>";
  echo "<td>" . $row['CompanyField'] . "</td>";
  echo "<td>" . $row['ContactName'] . "</td>";
  echo "<td>" . $row['ContactPhone'] . "</td>";
  echo "<td>" . $row['ContactEmail'] . "</td>";
  echo "<td>" . $row['ContactImage'] . "</td>";
  echo "<td>" . $row['CompanyDetail'] . "</td>";
  echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

When I try to fetch the data, I get this message in console:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://myurl.com/get.php?q=1

I know this would be simple enough for some of you and would appreciate all feedback. I've checked my code several times, but can't find the error?

I'm a newbie as you can probably tell.

Both files are en the same directory.

UPDATE!!

I've changed mysqli to mysql in the files. Now I get this error message:

GET http://myurl.com/get.php?q=1 500 (Internal Server Error) 
company.html:21
showCompany company.html:21
onchange company.html:28
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
    • ¥30 eclipse开启服务后,网页无法打开
    • ¥30 雷达辐射源信号参考模型
    • ¥15 html+css+js如何实现这样子的效果?
    • ¥15 STM32单片机自主设计
    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
    • ¥15 不小心不正规的开发公司导致不给我们y码,
    • ¥15 我的代码无法在vc++中运行呀,错误很多
    • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
    • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000