当我尝试在mysql数据库中执行我的SUCCESSFUL Select时,它会返回除第一个以外的所有字段。 (是的,它很安静)。
如果select只返回1个字段,则不会打印任何值。 如果select返回2个值,则只打印1个(第2个)。 等
我不明白为什么会这样!
$vector=array();
$sql="SELECT * FROM products";
$con=new DataBase();
$resultado=$con->selectQuery($sql,$vector); //you have this funtion below
if($resultado!=null){
if (!$resultado->fetchColumn()){
//no hay productos
$cadena="No products in our web";
}
else{
$cadena=$resultado; //IF ALL HAVE GONE CORRECT
}
}
else{
$cadena="Sorry, we are having some problems. Try again later.";
}
if (is_string($cadena))
{
//some code.
}
else
{
///////////////////////
// HERE I PROCEED TO TAKE THE DATA OF THE SELECT.
foreach ($cadena as $row)
{
$prods .="$row[price]";
}
}
//////////////////////////////////////////////
//selectQuery FUNCTION...
public function selectQuery($sql,$vector){
//select
if($this->connect()){//data to connect to the DB. It works well.
$sentencia = $this->db->prepare($sql);
$sentencia->execute($vector);
$this->disconnect();
}else{
$sentencia=null;
}
//$jjj=$sentencia->fetchAll(); //s'ha de fer si no a vegades dona error
//print_r($jjj) ; exit;
return $sentencia;
}
答案 0 :(得分:0)
检查文档fetchColumn() - http://php.net/manual/en/pdostatement.fetchcolumn.php
您只获取一列而不是所有列。使用fetchAll()函数选择所有列fetchAll() - http://php.net/manual/en/pdostatement.fetchall.php