PHP OOP 使用 mysql 从数据库中获取数组
PHP OOP Fetch array from Database with 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 / >";
}
虽然按照我的计划工作,但我对代码不满意,我想从 "db" 中获取 "Id" 和 "Grad" 的信息。
$this->Id = $this->db->getInfo();
$this->Grad = $this->db->getInfo();
好吧,我在尝试回显 "Id" 和 "Grad" 时卡住了,我收到了他的通知
Notice: Array to string conversion in
C:\xampp\htdocs\poo\classes\MVC\userlogin.php on line 39 Array
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
你的 getInfo() 函数 returns 数组,所以试着像
一样打印它
echo '<pre>';
print_r($this->id);
echo '</pre>';
所以看起来 getInfo() 会获取第一行的第一个值,然后在第二次调用时获取第一行的第二个值。如果调用第三次呢?
你可以做的一件事是,因为你只获取 1 行,所以在你的 getInfo() 中只 return mysql_fetch_assoc($this->Resource) 没有 while 循环。这只会给你第一行作为关联数组,你可以这样做:
$row = $this->db->getInfo();
$this->Id = $row['id'];
$this->Grad = $row['grad'];
我在这里还应该提到,mysql_ 函数已被弃用,如果用户提供 $Email 或 $Parola,代码很容易受到 mysql 注入攻击。查看 PDO 和准备好的语句以防止出现这种情况。
如果你真的喜欢你拥有的格式,你可以在你的 getInfo():
中这样做
if (!$this->Row) {
if ($this->Resource) {
$this->Row = mysql_fetch_array($this->Resource);
}
}
return array_shift($this->Row);
大家好,我有以下代码:
$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 / >";
}
虽然按照我的计划工作,但我对代码不满意,我想从 "db" 中获取 "Id" 和 "Grad" 的信息。
$this->Id = $this->db->getInfo();
$this->Grad = $this->db->getInfo();
好吧,我在尝试回显 "Id" 和 "Grad" 时卡住了,我收到了他的通知
Notice: Array to string conversion in C:\xampp\htdocs\poo\classes\MVC\userlogin.php on line 39 Array
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
你的 getInfo() 函数 returns 数组,所以试着像
一样打印它echo '<pre>';
print_r($this->id);
echo '</pre>';
所以看起来 getInfo() 会获取第一行的第一个值,然后在第二次调用时获取第一行的第二个值。如果调用第三次呢?
你可以做的一件事是,因为你只获取 1 行,所以在你的 getInfo() 中只 return mysql_fetch_assoc($this->Resource) 没有 while 循环。这只会给你第一行作为关联数组,你可以这样做:
$row = $this->db->getInfo();
$this->Id = $row['id'];
$this->Grad = $row['grad'];
我在这里还应该提到,mysql_ 函数已被弃用,如果用户提供 $Email 或 $Parola,代码很容易受到 mysql 注入攻击。查看 PDO 和准备好的语句以防止出现这种情况。 如果你真的喜欢你拥有的格式,你可以在你的 getInfo():
中这样做if (!$this->Row) {
if ($this->Resource) {
$this->Row = mysql_fetch_array($this->Resource);
}
}
return array_shift($this->Row);