从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.
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:
Step 1: Add Dynamic Links to Your Employee List
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>"; } } ?>
- Avoid SQL Injection: Always use prepared statements (like the
?placeholder in the examples) instead of directly inserting$_GETvalues 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

