Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MYSQLI获取错误_Php_Mysql_Postgresql_Mysqli - Fatal编程技术网

PHP MYSQLI获取错误

PHP MYSQLI获取错误,php,mysql,postgresql,mysqli,Php,Mysql,Postgresql,Mysqli,在学校,我的老师告诉我很多关于使用POSTGRES数据库用PHP编码的事情。 但当我开始使用MYSQLI服务器时,我做不了很多事情 首先是功能。 我的老师让我做这种功能 $rows = getBuildings(); if (count($elenco) == 0) { print "<hr> No buildings here </hr>"; } else { ?> <thead>

在学校,我的老师告诉我很多关于使用POSTGRES数据库用PHP编码的事情。 但当我开始使用MYSQLI服务器时,我做不了很多事情

首先是功能。 我的老师让我做这种功能

$rows = getBuildings();
if (count($elenco) == 0) {
    print "<hr> No buildings here </hr>";
} else {
?>
                <thead>
                    <tr>
                        <th>address</th>
                        <th>town</th>
                        <th>Manager</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
    foreach ($elenco as $riga) {
?>
                        <tr>
                            <td> <?= $row->address ?></td>
                            <td> <?= $row->town ?></td>
                            <td> <?= $row->name ?> <?= $riga->surname ?> </td>
                        </tr>
    <?php
    }
?>
                </tbody>
            </table>
        </tbody>
    </table>
        <?php
}
?>
         </body>
         </html>
     <?php

function getBuldings() {
    global $db;
    $sql  = "select * from buildings, boss 
        where boss.codBo=buildings.codBu";
    $rows = array();
    if (!DB::isError($tab = $db->query($sql))) {
        while ($row = $tab->fetchRow(DB_FETCHMODE_OBJECT)) {
            $rows[] = $row;
        }
    } else {
        die("ErrorSQL:" . $tab->getMessage());
    }
    return $rows;
}
?>
$rows=getBuildings();
如果(计数($elenco)==0){
打印“
此处无建筑物””; }否则{ ?> 地址 镇 经理
但是为了让它在MYSQL中工作,我必须做很多更改。事实上!BD::isError中有一个错误。我必须从这个更改中更改connection.php

<?php
Session_Start();
require_once("DB.php");
$db = DB::connect("pgsql:etc....");
if (DB::isError($db)) {
    die("Error: " . $db->getMessage());
}
?>

对这个

<?php
$servername = "localhost";
$username   = "test";
$password   = "";
$dbname     = "test";
// Create connection
$db         = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}
$a = mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
echo "Connected successfully " . $a . " ";
?>

为了显示查询结果,我的代码变为:

<body>
        <?php
$rows = getBoss();
if (count($rows) == 0) {
    print "<h2>No Boss here</h2>";
} else {
    foreach ($rows as $row) {
        print "<p>" . $row['name'] . "</p>";
        print "<p>" . $row['surname'] . "</p>";
    }
}
?>
        <?php
function getBoss() {
    global $db;
    $sql  = "select *  
            from test_boss 
            order by name, surname";
    $rows = array();
    if ($tab = $db->query($sql)) {
        while ($row = $tab->fetch_array()) {
            $rows[] = $row;
        }
        return $elenco;
    } else {
        die("Errore sql: " . $tab->getMessage() . ":" . $sql);
    }
}
?>
    </body>

    </html>

在facts中工作得相当好,但我必须将FETCH_行更改为FETCH_数组。 我不能再使用从$rows值发布数据的方法了

<?= $row->name ?>

因为有个错误

注意:尝试获取非对象的属性

但我不得不使用

print "<p>".$row['name']."</p>";
打印“”$row['name']。“

”;

为了正确地使用FETCH_ROW方法,我可以做些什么?

您可以通过微调代码将数组类型强制转换为对象

如果您想要以
$row->name
的形式检索对象,那么您的代码将是

while ($row= $tab->fetch_array()) {
    $rows[] = (object)$row;
}


通过微调代码,可以将数组类型强制转换为对象

如果您想要以
$row->name
的形式检索对象,那么您的代码将是

while ($row= $tab->fetch_array()) {
    $rows[] = (object)$row;
}


考虑使用PDO,为什么不直接调用“MARCB”呢?它只会给我带来一个对象(一个列)结果表的。我通常使用多个对象进行询问。我正在编辑我的问题,添加另一个相互关联的问题。@JayBlanchard,不幸的是时间是我的敌人,我没有太多时间来更改我已经编写的所有代码。将来我将使用这种方式进行编码。@amicoff:fetchrow返回一个结果集的整行,而不仅仅是一个字段。考虑使用PDO,为什么不直接调用对象?MARCB,它只给对象(列)结果表的。我通常使用多个对象进行询问。我正在编辑我的问题,添加另一个相互关联的问题。@JayBlanchard,不幸的是时间是我的敌人,我没有太多时间来更改我已经编写的所有代码。将来我将使用这种方式进行编码。@amicoff:fetchrow返回一个结果集的整行,而不仅仅是一个字段。在这种情况下,如果我只想选择一个对象,我有这个方法。谢谢!在这种情况下,如果我只想选择一个对象,我有这个方法。谢谢!