You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从MS SQL Server向网站导入数据时fetch函数调用报错求助

Hey there! Let's work through the two issues you're hitting—first the Call to a member function fetch() error with your dbo.TimeLogs table, then passing values dynamically via href links to your view page.

1. Fixing the "Call to a member function fetch()" Error

This error almost always means the object you're calling fetch() on isn't a valid database result set (like a PDOStatement or mysqli result object). Here are the most common fixes:

Check Your SQL Query First

Double-check that your query for dbo.TimeLogs doesn't have typos—table names, column names, or syntax errors will break the query before it returns a result. For example:

// ❌ Might be a typo (TimeLog instead of TimeLogs)
$sql = "SELECT * FROM dbo.TimeLog";

// ✅ Correct query (with a parameter for dynamic filtering later)
$sql = "SELECT * FROM dbo.TimeLogs WHERE EmployeeID = ?";

Validate Query Execution

Always check if your query executed successfully before calling fetch(). Using PDO (a common choice for SQL Server connections), add error checking:

$stmt = $pdo->prepare($sql);
// Pass any parameters needed (we'll get to dynamic values next)
$stmt->execute([$targetEmployeeId]);

// Debug: Check if the statement is valid
if (!$stmt) {
    die(print_r($pdo->errorInfo(), true)); // Prints detailed SQL errors
}

// Now safely call fetch()
while ($logEntry = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process your TimeLogs data here
}

Confirm Database Connection & Permissions

Even though your Employees table works, make sure your database user has read access to TimeLogs, and that your connection isn't dropping mid-query.

To link from your employee list to viewlogs.php with a specific employee's ID (so you can fetch their TimeLogs), follow these steps:

On the page where you display Employees data, add hrefs with the employee ID as a query parameter:

// Assuming you're looping through fetched Employees data
while ($employee = $empStmt->fetch(PDO::FETCH_ASSOC)) {
    // Pass the EmployeeID as a query parameter in the link
    echo "<a href='viewlogs.php?emp_id=" . $employee['EmployeeID'] . "'>View " . $employee['Name'] . "'s Time Logs</a><br>";
}

Step 2: Receive & Use the Value in viewlogs.php

In viewlogs.php, grab the emp_id parameter, validate it, then use it to query TimeLogs:

<?php 
session_start(); 
if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit;
}

// Validate the incoming employee ID to avoid bad data
if (!isset($_GET['emp_id']) || !is_numeric($_GET['emp_id'])) {
    die("Invalid employee ID");
}
$employeeId = (int)$_GET['emp_id'];

// Connect to your SQL Server database (adjust credentials as needed)
$pdo = new PDO("sqlsrv:Server=YOUR_SERVER_NAME;Database=YOUR_DB_NAME", "DB_USER", "DB_PASS");

// Fetch only the logs for this specific employee
$sql = "SELECT * FROM dbo.TimeLogs WHERE EmployeeID = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$employeeId]);

// Handle empty results
if ($stmt->rowCount() === 0) {
    echo "No time logs found for this employee.";
} else {
    // Loop through and display the logs
    while ($log = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo "Log Date: " . $log['LogDate'] . " | Hours Worked: " . $log['Hours'] . "<br>";
    }
}
?>
Quick Extra Tips
  • Avoid SQL Injection: Always use prepared statements (like the ? placeholder in the examples) instead of directly inserting $_GET values into your SQL. This keeps your database safe.
  • Debugging Mode: Turn on PHP error reporting during development to see detailed errors:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

内容的提问来源于stack exchange,提问作者user8792746

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:17:12