Php错误消息“未捕获异常'异常',消息'查询失败:数组”

时间:2016-06-27 04:47:03

标签: php sql-server simpleusers

我试图将一个表从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堆栈跟踪:#0 C:\ Users \ Adam \ Desktop \ SimpleUsers MSSQL \ Useraccess.php(8):SimpleUsers-&gt; getUsers()#1 {main}在第29行的C:\ Users \ Adam \ Desktop \ SimpleUsers MSSQL \ Useraccess.php中抛出

3 个答案:

答案 0 :(得分:1)

您的conn类属性未设置。

在致电$stmt = sqlsrv_query($this->conn, $sql);之前,您必须在sqlsrv_connect

中进行设置

尝试添加construct:

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

答案 1 :(得分:1)

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

?> 

希望它会有所帮助。

答案 2 :(得分: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"]);

?>