簡體   English   中英

php錯誤消息“未捕獲的異常'Exception'和消息'Query Failed:Array”

[英]Php Error Message “ Uncaught exception 'Exception' with message 'Query Failed:Array”

我試圖將表格從MS-SQL數據庫呈現到網頁,但出現此錯誤。 我還是PHP新手。 請幫忙

Useraccess.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為資源,在第26行的C:\\ Users \\ Adam \\ Desktop \\ SimpleUsers MSSQL \\ Useraccess.php中給出null

注意:第29行的C:\\ Users \\ Adam \\ Desktop \\ SimpleUsers MSSQL \\ Useraccess.php中的數組到字符串的轉換

致命錯誤:C:\\ Users \\ Adam \\ Desktop \\ SimpleUsers MSSQL \\ Useraccess.php:29中消息“查詢失敗:數組”的未捕獲異常'Exception'堆棧跟蹤:#0 C:\\ Users \\ Adam \\ Desktop \\ SimpleUsers MSSQL \\ Useraccess.php(8):第29行上的C:\\ Users \\ Adam \\ Desktop \\ SimpleUsers MSSQL \\ Useraccess.php中拋出的SimpleUsers-> getUsers()#1 {main}

您的conn類屬性未設置。

在調用$stmt = sqlsrv_query($this->conn, $sql); 您必須在sqlsrv_connect中進行設置。

嘗試添加構造:

public function __construct()
{
    $this->conn = sqlsrv_connect($GLOBALS["serverName"], $GLOBALS["connectionInfo"]);
}

Useraccess.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"]  );

?> 

希望它會有所幫助。

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"]);

?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM