I am trying to make a query with PHP
to a database in SQLServer
with ajax
and MVC
but when I execute the query it throws me the following error
sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\Personal\modelos\usuarios.modelo.php</b> on line <b>49</b><br />
This is the way I sent the input via JS
to the ajax file
var validarUsuarioRepetido = false;
var rutaOculta = $("#rutaOculta").val();
$("#password").change(function(){
var usuarioPassword = $('#password').val();
var datos = new FormData();
datos.append("validarUsuario", usuarioPassword);
$.ajax({
url: rutaOculta+"ajax/usuarios.ajax.php",
method: "POST",
data: datos,
cache: false,
contentType: false,
processData: false,
success:function(respuesta){
$("#user").val(respuesta);
console.log(respuesta);
}
})
})
later the ajax receives it in the following way and sends it to the controller
<?php
require_once "../controladores/usuarios.controlador.php";
require_once "../modelos/usuarios.modelo.php";
class AjaxUsuarios{
/**
* Validar Usuario
**/
public $validarUsuario;
public function ajaxValidarUsuario(){
$datos = $this->validarUsuario;
$respuesta = ControladorUsuarios::ctrMostrarUsuarioP($datos);
echo ($respuesta);
}
}
/**
* Validar Usuario
**/
if (isset($_POST["validarUsuario"])){
$valUsuario = new AjaxUsuarios();
$valUsuario -> validarUsuario = $_POST["validarUsuario"];
$valUsuario -> ajaxValidarUsuario();
}
When the controller receives it, he sends it to the model to generate the query and return it
static public function mdlMostrarUsuarioP($tabla, $datos){
$serverName = "SERVIDOR"; //serverName\instanceName
$connectionInfo = array( "Database"=>"BASEDEDATOS", "UID"=>"USUARIO", "PWD"=>"PSSWORD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$sql = "SELECT * FROM $tabla WHERE CB_ID_NUM=?";
$stmt1 = sqlsrv_prepare($conn, $sql, array($datos));
$result = sqlsrv_execute($stmt1);
return $stmt3 = sqlsrv_fetch_array($result);
}
and the moment it enters the sqlsrv_fetch_array is where I get the error mentioned above, I hope someone can help me, this is the first time I try to generate the connection and a query with SQLServer