Warning: sqlsrv_fetch_array () expects parameter 1 to be resource, boolean given in D: \ xampp5.6 \ htdocs \ sysriomed \ bank-material.php on line 27

0

I am making a form for editing materials (products). I'm viewing the bill of materials and I created on my form a button that passes my IdItem . I'm wrong:

  

Warning: sqlsrv_fetch_array () expects parameter 1 to be resource,   boolean given in D: \ xampp5.6 \ htdocs \ sysriomed \ banco-material.php on   line 27

functionbuscaMaterial($conn,$IdItem){$query="select m.id as IdMat, I.id as IdItem, M.nome, M.tipo, I.nSerie ,I.valorUnitario from medx.dbo.material as M, medx.dbo.itemMaterial as I on M.id=I.idMaterial where IdItem = {$IdItem}";
    $resultado = sqlsrv_query($conn, $query);
    return sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC);
}

I'm working with SQL Server 2014 and PHP 5.6.

  • banco-material.php is where my functions related to the materials are.
  • material-altera-formulario is the data edit form.
  • lista-material.php is my list of material.
asked by anonymous 26.10.2016 / 18:11

1 answer

4
  Warning: sqlsrv_fetch_array () expects parameter 1 to be resource, boolean given in

This says there is some kind of error in your query, whether syntax or constraint violation, for example. To be sure of what happened use the function sqlsrv_errors () it will return an array with the information of the error.

As a test you can leave the code like this:

$resultado = sqlsrv_query($conn, $query) or die(print_r(sqlsrv_errors()));

In this case the problem was the lack of the JOIN before the ON and not use the original name of the column in WHERE

Original code:

SELECT m.id as IdMat, I.id as IdItem, M.nome, M.tipo, I.nSerie ,I.valorUnitario
FROM medx.dbo.material as M, medx.dbo.itemMaterial as I on M.id=I.idMaterial
WHERE IdItem = {$IdItem}

Corrected code:

SELECT m.id as IdMat, I.id as IdItem, M.nome, M.tipo, I.nSerie ,I.valorUnitario
FROM medx.dbo.material as M INNER JOIN medx.dbo.itemMaterial as I  on M.id = I.idMaterial
WHERE I.id = {$IdItem}
    
26.10.2016 / 18:39