1

I wanted the logic is if there is data in the database then query the data. Or else if there is no data then it will show an error message.

Here is my code:

$stmt = $db->prepare($query);
$stmt->execute(array('date' => $myFormat));
$data = $stmt->fetchAll();
if ( !$data ) {
    echo 'No data found in database!';
} else {
    return $data = $query;
}

Before this code, if the code that query from database:

//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";
}

I tried input a non data to execute the code but somehow it didn't show error and straight process to the scripting area.

The script below:

//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;
    
    //add all initial data into the matrix variable for easier access to them later
$rows[0]
    $rows = array(
        array($r1),
        array($r2),
        array($r3),
        array()
    );
    
}
//incoporate modulo value as an argument.
function incmod($a, $m)
{
    return ($a % $m) + 1;
}

//Population each row, with $populationCount number of elements, where each element is added with incmod(X, $mod)
function populateRow($rowId, $populationCount, $mod)
{
    //function to access the global variable.
    global $rows;
    $row = $rows[$rowId];
    while (sizeof($row) < $populationCount) {
        $rowInd = sizeof($row) - 1;
        $m      = incmod($row[$rowInd], $mod);
        array_push($row, $m);
    }
    
    //set the row back into the global variable.
    $rows[$rowId] = $row;
}
2
  • 1
    Finding no data is not an error. If you want to detect that then you need to check the number of rows returned by your query. Commented Dec 29, 2013 at 6:10
  • PDO doesn't throw an exception/error when the query returns an empty result set. You'll have to manually probe if ->fetch() returns anything. Commented Dec 29, 2013 at 6:11

1 Answer 1

2
$stmt = $db->prepare($query);
$stmt->execute(array('date' => $myFormat));
$data = $stmt->fetchAll();
if ( !$data ) {
    echo 'No data found in database!';
}
Sign up to request clarification or add additional context in comments.

7 Comments

i changed the echo 'No data found in database!'; to die('No data found in database!'); because if it echo, the script below still trigger despite no result was found. But now the problem is, if I add this line $data = $stmt->fetchAll(); if ( !$data ) { echo 'No data found in database!'; } For no data, it trigger the error and stop the script, but if there is data, it couldn't fetch the data. anyway I added my script code in first post
1. Don't make it die. 2. use else condition instead. 3. use foreach to loop over $data
added my current code, now I got two error, 1st: if I enter data where not store in database, it return the echo and looping error Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\jeff\result.php on line 65 2nd: if the data is found it return nothing and the script didn't trigger.
I don't know what are you doing, but you're doing it wrong. Why you are using so complex way for populating $rows? Can't you use $data already?
I'm sorry as I'm still new with php and pdo so what I think is making the code more complicated. What I need is, if no data from database echo error message else proceed with the data from database.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.