CodeIgniter跨控制器无法访问Session数据,查库返回Null求助
Hey there! Let's figure out why your Session-driven database fetch is returning null and showing up empty in your view. I’ll walk through the most common pitfalls and how to debug each part of your code step by step.
1. First: Confirm Your Session Data Is Actually Being Saved
Start with the basics—your login controller is supposed to store the user’s identifier (like id_perawat) in the Session after a successful login. But is it really doing that?
- Right after login (before redirecting to the profile page), add a debug line to print out the Session data:
Check if the key you need (e.g.,// In your login controller's success block var_dump($this->session->userdata()); // Adjust based on your framework's Session method exit;id_perawat) exists and has the correct value. If it’s missing or wrong, your Session setup is the root issue—double-check how you’re setting the Session (typos in key names are super common!).
2. Debug Your
c_perawat->DataProfil() Method Next, look at how you’re pulling the Session data and calling your model:
- Are you fetching the Session key correctly? For example, if you stored
id_perawat, make sure you’re using$id = $this->session->userdata('id_perawat');(no typos, case sensitivity matters!). - Add debug lines here too:
If// Inside DataProfil() $id = $this->session->userdata('id_perawat'); echo "Session ID pulled: " . $id; // Check if this is the correct value $profil_data = $this->model_perawat->getProfil($id); var_dump($profil_data); // See if the model is returning null here exit;$idis null, your Session isn’t being carried over (check if Session cookies are enabled, or if your framework requires Session initialization in the controller). If$profil_datais null here, the problem is in your model.
3. Audit Your Model’s
getProfil() Function Let’s dig into the database query logic:
- Check your SQL syntax: If you’re writing raw SQL, make sure table/column names are correct (e.g.,
perawatvsperawats,id_perawatvsid_perawats). A typo here will return zero results, hence null. - Verify parameter binding: If using a query builder (like CodeIgniter’s), make sure you’re passing the ID correctly:
If you’re using raw SQL, don’t forget to escape the ID to avoid SQL injection (and ensure the value is passed properly):// Example of correct binding public function getProfil($id) { return $this->db->where('id_perawat', $id)->get('perawat')->row(); }public function getProfil($id) { $query = $this->db->query("SELECT * FROM perawat WHERE id_perawat = ?", [$id]); return $query->row(); } - Check the database directly: Take the ID you pulled from the Session and run a manual query in your database tool (phpMyAdmin, pgAdmin, etc.):
If this returns no rows, the problem isn’t your code—it’s that the ID in the Session doesn’t match any record in yourSELECT * FROM perawat WHERE id_perawat = 'YOUR_SESSION_ID_VALUE';perawattable.
4. Make Sure Data Is Passed to the View Correctly
Even if your model returns data, you might be passing it to the view wrong:
- In
DataProfil(), confirm you’re assigning the model’s result to a variable in the data array:$data['profil'] = $this->model_perawat->getProfil($id); $this->load->view('profil_view', $data); - In your view, make sure you’re using the correct variable name (e.g.,
$profil) and checking if it’s not null before displaying:
Typos in variable names here will make it look like the data is null, even if it’s being passed correctly.<?php if (!empty($profil)): ?> <p>Nama: <?= $profil->nama_perawat ?></p> <!-- Other profile fields --> <?php else: ?> <p>No profile data found.</p> <?php endif; ?>
5. Bonus Debugging Tips
- Turn on your framework’s error logging: It might catch SQL errors or Session warnings you’re not seeing in the browser.
- Check if Session cookies are enabled in your browser: If cookies are blocked, your Session data won’t persist between requests.
内容的提问来源于stack exchange,提问作者kitcat
相关产品推荐
相关产品推荐

