如果从数据库中找到数据,我需要从数据库返回$ data,但不幸的是,我的代码无法返回数据。
我尝试输入不存在的数据,它设法返回错误消息,但如果数据存在,则页面不显示任何内容。
我可以在if语句中知道我哪里出错吗?
下面是我的代码,包含$ data返回后脚本将执行。
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$dob = implode('/', array($day,$month,$year));
if(isset($_POST['submit'])){
if(empty($_POST['day']) || empty($_POST['month']) || empty($_POST['year'])){
echo "You need to fill in each field.<br /><br /><a href=\"./\" title=\"Fill In Again\">Click here to <b>fill in each field</b> again</a>";
exit;
}
}
//Date that user keyin
$userDate = $dob;
$dateTime = DateTime::createFromFormat('d/m/Y', $userDate);
$myFormat = $dateTime->format('Y-m-d');
//Query string and put it in a variable.
if(isset($_POST['dob_chi'])){
$query = "SELECT * FROM $table_n WHERE dob_chi = :date";
} else {
$query = "SELECT * FROM $table_n WHERE dob_eng = :date";
}
$stmt = $db->prepare($query);
$stmt->execute(array('date' => $myFormat));
$data = $stmt->fetchAll();
if ( !$data ) {
echo 'No data found in database!';
exit;
} else {
return $data=$userDate;
}
//Now we create a while loop for every entry in our DB where the date is match.
while ($row = $stmt->fetchObject()) {
$r1 = $row->rowone;
$r2 = $row->rowtwo;
$r3 = $row->rowthree;
$englishdate = $row->dob_eng;
$chinesedate = $row->dob_chi;
$zodiac = $row->zodiac;
//add all initial data into a matrix variable for easier access to them later
//To access rowone use $rows[0][0], rowtwo $rows[1][0] ect.
//The matrix is an array which contains multiple array. eg. 2-dimensional arrays
//To get all the variables with $r1X simply retrieve the first array of the matrix eg $rows[0]
$rows = array(array($r1),array($r2),array($r3),array());
}
//Similarities between row1 and row2 made me incorporate modulo value as an argument.
function incmod($a, $m){
return ($a % $m) + 1;
}
感谢您的时间
答案 0 :(得分:0)
摆脱界限:
return $data=$userDate;
因为它会结束你的脚本并覆盖$data
变量。
变化:
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
然后更改行:
while ($row = $stmt->fetchObject()) {
为:
foreach ($data as $row) {