Load mysqli php data via ajax call

后端 未结 2 1844
野的像风
野的像风 2020-12-20 06:55

What I\'m trying to do is calling some database data via ajax and php. But the ajax call doesn\'t work, and I can\'t find out a solution on the web.

So here is my co

2条回答
  •  悲&欢浪女
    2020-12-20 07:32

    I just rewrote the php code using PDO, should be more safe now.

    db.php

    setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);  
    }
    
    catch(PDOException $e) {
    
        echo "Connection failed, an error occured! Please contact server administrator."; //user friendly message
        getErrorsLog($e->getMessage());
     }
    
     function closeDbConn () {
    
        $dbh = null;
    
     }
    
     function getErrorsLog($message) {
    
        $file = 'dberrors.log';
        $date = date("d/m : H:i :");
    
        // Open the file to get existing content
        $current = file_get_contents($file);
        // Append a new error message to the file
        $current .= $date.$message;
        $current .= "\r\n";
        // Write the contents back to the file
        file_put_contents($file, $current);
        exit();
    
     }
    
    ?>
    

    blogdata.php

    prepare("SELECT * FROM $tableName WHERE category =? ORDER BY created DESC"); 
     }
     else {  
    
        try {
    
        $stmt = $dbh->prepare("SELECT * FROM $tableName ORDER BY created DESC");
    
        }
    
        catch (PDOException $e) {
    
            getErrorsLog($e->getMessage());
    
        }
    
     }
    
    $stmt->bindValue(1, $view, PDO::PARAM_STR);
    
    $stmt->execute();
    
    $affected_rows = $stmt->rowCount(); //Rows count
    
     if ($affected_rows == 0) {
    
         echo "The data you looking for no longer exist, please contact the administrator.";
         exit();
     }
    
    foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
    
        $data[] = $row;
    
     }
    
    echo json_encode($data);
    
    closeDbConn();
    
    ?>
    

提交回复
热议问题