PHP数组where子句
我的数组有多个类别,如生日、婚礼等。我想随机选择4个类别等于生日的项目。并呼应这些价值观PHP数组where子句,php,arrays,where-clause,Php,Arrays,Where Clause,我的数组有多个类别,如生日、婚礼等。我想随机选择4个类别等于生日的项目。并呼应这些价值观 class Cake { //properties:database connection and table name private $conn; private $table_name ='cakes'; //object properties: one for each field in our table public $id; public
class Cake {
//properties:database connection and table name
private $conn;
private $table_name ='cakes';
//object properties: one for each field in our table
public $id;
public $name;
public $category;
public $price;
public $description;
public $thumb;
public $large;
public function __construct($db){
//bring in the connection info, store it in this object's $conn property
$this->conn = $db;
}
/**
* the readAll method gets all existing row data
* @return all the retrieved row data
**/
//if no public or private auto public
function readAll(){
$cols = array('id', 'name', 'category', 'price');
//create a SQL query and run it, storing the data in a var $stmt
$stmt = $this->conn->prepare('SELECT id, name, category, description, price, thumb, large FROM '.$this->table_name.' ORDER BY RAND() LIMIT 4'); //this needs to be one or it will be multiple colls
//run the query, getting the data and stuffing into $stmt
$stmt->execute();
//send info back to where readAll() was called
return $stmt;
}//end readAll();
function readWed(){
$cols = array('id', 'name', 'category', 'price');
//create a SQL query and run it, storing the data in a var $stmt
$stmt = $this->conn->prepare('SELECT id, name, category, description, price, thumb, large FROM '.$this->table_name.' WHERE category = Birthday ORDER BY RAND() LIMIT 4');
$stmt->execute();
//send info back to where readWed() was called
return $stmt;
}//end readWed();
这里是我尝试输出值的地方,阅读所有作品,但不使用where子句。错误报告也不会输出
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include_once('config/spc_database.php');
include_once('object/cake.php');
$database = new Database();
$conn=$database->getConnection();
$cake = new Cake($conn);
$stmt = $cake->readWed();
?>
<div class="left-img">
<?php while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
<div class="element hvr-grow">
<?php echo "HELLOS"; ?>
<a href="description.php?detailsid=<?php echo $row['id'];?>">
<img class="imgurmob" src="img/
<?php echo $row['category']; ?>/<?php echo $row['thumb']; ?>" alt="img-sub-category">
</a>
</div>
<?php } ?>
在where子句查询中,您使用的是字符串值
生日
,因此您必须在引号中使用该值,如下所示:
"SELECT id, name, category, description, price, thumb, large FROM ".$this->table_name." WHERE category = 'Birthday' ORDER BY RAND() LIMIT 4"
代码示例:
$stmt = $this->conn->prepare("
SELECT id, name, category, description, price, thumb, large
FROM ".$this->table_name."
WHERE category = 'Birthday'
ORDER BY RAND()
LIMIT 4");
其中category=“生日”。。。。。引号???您的意思是“WHERE category=”。生日。“按兰德订购()限制4”?这给出了一个未定义的常量错误。字符串值是多少???或常量变量?将“.birth.”更改为“birth”(如果字段中的值有效),或将其更改为“.birth.”如果设置为我怀疑的常量。“”。此处的“生日”表示错误报告不应显示任何内容。
注意:使用未定义的常量生日-假定为“生日”
因此某些内容正在关闭错误。