[英]Get result from more than one colum MySQL PHP
I am trying to get the results from two MySQL columns with PHP, but I can only retrieve one. 我试图从PHP的两个MySQL列中获取结果,但是我只能检索其中一个。
Below is my code. 下面是我的代码。
$dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT user_id, Email, User_Password, twitter FROM user_detail WHERE Email = :Email );
$stmt->bindParam(':Email', $Email, PDO::PARAM_STR);
$stmt->execute();
$user_id = $stmt->fetchColumn();
$twitter = $stmt->fetchColumn();
PDOStatement::fetch I guess ? PDOStatement :: fetch我猜是吗?
$stmt->execute();
if($row = $stmt->fetch()) {
$user_id = $row['name'];
$twitter = $row['twitter'];
}
<?php
$stmt->execute();
$datas = $stmt->fetchAll();
foreach($datas as $d) {
echo $d['user_id']. '<br>';
echo $d['Email']. '<br>';
print_r($d);
}
It will help you to understand how this work I think, print_r will show you the array of the current item, each element is accessible by $d[key]; 我想它将帮助您了解此工作方式,print_r将向您显示当前项目的数组,每个元素都可以通过$ d [key]访问;
fetchColumn() - Returns a single column from the next row of a result set or FALSE if there are no more rows. fetchColumn() -从结果集的下一行返回单个列;如果没有更多行,则返回FALSE。
fetchAll() - Returns an array containing all of the result set rows. fetchAll() -返回包含所有结果集行的数组。
Since you already specified which columns to select in your SELECT statement, performing fetchAll() is perfect for what you're trying to achieve. 由于您已经在SELECT语句中指定了要选择的列,因此执行fetchAll()非常适合您要实现的目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.