2

MS-SQL データベースから Web ページにテーブルをレンダリングしようとしたところ、このエラーが発生しました。私はまだPHPの初心者です。助けてください

ユーザーアクセス.php

 <?php


        $path = dirname(__FILE__);
        require_once(dirname(__FILE__)."/simpleusers/config.inc.php");

        $SimpleUsers = new SimpleUsers();
        $users = $SimpleUsers->getUsers();

        class SimpleUsers
        {

            private $mysqli , $stmt;
            private $conn;
            private $sessionName = "SimpleUsers";
            public $logged_in = false;
            public $userdata;
            public $uPassword;
            public $salt;


           public function getUsers()
            {
                $sql = "SELECT DISTINCT userId, uUsername, uActivity, uCreated FROM users ORDER BY uUsername ASC";

                $stmt = sqlsrv_query($this->conn, $sql);

                if( $stmt == false){
                    throw new Exception("Query Failed:".sqlsrv_errors());
                }
                $stmt->execute();
                $stmt->store_result();

                if( $stmt->num_rows == 0){
                    return array();
                }


                $users = array();
                $i = 0;

                while( $stmt->fetch() )
                {       
                    $users[$i]["userId"] = $userId;
                    $users[$i]["uUsername"] = $username;
                    $users[$i]["uActivity"] = $activity;
                    $users[$i]["uCreated"] = $created;

                    $i++;
                }
            }               
        }   
    ?>


    <html>
        <head>
            <title></title>
          <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
          <style type="text/css">

                * { margin: 0px; padding: 0px; }
                body
                {
                    padding: 30px;
                    font-family: Calibri, Verdana, "Sans Serif";
                    font-size: 12px;
                }
                table
                {
                    width: 800px;
                    margin: 0px auto;
                }

                th, td
                {
                    padding: 3px;
                }

                .right
                {
                    text-align: right;
                }

            h1
            {
                color: #FF0000;
                border-bottom: 2px solid #000000;
                margin-bottom: 15px;
            }

            p { margin: 10px 0px; }
            p.faded { color: #A0A0A0; }

          </style>

        </head>
        <body>

            <h1>User administration</h1>
            <table cellpadding="0" cellspacing="0" border="1">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Last activity</th>
                        <th>Created</th>
                        <th></th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="4" class="right">
                            <a href="newuser.php">Create new user</a> | <a href="logout.php">Logout</a>
                        </td>
                    </tr>
                </tfoot>
                <tbody>
                    <?php foreach
                    ( $users as $user ): ?>
                    <tr>
                        <td><?php echo $user["uUsername"]; ?></td>
                        <td class="right"><?php echo $user["uActivity"]; ?></td>
                        <td class="right"><?php echo $user["uCreated"]; ?></td>
                        <td class="right"><a href="deleteuser.php?userId=<?php echo $user["userId"]; ?>">Delete</a> | <a href="userinfo.php?userId=<?php echo $user["userId"]; ?>">User info</a> | <a href="changepassword.php?userId=<?php echo $user["userId"]; ?>">Change password</a></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

        </body>
    </html> 

config.inc.php

<?php

$GLOBALS["serverName"] = "DESKTOP-KRF6KT7\SQLEXPRESS";  
$GLOBALS["database"] = "SimpleUsers";  
$GLOBALS["uid"] = "sa";  
$GLOBALS["pwd"] = "twinz0000";  
$GLOBALS["connectionInfo"] = array(
                                "Database"=>$GLOBALS["database"],
                                "UID"=>$GLOBALS["uid"],
                                "PWD"=>$GLOBALS["pwd"])

?> 

エラー表示

警告: sqlsrv_query() は、パラメーター 1 がリソースであると想定しています。null は C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php 行 26 で指定されています

注意: C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php の 29 行目の配列から文字列への変換

致命的なエラー: C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php:29 のメッセージ 'Query Failed:Array' を伴うキャッチされない例外 'Exception' スタック トレース: #0 C:\Users\Adam\Desktop\SimpleUsers MSSQL \Useraccess.php(8): SimpleUsers->getUsers() #1 {main} が C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php 行 29 でスローされる

4

3 に答える 3

1

connクラス プロパティが設定されていません。

呼び出す前に、 sqlsrv_connect$stmt = sqlsrv_query($this->conn, $sql);で設定する必要があります。

コンストラクトを追加してみてください:

public function __construct()
{
    $this->conn = sqlsrv_connect($GLOBALS["serverName"], $GLOBALS["connectionInfo"]);
}
于 2016-06-27T04:56:28.147 に答える
1
I have these two items in my php.ini file enabled:

extension=php_pdo_sqlsrv_53_ts.dll
extension=php_sqlsrv_53_ts.dll

Which is what I had before. Using Wamp Server. PHP 5.3.13 all the same as before. What else am I missing that is not allowing this to connect to the SQL server.

After connection file code.
<?php

$GLOBALS["serverName"] = "localhost";  
$GLOBALS["database"] = "SimpleUsers";  
$GLOBALS["uid"] = "root";
$GLOBALS["pwd"] = "";  
$GLOBALS["connectionInfo"] = array(
                                "Database"=>$GLOBALS["database"],
                                "UID"=>$GLOBALS["uid"],
                                "PWD"=>$GLOBALS["pwd"]);

$conn = sqlsrv_connect($GLOBALS["serverName"], $GLOBALS["connectionInfo"]);

?> 
于 2016-06-27T05:39:43.800 に答える
1

ユーザーアクセス.php

 <?php


            $path = dirname(__FILE__);
            require_once(dirname(__FILE__)."/simpleusers/config.inc.php");

            $SimpleUsers = new SimpleUsers();
            $users = $SimpleUsers->getUsers();

            class SimpleUsers
            {

                private $mysqli , $stmt;
                private $conn;
                private $sessionName = "SimpleUsers";
                public $logged_in = false;
                public $userdata;
                public $uPassword;
                public $salt;
                public $conn=$GLOBALS["conn"] ;

               public function getUsers()
                {
                    $sql = "SELECT DISTINCT userId, uUsername, uActivity, uCreated FROM users ORDER BY uUsername ASC";

                    $stmt = sqlsrv_query($this->conn, $sql);

                    if( $stmt == false){
                        throw new Exception("Query Failed:".sqlsrv_errors());
                    }
                    $stmt->execute();
                    $stmt->store_result();

                    if( $stmt->num_rows == 0){
                        return array();
                    }


                    $users = array();
                    $i = 0;

                    while( $stmt->fetch() )
                    {       
                        $users[$i]["userId"] = $userId;
                        $users[$i]["uUsername"] = $username;
                        $users[$i]["uActivity"] = $activity;
                        $users[$i]["uCreated"] = $created;

                        $i++;
                    }
                }               
            }   
        ?>


        <html>
            <head>
                <title></title>
              <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
              <style type="text/css">

                    * { margin: 0px; padding: 0px; }
                    body
                    {
                        padding: 30px;
                        font-family: Calibri, Verdana, "Sans Serif";
                        font-size: 12px;
                    }
                    table
                    {
                        width: 800px;
                        margin: 0px auto;
                    }

                    th, td
                    {
                        padding: 3px;
                    }

                    .right
                    {
                        text-align: right;
                    }

                h1
                {
                    color: #FF0000;
                    border-bottom: 2px solid #000000;
                    margin-bottom: 15px;
                }

                p { margin: 10px 0px; }
                p.faded { color: #A0A0A0; }

              </style>

            </head>
            <body>

                <h1>User administration</h1>
                <table cellpadding="0" cellspacing="0" border="1">
                    <thead>
                        <tr>
                            <th>Username</th>
                            <th>Last activity</th>
                            <th>Created</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <td colspan="4" class="right">
                                <a href="newuser.php">Create new user</a> | <a href="logout.php">Logout</a>
                            </td>
                        </tr>
                    </tfoot>
                    <tbody>
                        <?php foreach
                        ( $users as $user ): ?>
                        <tr>
                            <td><?php echo $user["uUsername"]; ?></td>
                            <td class="right"><?php echo $user["uActivity"]; ?></td>
                            <td class="right"><?php echo $user["uCreated"]; ?></td>
                            <td class="right"><a href="deleteuser.php?userId=<?php echo $user["userId"]; ?>">Delete</a> | <a href="userinfo.php?userId=<?php echo $user["userId"]; ?>">User info</a> | <a href="changepassword.php?userId=<?php echo $user["userId"]; ?>">Change password</a></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>

            </body>
        </html> 

config.inc.php

<?php

$GLOBALS["serverName"] = "DESKTOP-KRF6KT7\SQLEXPRESS";  
$GLOBALS["database"] = "SimpleUsers";  
$GLOBALS["uid"] = "sa";  
$GLOBALS["pwd"] = "twinz0000";  
$GLOBALS["connectionInfo"] = array(
                                "Database"=>$GLOBALS["database"],
                                "UID"=>$GLOBALS["uid"],
                                "PWD"=>$GLOBALS["pwd"]);

$GLOBALS["conn"] = sqlsrv_connect($GLOBALS["serverName"], $GLOBALS["connectionInfo"]  );

?> 

それが役立つことを願っています。

于 2016-06-27T05:02:21.573 に答える