简体   繁体   中英

What is wrong with my PHP?

I am new to PHP, I watched a tutorial about SQL and PHP, and I made those files:

insert_news.php:

    require_once __DIR__ .'/db_connect.php';

    $sql = "INSERT INTO news ( descriere, data, autor) VALUES (?, ?, ?)" ;
    $params = array("The description", "12/11/2011", "COsmin");


    $serverName = "my server";
    $connectionInfo = array( "Database"=>"myusername", "UID"=>"myusername", "PWD"=>"mypassword");

    $conn = sqlsrv_connect($serverName, $connectionInfo );
    $stmt = sqlsrv_query( $conn , $sql, $params );

    if( $stmt == false ) {
        echo("NOT");
        die( print_r( sqlsrv_errors(), true));
    }

?>

This code seems to be working well.

Now my problem is that I wanted to make a class which will make the connections to the database more easier. That's the code:

db_connect.php

<?php
class DB_CONNECT {
    function __construct(){
        $this->connect();
    }

    function __Destruct(){
        $this->close();
    }

    function connect() {
        $serverName = "my server";
        $connectionInfo = array( "Database"=>"my db", "UID"=>"my user", "PWD"=>"my pass" );

        $con = sqlsrv_connect($serverName, $connectionInfo );
                return $con;
    }
    function close(){
        sqlsrv_close();
    }
}
?>

And if I want to call this class from my insert_new.php file like that:

    <?php

    require_once __DIR__ .'/db_connect.php';

    $sql = "INSERT INTO Stiri ( descriere, data, autor) VALUES (?, ?, ?)" ;
    $params = array("ASta e o stvnmbvire aduagata printr-un web service php", "12/11/2011", "COsn");

    $conn = new DB_CONNECT() ;
    $stmt = sqlsrv_query( $conn , $sql, $params );

    if( $stmt == false ) {
        echo("NU");
        die( print_r( sqlsrv_errors(), true));
    }
?>

When I try this, my server returns me that:

Warning: sqlsrv_query() expects parameter 1 to be resource, object given in C:\\xampp\\htdocs\\android_connect\\insert_news.php on line 9 NUArray ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -14 [code] => -14 [2] => An invalid parameter was passed to sqlsrv_query. [message] => An invalid parameter was passed to sqlsrv_query. ) )

I mention that I use XAMPP to compile the php files and the database is a SQL Server 2005... Sorry If I wasted your time with my silly stuff...

You are giving your custom object to the function sqlsrv_query instead of the DB Instance. In Your class, you should make the $con variable a public attribute like this:

class DB_CONNECT {
    public $con = false;
    function __construct(){
        $this->connect();
    }

    function __Destruct(){
        $this->close();
    }

    function connect() {
        $serverName = "my server";
        $connectionInfo = array( "Database"=>"my db", "UID"=>"my user", "PWD"=>"my pass" );

        $this->con = sqlsrv_connect($serverName, $connectionInfo );
                return $con;
    }
    function close(){
        sqlsrv_close();
    }
}

and give this attribute to your query function like this:

if($conn->con) {
   $stmt = sqlsrv_query( $conn->con , $sql, $params );
} else {
   // error
}

It seems as if you are not actaully connecting to the DB.

This is your problem

$conn = new DB_CONNECT();
$stmt = sqlsrv_query( $conn , $sql, $params );

$conn is not a connection here it is just a DB_CONNECT object. You need to call connect() method to get a connection. So try this:

$conn = new DB_CONNECT();
$stmt = sqlsrv_query( $conn->connect() , $sql, $params );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM