Warning: sqlsrv_fetch_array() expects Parameter 1 to be Resource, Boolean Given in D: xampp5.6 htdocs sysriomed bank-material.php on line 27

Asked

Viewed 1,193 times

0

I am making a form for editing materials (products). I am viewing the list of materials and I created in my form a button that passes my IdItem. I’m in error:

Warning: sqlsrv_fetch_array() expects Parameter 1 to be Resource, Boolean Given in D: xampp5.6 htdocs sysriomed bank-material.php on line 27

inserir a descrição da imagem aqui

function buscaMaterial($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 am working with SQL Server 2014 and PHP 5.6.

  • banco-material.php is where are my duties related to materials.
  • material-altera-formulario is the data editing form.
  • lista-material.php is my material list.
  • Looks like your query failed.

  • But in the database it works

  • Put the query code there.

  • I edited by code. I had already posted all the content in git. sqlsrv_fetch_array return multiple or one result?

  • 1

    Leave it at that: $resultado = sqlsrv_query($conn, $query) or die(print_r(sqlsrv_errors())); Returns only one element.

  • Returned this: Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 156 [code] => 156 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax pr xima keyword 'on'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax pr xima keyword 'on'. ) )

  • Has syntax error :P seems to have missed the INNER JOIN before the ON

  • Array ( [0] => Array ( [0] => 42S22 [SQLSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Column name 'Iditem' read. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Column name 'Iditem' read. ) ) 1

  • I put and is giving that column name is invalid

  • vc cannot use the alias in Where or Join, should use the original column name in case I.id

Show 6 more comments

1 answer

4


Warning: sqlsrv_fetch_array() expects Parameter 1 to be Resource, Boolean Given in

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

As a test you can leave the code this way:

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

In that case the problem was the lack of JOIN formerly claudia ON and do not use the original column name 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}
  • But this only identifies. Does not resolve the error. In SQL it works

  • @fabricio_wm updated the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.