致命错误:找不到类"TableRows" [英] Fatal error: Class 'TableRows' not found in

查看:61
本文介绍了致命错误:找不到类"TableRows"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这样的错误

致命错误:在第15行的/Applications/XAMPP/xamppfiles/htdocs/colorlib-search-23/test.php中找不到类'TableRows'

Fatal error: Class 'TableRows' not found in /Applications/XAMPP/xamppfiles/htdocs/colorlib-search-23/test.php on line 15

这是我的代码:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM users"); 
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;

检查数据库表的名称以及所有复制粘贴的其他代码的名称,但仍然无法正常工作

Checked names of the database table and all, copy-pasted other code but still it's not working

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM users"); 
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;

不应出现此错误.

推荐答案

Internet上有一篇非常有害的文章,告诉您需要任何TableRows类来使用PDO. 哪个是垃圾.

There is a certain extremely harmful article on the Internet telling that you need whatever TableRows class to work with PDO. Which is extremely rubbish.

实际上,您不需要这样的东西. 只需使用常规的foreach

In reality you don't need anything like this. Just use a regular foreach

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->query("SELECT * FROM users"); 
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

至此,您已经拥有一个常规的PHP数组,可以使用foreach将其迭代为任何其他数组,或者使用json_encode进行编码或以其他方式使用

At this point you have a conventional PHP array that can be iterated over as any other array using foreach or encoded using json_encode or used any other way

// iterate over rows
foreach($data as $row) { 
    // iterate over values in each row
    foreach($row as $v) { 
        echo $v, " ";
    }
    echo "<br>"\n;
}

这篇关于致命错误:找不到类"TableRows"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆