0

我正在尝试使用 ajax、php 和 MySql 获取结果。但是,我的服务器端脚本出现以下错误。

警告:mysqli_select_db() 期望参数 1 为 mysqli,第 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);
?>

我不知道我哪里出错了。请帮忙。

4

2 回答 2

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"); 

读取mysqli_select_db

于 2013-09-05T04:37:14.527 回答
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();
于 2017-10-04T10:26:09.917 回答