2015-01-26 115 views
0

嗨,大家好,我有以下代码:PHP OOP取回阵列从数据库和MySQL

$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信息“和”毕业“从这样的”数据库“。

$this->Id = $this->db->getInfo(); 
$this->Grad = $this->db->getInfo(); 

那么这里我想呼应 “ID” 和 “梯度” 我得到他的注意,当遇到问题

注意:数组字符串转换在 C:\ XAMPP \ htdocs中\便便\类\ MVC \ userlogin.php第39行上阵列

代码的getInfo():

//$this->Resource is the mysql_query of the setQuery 
$this->Rows = array(); 

if ($this->Resource) { 

    while ($row = mysql_fetch_array($this->Resource)) { 

     $this->Rows[] = $row; 

    } 

} 
return $this->Rows; 

我想提一提的是我是PHP和OOP的初学者。 代码对所有功能使用http://tny.cz/4c5596fc

回答

0

所以它看起来像的getInfo()将获得第1行的第1个值,然后第二次调用时获取第一行的第二个值。如果它被称为第三次呢?

你可以做的一件事,因为你只取1行,只是在没有while循环的情况下,在你的getInfo()中返回mysql_fetch_assoc($ this-> Resource)。这将只是给你的第一行作为关联数组,你可以这样做:

$row = $this->db->getInfo(); 
$this->Id = $row['id']; 
$this->Grad = $row['grad']; 

我还要在这里提到的是mysql_功能已被取消,该代码是容易的MySQL注入攻击如果用户提供$电子邮件或$ Parola。看看PDO并准备好声明来防止这种情况发生。 如果你真的喜欢你的格式,你可以做这样在你的getInfo():

if (!$this->Row) { 
    if ($this->Resource) { 
     $this->Row = mysql_fetch_array($this->Resource); 
    } 
} 
return array_shift($this->Row); 
+0

我意识到SQL注入是可能的,我将在稍后检查PDO,因为我可以获得更多关于PHP的知识。 – camin10 2015-01-26 21:45:10

0

你的getInfo()函数返回数组,所以尽量只打印它像

echo '<pre>'; 
print_r($this->id); 
echo '</pre>'; 
+0

这会不会是我的解决办法,因为我希望我的“ID”和“梯度”存储在2回声后的会话。 – camin10 2015-01-26 20:02:31

+0

然后将其存储在$ _SESSION全局数组 – 2015-01-26 20:09:33

+0

我需要在其他页面上回显Id和Grad,以便它不起作用。 – camin10 2015-01-26 20:37:05