我的代码有4个文件。
第一个是主页:
<?php
session_start();
$pagetitle = "Schedules";
include_once('head.php');
include_once('navbar.php');
?>
<body>
Name:<input type="text" id="DEPID" />
<input type="submit" id="DEPID-Submit" value="Search for Flight" />
<div id="DEPID-data"></div>
<script src="jquery-1.8.2.min.js"></script>
<script src="js/global.js"></script>
</body>
<?php
include_once('footer.php');
include_once('scripts.php');
?>
代码的第二部分是php编码:
<?php
if( isset($_POST['DEPID']) === true && empty($_POST['DEPID']) === false) {
require'../db/connect.php';
$query = mysql_query("SELECT * FROM Flights WHERE DEPID ='DEPID'");
$result = mysql_query($query, $mysql_connect) or die(mysql_error());
echo "<table><tr><th>Flight ID</th><th>Departure Airport</th><th>Arrival Airport</th><th>Distance</th></tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr><td>" . $row['FLID'] . "</td><td>" . $row['DEPID'] . "</td><td>" . $row['ARRID'] . "</td><td>" . $row['Distance'] . "</td></tr>";
}
echo "</table>";
}
?>
代码的第三部分是我与数据库连接的地方:
<?php
$link = mysql_connect('localhost', 'ak118043_ako', '2391990ak4726790');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('ak118043_Project', $link);
if (!$db_selected) {
die ('Can\'t use ak118043_Project : ' . mysql_error());
}
?>
第四个是ajax编码:
$('input#DEPID-Submit').on('click', function(){
var DEPID= $('input#DEPID').val();
if($.trim(DEPID) != ''){
$.post('ajax/name.php', {DEPID: DEPID}, function(data){
$('div#DEPID-data').text(data);
});
}
});
问题是以下错误消息:
警告:mysql_query()期望参数1为字符串,资源在 /home/ak118043/public_html/ajax/name.php 在线提供 9
答案 0 :(得分:2)
首先,您要查询两次并使用mysql_error()
向您显示语法错误。
错误在于这两行:
$query = mysql_query("SELECT * FROM Flights WHERE DEPID ='DEPID'");
$result = mysql_query($mysql_connect,$query)or die ("Error");
删除第二行查询行中的mysql_query()
作为第9行:
$result = ($mysql_connect,$query) or die ("Error");
但该行$mysql_connect
似乎是您的连接变量,应该在之后而不是第一个,因为这是mysqli_
语法。
$result = ($query, $mysql_connect) or die ("Error");
修改:看到您的连接代码后
这是不正确的。
mysql_connect("localhost","ak118043_ako", "2391990ak4726790", "ak118043_Project");
您正在使用四个参数,您应该只使用三个参数。 mysqli_
而不是mysql_
使用了四个参数。
在PHP.net http://php.net/manual/en/function.mysql-select-db.php中关注此示例,并使用您自己的凭据替换:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
因为$mysql_connect
未定义且您没有变量。
现在,而不是or die ("Error")
执行or die(mysql_error())
并获取真正的错误。
同样if( isset($_POST['DEPID']) === true && empty($_POST['DEPID']) ===false){
您可以将其替换为if(!empty($_POST['DEPID'])){
编辑#2 (快速mysqli重写让你前进)
将整个代码更改为以下内容:
<?php
$DB_HOST = 'localhost';
$DB_USER = 'xxx'; // change this to your own
$DB_PASS = 'xxx'; // change this to your own
$DB_NAME = 'your_database'; // change this to your own
$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
die('Connection failed [' . $Link->connect_error . ']');
}
?>
然后
<?php
if(!empty($_POST['DEPID'])){
require'../db/connect.php';
$query = "SELECT * FROM Flights WHERE DEPID ='DEPID'";
$result = mysqli_query($Link, $query) or die(mysqli_error($Link));
echo "<table><tr><th>Flight ID</th><th>Departure Airport</th><th>Arrival Airport</th><th>Distance</th></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['FLID'] . "</td><td>" . $row['DEPID'] . "</td><td>" . $row['ARRID'] . "</td><td>" . $row['Distance'] . "</td></tr>";
}
echo "</table>";
}
保存面孔
$result = mysql_query($query, $mysql_connect)
,您使用了错误的变量。 $mysql_connect
它应该是$link
答案 1 :(得分:0)
删除$result
行,并在while
周期更改变量名称。
$query = mysql_query("SELECT * FROM Flights WHERE DEPID ='DEPID'");
// $result = mysql_query($mysql_connect,$query)or die ("Error"); //Line 9
echo "<table><tr><th>Flight ID</th><th>Departure Airport</th><th>Arrival Airport</th><th>Distance</th></tr>";
while($row = mysql_fetch_array($query)) {...
^^
或从第一行删除mysql_query
:
$query = "SELECT * FROM Flights WHERE DEPID ='DEPID'";
$result = mysql_query($mysql_connect,$query)or die ("Error"); //Line 9
echo "<table><tr><th>Flight ID</th><th>Departure Airport</th><th>Arrival Airport</th><th>Distance</th></tr>";
while($row = mysql_fetch_array($result)) {...