我正在尝试使用ajax,php和MySql获取结果。但是,我在服务器端脚本上遇到以下错误。
警告:mysqli_select_db()期望参数1为mysqli,resource 在第10行的D:\ htdocs \ classes \ xxx中给出
警告:mysql_query():提供的参数不是有效的MySQL-Link 资源
服务器端代码如下:
<?php
$q = intval($_GET['q']);
$con= mysqli_connect("localhost","root","abcd123") or die ("could not connect to mysql");
mysqli_select_db($con,"payrolldb001") or die ("no database");
$sql="SELECT substationid,substationcode FROM wms_substation WHERE assemblylineid = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<select>";
while($row = mysqli_fetch_array($result))
{
echo "here";
echo "<option>". $row['substationcode'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
我无法弄清楚我哪里出错了。请帮忙。
答案 0 :(得分:4)
试试这个,
$con = mysqli_connect("localhost","root","abcd123","payrolldb001") or die("Error " . mysqli_error($con));
$sql="SELECT substationid,substationcode FROM wms_substation WHERE assemblylineid = '".$q."'";
$result = mysqli_query($con,$sql);
...
或强>
$con= mysqli_connect("localhost","root","abcd123") or die ("could not connect to mysql");
mysqli_select_db($con,"payrolldb001") or die ("no database");
答案 1 :(得分:3)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','dbu','password','dbname');
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//MySqli Select Query
$results = $mysqli->query("SELECT * FROM users");
while($row = $results->fetch_assoc()) {
echo $row["id"];
}
// Frees the memory associated with a result
$results->free();
// close connection
$mysqli->close();