user7409253
user7409253

Reputation: 103

PHP fatal error - Call to a member function prepare() on resource

I have looked at other questions but i can't see a link between mine and others issues so i was wondering if i could get some advice on where i am going wrong.

I read about not mixing API's but im not sure if i am or not?

submitNoteText.php:

?php include 'connectionDetails.php'; ?>

<?php


if (isset($_POST['noteid1'], $_POST['notetext1'])) 
{
    var_dump($_POST['notetext1']);

    $stmt = $conn->prepare("UPDATE Notes SET Note = ? WHERE NoteID = ?");
    $stmt->bind_param("si", $notetext2, $noteid2);

    $noteid2 = $_POST['noteid1'];
    $notetext2 = $_POST['notetext1'];
    $stmt->execute();

    $stmt->close();
}
else
{
    if (isset($_POST['notetext1'])) {
        var_dump($notetext2);
    }
    else
    {
        echo "Test";
    }

}


?>

connectionDetails.php:

<?php
$myServer = "test ip";
$connectionInfo = array('Database' => 'DiscoverThePlanet', 'UID' => 'Test', 'PWD' => 'Test');

//connection to the database
$conn = sqlsrv_connect($myServer, $connectionInfo)
  or die("Couldn't connect to SQL Server on $myServer"); 

//Test connection to server
// if ($conn) 
// {
//     echo "connection successful";    # code...
// }

?>

The error is in the submitNoteText.php and is this line:

$stmt = $conn->prepare("UPDATE Notes SET Note = ? WHERE NoteID = ?");

Upvotes: 0

Views: 4823

Answers (2)

Vini
Vini

Reputation: 626

You are calling variables before you declare them

$stmt = $conn->prepare("UPDATE Notes SET Note = ? WHERE NoteID = ?");
$stmt->bind_param("si", $notetext2, $noteid2);

$noteid2 = $_POST['noteid1'];
$notetext2 = $_POST['notetext1'];

Switch order to

$noteid2 = $_POST['noteid1'];
$notetext2 = $_POST['notetext1'];

$stmt = $conn->prepare("UPDATE Notes SET Note = ? WHERE NoteID = ?");
$stmt->bind_param("si", $notetext2, $noteid2);

Ok, i just noticed :

you are calling :

$conn->prepare()

but $conn is not instance of PDO. You are mixing api. Change your $conn according to docs: http://php.net/manual/pl/pdo.construct.php

Upvotes: 2

ʰᵈˑ
ʰᵈˑ

Reputation: 11375

The sqlsrv_* API doesn't have a prepare() method. You're mixing database APIs.

Either use PDO or use sqlsrv_query()

Upvotes: 3

Related Questions