Fatal error: Call to a member function query() on a non-object XAMPP to SQL SERVER 2012
Fatal error: Call to a member function query() on a non-object XAMPP to SQL SERVER 2012
我可以就此事寻求一些指导,我正在 PHP 到 SQL SERVER 上尝试一些测试代码和指南,mysqli
似乎可以与 phpmyadmin 一起正常工作下面的代码,但是,sqlsrv_connect
它似乎不起作用,我知道当涉及到 MSSQL 时有不同的结构,我可以要求 link 或任何来源我可以找到用于连接(或调用从 PHP 到 SQL SERVER 2012 的查询)?
我确实检查了这里提出的所有问题,不幸的是大多数问题都是基于 mysqli 的,我没有看到(或者可能已经跳过)与 SQL SERVER 2012 相关的问题。
我尝试了这个指南 $query = mysqli_query($conn, $sql);
,但没有成功。
这是 login.php
的代码
session_start();
include 'includes/conn.php';
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM usermasterfile WHERE username = '$username'";
$query = $conn->query($sql);
if($query->num_rows < 1){
$_SESSION['error'] = 'Cannot find account with the username';
}
else{
$row = $query->fetch_assoc();
if(password_verify($password, $row['password'])){
$_SESSION['admin'] = $row['id'];
}
else{
$_SESSION['error'] = 'Incorrect password';
}
}
}
else{
$_SESSION['error'] = 'Input admin credentials first';
}
header('location: index.php');
?>
而且,这是 conn.php
的代码
$serverName = "XXXXXX\SQLSERVER";
$connectionInfo = array( "Database"=>"XXXXXX", "UID"=>"XXXXXX", "PWD"=>"xxxxxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
此代码工作文件(用于 phpmyadmin)
$conn = new mysqli('xxxx', 'xxxx', '', 'xxxx');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
连接建立,但错误依旧
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\path\tofile\login.php on line 9
函数 sqlsrv_*
是 PHP Driver for SQL Server 的一部分,它们不是面向对象的。这就是你错误的原因。
您的 conn.php
似乎是正确的,所以只需在 login.php
中使用适当的 sqlsrv_*
函数即可:
<?
session_start();
include 'includes/conn.php';
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM usermasterfile WHERE username = '$username'";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($query === false ) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
if (sqlsrv_num_rows($query) < 1) {
$_SESSION['error'] = 'Cannot find account with the username';
} else {
$row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
if (password_verify($password, $row['password'])) {
$_SESSION['admin'] = $row['id'];
}
else{
$_SESSION['error'] = 'Incorrect password';
}
}
}
else{
$_SESSION['error'] = 'Input admin credentials first';
}
header('location: index.php');
?>
我可以就此事寻求一些指导,我正在 PHP 到 SQL SERVER 上尝试一些测试代码和指南,mysqli
似乎可以与 phpmyadmin 一起正常工作下面的代码,但是,sqlsrv_connect
它似乎不起作用,我知道当涉及到 MSSQL 时有不同的结构,我可以要求 link 或任何来源我可以找到用于连接(或调用从 PHP 到 SQL SERVER 2012 的查询)?
我确实检查了这里提出的所有问题,不幸的是大多数问题都是基于 mysqli 的,我没有看到(或者可能已经跳过)与 SQL SERVER 2012 相关的问题。
我尝试了这个指南 $query = mysqli_query($conn, $sql);
,但没有成功。
这是 login.php
的代码 session_start();
include 'includes/conn.php';
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM usermasterfile WHERE username = '$username'";
$query = $conn->query($sql);
if($query->num_rows < 1){
$_SESSION['error'] = 'Cannot find account with the username';
}
else{
$row = $query->fetch_assoc();
if(password_verify($password, $row['password'])){
$_SESSION['admin'] = $row['id'];
}
else{
$_SESSION['error'] = 'Incorrect password';
}
}
}
else{
$_SESSION['error'] = 'Input admin credentials first';
}
header('location: index.php');
?>
而且,这是 conn.php
的代码$serverName = "XXXXXX\SQLSERVER";
$connectionInfo = array( "Database"=>"XXXXXX", "UID"=>"XXXXXX", "PWD"=>"xxxxxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
此代码工作文件(用于 phpmyadmin)
$conn = new mysqli('xxxx', 'xxxx', '', 'xxxx');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
连接建立,但错误依旧
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\path\tofile\login.php on line 9
函数 sqlsrv_*
是 PHP Driver for SQL Server 的一部分,它们不是面向对象的。这就是你错误的原因。
您的 conn.php
似乎是正确的,所以只需在 login.php
中使用适当的 sqlsrv_*
函数即可:
<?
session_start();
include 'includes/conn.php';
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM usermasterfile WHERE username = '$username'";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($query === false ) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
if (sqlsrv_num_rows($query) < 1) {
$_SESSION['error'] = 'Cannot find account with the username';
} else {
$row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
if (password_verify($password, $row['password'])) {
$_SESSION['admin'] = $row['id'];
}
else{
$_SESSION['error'] = 'Incorrect password';
}
}
}
else{
$_SESSION['error'] = 'Input admin credentials first';
}
header('location: index.php');
?>