PHP OOP使用mysql从数据库获取数组

PHP OOP使用mysql从数据库获取数组,php,mysql,arrays,oop,Php,Mysql,Arrays,Oop,大家好,我有以下代码: $this->db->setQuery("SELECT id,grad FROM login WHERE Email='$Email' AND Parola='$Parola' LIMIT 1"); //setQuery Is setting the Query if($this->db->NumRows() > 0) { //Checking if Number of Rows is greater than 0 if(ses

大家好,我有以下代码:

$this->db->setQuery("SELECT id,grad FROM login WHERE Email='$Email' AND Parola='$Parola' LIMIT 1");
//setQuery Is setting the Query 
if($this->db->NumRows() > 0) {
//Checking if Number of Rows is greater than 0 

    if(session_id() == ''){
         session_start();
    }

    $this->Email = $Email;

    while($row = mysql_fetch_array($this->db->GetResource())){
        //Fetching from the database the "Id" And "Grad"
        $this->Id = $row[0];
        $this->Grad = $row[1];
    }
    echo $this->Id . "<br />";
    echo $this->Grad . "<br / >";
}
在这里,当我试图回应“Id”和“Grad”时,我被卡住了,我收到了他的通知

注意:中的数组到字符串转换 第39行数组上的C:\xampp\htdocs\poo\classes\MVC\userlogin.php

getInfo()的代码:

我想说的是,我是PHP和OOP的初学者。 使用的所有函数的代码

您的getInfo()函数返回数组,所以请尝试像

echo '<pre>';
print_r($this->id);
echo '</pre>';
echo';
打印($this->id);
回声';

因此看起来getInfo()将获得第一行的第一个值,然后在第二次调用时获得第一行的第二个值。如果叫第三次呢

您可以做的一件事,因为您只获取1行,就是在getInfo()中返回mysql\u fetch\u assoc($this->Resource),而不使用while循环。这将为您提供作为关联数组的第一行,您可以执行以下操作:

if (!$this->Row) {
    if ($this->Resource) {
        $this->Row = mysql_fetch_array($this->Resource);
    }
}
return array_shift($this->Row);
这里我还应该提到,mysql_uu函数已被弃用,如果用户提供$Email或$Parola,代码很容易受到mysql注入攻击。查看PDO并准备好声明以防止出现这种情况。 如果您确实喜欢现有格式,可以在getInfo()中这样做:


这将不是我的解决方案,因为我希望我的“Id”和“Grad”在echo之后存储在2个会话中。然后将其存储在$\u会话全局阵列上。我需要在其他页面上回显Id和Grad,这样它就不会工作。Php按每个请求运行。要跨多个页面请求持久化数据,您需要将数据存储在$_会话全局数组中,或者在任何请求时从数据库中获取数据。我知道SQL注入是可能的,稍后我将检查PDO,以了解更多PHP知识。
echo '<pre>';
print_r($this->id);
echo '</pre>';
$row = $this->db->getInfo();
$this->Id = $row['id'];
$this->Grad = $row['grad'];
if (!$this->Row) {
    if ($this->Resource) {
        $this->Row = mysql_fetch_array($this->Resource);
    }
}
return array_shift($this->Row);