Fatal error: Call to a member function prepare () on a non-object in

1

Good morning, when I try to consult a folio on my form I get an error that says:

  

Fatal error: Call to a member function prepare () on a non-object in

Here my code:

<?php
$serverName = "10.21.22.16"; //serverName\instanceName
$connectionInfo = array( "Database"=>"Siec", "UID"=>"sa", "PWD"=>"k115");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$result="";
if(array_key_exists("search",$_POST)){
$consul= 'SELECT SOL.FOLIO, SOL.ESTATUSPAG, SV.TOTALP, CS.SERVICIO 
FROM 
SOLICITU SOL
INNER JOIN SERVSOL SV ON SV.FOLIO=SOL.FOLIO
INNER JOIN  CATSERV CS ON CS.SERVS = SV.SERVS
WHERE SOL.FOLIO = ?';
$stmt=$conn->prepare($consul);
$result=$stmt->execute(array($_POST['folio']));
$result=$stmt->fetch();
if(empty($result)){
$result = "No se encontraron resultados!!";
}
}
?>
<html>
<body>
<form action="" method="post">
<div>
<label for="folio">folio: </label>
<input type="text" name="folio" id="folio"/>
<input type="submit" name="search" value="Buscar" />
</div>
</form>
</body>
<?php
if( !empty($result) ) {
?>
<p><strong>folio</strong>: <?php echo $result['folio'];?></p>
<p><strong>Estatus Pagado</strong>: <?php echo $result['Estatuspag'];?></p>
<p><strong>Total</strong>: <?php echo $result['Totalp'];?></p>
<p><strong>Servicio</strong>: <?php echo $result['SERVICIO'];?></p>
<?php
}
?>
</html>

It's just on that line where the error marks me

  

$ stmt = $ conn-> prepare ($ consul);

Will someone know why? Thanks for your answers

    
asked by Augustoock 27.02.2017 в 16:40
source

2 answers

2

It seems that your problem is in the connection, you can use the example of the official documentation and verify if the connection was successful

$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Conexión establecida.<br />";
}else{
     echo "Conexión no se pudo establecer.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

The variable $conn is returning FALSE and not a resource, so it shows you the error

EDIT:

Your error is because the variable $conn is of type resource no object

If we review the official documentation, to make a prepared query you need to use the function sqlsrv_prepare ()

Summing up your code goes like this:

$serverName = "10.21.22.16"; //serverName\instanceName
$connectionInfo = array( "Database"=>"Siec", "UID"=>"sa", "PWD"=>"k115");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$result="";
if(array_key_exists("search",$_POST))
{
    $consul= "SELECT 
                  SOL.FOLIO, 
                  SOL.ESTATUSPAG, 
                  SV.TOTALP, 
                  CS.SERVICIO 
              FROM 
                  SOLICITU SOL
                  INNER JOIN SERVSOL SV ON SV.FOLIO=SOL.FOLIO
                  INNER JOIN  CATSERV CS ON CS.SERVS = SV.SERVS
              WHERE SOL.FOLIO = ?";

    $stmt = sqlsrv_prepare($conn, $consul, array($_POST['folio']));

    if( !$stmt ) 
    {
        $result = "Error en la consulta";
        //die( print_r( sqlsrv_errors(), true));
    }

    if( sqlsrv_execute( $stmt ) === false ) 
    {
      $result = "No se encontraron resultados!!";
      //die( print_r( sqlsrv_errors(), true));
    }
    else
    {
        $result = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)
    }            

    sqlsrv_free_stmt($stmt);
}

On the other hand, I recommend you use PDO which also has support for MS SQL

Using POO is much easier and your code would look something like this:

$conn = new PDO('sqlsrv:Server=10.21.22.16;Database=Siec','sa','k115');

if(array_key_exists("search",$_POST))
{
    $consul= "SELECT 
                  SOL.FOLIO, 
                  SOL.ESTATUSPAG, 
                  SV.TOTALP, 
                  CS.SERVICIO 
              FROM 
                  SOLICITU SOL
                  INNER JOIN SERVSOL SV ON SV.FOLIO=SOL.FOLIO
                  INNER JOIN  CATSERV CS ON CS.SERVS = SV.SERVS
              WHERE SOL.FOLIO = ?";

    $stmt = $conn->prepare($consul);
    $folio = $_POST['folio'];
    $stmt->bindParam(1, $folio, PDO::PARAM_STR);

    if( !$stmt->execute() ) 
    {
        $result = "Error en la consulta";
    }
    else
    {
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
    }            

}

You can read the official documentation of PDO to clarify any questions:)

    
answered by 27.02.2017 / 21:51
source
2

Your sentence will never work because you have forgotten some important steps, one is, bind_param() , that's where the error throws you, you must pass the parameter of your WHERE SOL.FOLIO =? , that is, your variable $_POST['folio']

  

mysqli_stmt :: bind_param - mysqli_stmt_bind_param - Add variables to a prepared statement as parameters

Specification of the type of characters
i the corresponding variable is of type integer
d the corresponding variable is of type double
s the corresponding variable is of type string
b the corresponding variable is a blob and it is sent in packets

Updated example:

$consul= 'SELECT SOL.FOLIO, SOL.ESTATUSPAG, SV.TOTALP, CS.SERVICIO 
FROM 
SOLICITU SOL
INNER JOIN SERVSOL SV ON SV.FOLIO=SOL.FOLIO
INNER JOIN  CATSERV CS ON CS.SERVS = SV.SERVS
WHERE SOL.FOLIO = ?';    
$stmt=$conn->prepare($consul);
$stmt->bind_param("Especificación del tipo de caracteres", $tu_variable_folio);
$stmt->execute();
//Transfiere un conjunto de resulados de la última consulta
$stmt->store_result();
//Comprobamos si existen registros en la Base de datos.
if($stmt->num_rows>0){
  //Vincula variables a una sentencia preparada para el almacenamiento de resultados.
  $stmt->bind_result($FOLIO,$ESTATUSPAG,$TOTALP,$SERVICIO);//Lo que has seleccionado en tu SELECT
  $stmt->fetch();
  //Ya puedes trabajar con tus datos desde la Base de datos.
} else {
   echo 'Se encontaron 0 resultados.";
}
$stmt->close();//Cerramos sentencia.

Your HTML would look like this:

<p><strong>folio</strong>: <?php echo $FOLIO;?></p>
<p><strong>Estatus Pagado</strong>: <?php echo $ESTATUSPAG;?></p>
<p><strong>Total</strong>: <?php echo $TOTALP;?></p>
<p><strong>Servicio</strong>: <?php echo $SERVICIO;?></p>

PHP Manual prepared statements .

  

Note: You must also take into account the correct order of your parameters to create your connection.

Example:

$conn = new mysqli("localhost", "usuario", "contraseña", "basedatos");
    
answered by 28.02.2017 в 00:33