Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 days ago.
Improve this question
I have a database, and i'm using the MVC method in php (Model View Contol).
Basically i made the three, the view (while i fetch the data from the SQL query) and the controller/model, however it gives me NULL, and i don't know why.
I want to be able to print in a table the data i get from the select made in the model. However i can't, i have been doing var_dump several times but it doesn't print anything but in the view only NULL.
Here is the model code file:
`<?php
require_once "database.php";
class alumno_model extends database{
function desplegarAlumnosNormal(){
$query = "SELECT * FROM ALUMNOS";
//pdo query and print_r
$stmt = $this->db->prepare($query);
$stmt->execute();
var_dump($stmt);
echo $stmt;
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
?>`
Here is the controller file:
`<?php
class Alumno{
public function mostrarAlumnos(){
//aqui lo que hacemos es recoger los valores POST, creamos una instancia de insertarModel y luego
require_once "../models/alumno_model.php";
$alumno = new alumno_model();
$mostrarTodos= $alumno->desplegarAlumnosNormal();
require_once "../views/verTodosLosAlumnos.php";
}
}
?>`
And, finally, here is the view file, which doesn't print any data due to the NULL value:
`<?php
echo '<table class ="centrar" border = 1px solid black>';
echo '
<th>ID_ALUMNO</th>
<th> DNI</th>
<th>NOMBRE</th>
<th> APELLIDO </th>
<th> FOTO</th>
<th> CONTRASENYA </th>
';
foreach ($mostrarTodos as $row ) {
echo '
<tr>
<td>' . $row['ID_ALUMNO'].'</td>
<td>' . $row['DNI'] . '</td>
<td>' . $row['NOMBRE'] . '</td>
<td>' . $row['APELLIDO']. '</td>
<td>' . $row['FOTO']. '</td>
<td>' . $row['CONTRASENYA'] . '</td>
<td>editar</td>
<td>eliminar</td>
</tr>';
}
echo '</table>';
?>`
CLARIFICATION:
The part that gives me NULL is the variable $mostrarTodos, which then its results are saved in $row, but it doesn't print anything but NULL
Related
My problem is when I enter a specific country name, for example: France, it'll output every data from my database instead of just France. I don't know where I've gone wrong and its probably something very simple but I don't even know how to attempt to fix it so I've come here to get some help
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$country = $_POST['country'];
$_SESSION['country'] = $country;
$sqlQuery = "SELECT * FROM campsites WHERE country LIKE '%$country%'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
//var_dump($result);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<tr><td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td></td></tr>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
my Database class
class campDataSet
{
public $dbHandle, $dbInstance;
public function __construct()
{
$this->db = new campData();
$this->conn = $this->db->getCampData();
}
public function fetchAllCamps()
{
//$sqlQuery = "SELECT campsites.id_campsite, campsites.campsite_name, campsites.address, campsites.postcode, campsites.country, campsites.lattitude, campsites.longitude, campsites.email, campsites.phone_number
// FROM sgb220_clientserver.campsites";
$sqlQuery = "SELECT * FROM sgb220_clientserver.campsites";
if ($data = $this->conn->prepare($sqlQuery)) {
$data->execute();
$dataSet = [];
while ($row = $data->fetch()) {
$dataSet[] = new DBdata($row);
}
} else {
echo "<script> alert(\"Could not prepare SQL statement\") </script>";
}
return $dataSet;
}
Your fetchAllCamps() method doesn't accept any arguments.
Instead of defining the $sqlQuery inside fetchAllCamps, use a parameter:
public function fetchAllCamps($sqlQuery) // <- This
{
if ($data = $this->conn->prepare($sqlQuery)) {
$data->execute();
$dataSet = [];
...
A warning about SQL Injection
Because you are inserting $_POST data directly into your query, the user is able to manipulate the sql and thus can extract/manipulate data however he wants to. Read up in SQL Injection and how to prevent it to keep your database safe from attackers.
This might be a good starting point: https://stackoverflow.com/a/601524/2232127
Your issue is that you are running a query that just gets all of the camps instead of only the ones in a certain country. Your fetchAllCamps() function does not accept any parameters.
It would probably be best to move your query into the fetchAllCamps() function, or make another function entirely if you need a function to give you all the camps instead o just ones in a certain country. Instead of passing in the query, just pass the $country variable. Build your query inside the function and run it.
This way you are separating all of your SQL from where you are building your HTML. This is more in line with modern programming standards.
I am wondering if anyone could help? I am trying to send 2 variables which I have extracted from a database to another page when the user clicks on a link. At the moment I can only send one. I know what I am doing below is wrong.....basically I want to send both uninum and groupid over to the other page.
for ($i = 0; $i < $count; $i++){
$q = "SELECT participants.sname, participants.uninum, groups.groupid FROM participants INNER JOIN groups ON participants.uninum =
groups.uninum WHERE groups.groupid ='".$groups[$i]."'";
$result = mysqli_query ($dbcon, $q); // Run the query.
if ($result) { // If it ran, display the records.
// Table header.
echo '<table>
<tr><td><b>Edit</b></td>
<td><b>Surnname</b></td>
<td><b>University ID</b></td>
<td><b>Group</b></td>
</tr>';
// Fetch and display the records:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr>
<td>Edit</td>
<td>' . $row['sname'] . '</td>
<td>' . $row['uninum'] . '</td>
<td>' . $row['groupid'] . '</td>
</tr>';
}
echo '</table>'; // Close the table.
mysqli_free_result ($result); // Free up the resources.
echo "<br><br>";
} else { // If it did not run OK.
// Public message:
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
// Debugging message:
echo '<p>' . mysqli_error($dbcon) . '<br><br>Query: ' . $q . '</p>';
}
}
You have used ? instead of & in your code.
<td>Edit</td>
Should be:
<td>Edit</td>
You can try this :
Edit
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I cant fetch my db row using PDO. Currently I am using fetch(PDO::FETCH_ASSOC) but my result is showing blank.
This is my code:
<?php
ini_set('display_errors', 1);
//create_cat.php
include 'dbfunctions.php';
include 'forum_header.php';
$db = getConnection();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$id = $_GET['id'];
$sql = "SELECT
topicid,
topicsubject
FROM
topics
WHERE
topics.topicid = :id ";
$result = $db->prepare($sql);
$result->bindParam(":id", $id, PDO::PARAM_INT);
$result->execute();
$numrows = $result->fetchColumn();
if(!$result)
{
echo 'The topic could not be displayed, please try again later.';
}
else
{
while($topicrow = $result->fetchAll(PDO::FETCH_ASSOC))
{
echo "hello";
//display post data
echo '<table class="topic" border="1">';
echo '<tr>';
echo '<th colspan="2">' . $topicrow['topicsubject'] . '</th>';
echo '</tr>';
//fetch the posts from the database
$posts_sql = "SELECT
posts.topicid,
posts.postcontent,
posts.postdate,
posts.postby,
users.userID
FROM
posts
LEFT JOIN
users
ON
posts.postby = users.userID
WHERE
posts.topicid = :id ";
$posts_result = $db->prepare($posts_sql);
$posts_result->bindParam(":id", $id, PDO::PARAM_INT);
$posts_result->execute();
$posts_numrows = $posts_result->fetchColumn();
if(!$posts_result)
{
echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
}
else
{
while($posts_row = $posts_result->fetch(PDO::FETCH_ASSOC))
{
echo '<tr class="topic-post">
<td class="user-post">' . $posts_row['userID'] . '<br/>' . date('d-m-Y H:i', strtotime($posts_row['postdate'])) . '</td>
<td class="post-content">' . htmlentities(stripslashes($posts_row['postcontent'])) . '</td>
</tr>';
}
}
if(!$_SESSION['CurrentUser'])
{
echo '<tr><td colspan=2>You must be signed in to reply. You can also sign up for an account.';
}
else
{
//show reply box
echo '<tr><td colspan="2"><h2>Reply:</h2><br />
<form method="post" action="forum_reply.php?id=' . $row['topicid'] . '">
<textarea name="reply-content"></textarea><br /><br />
<input type="submit" value="Submit reply" />
</form></td></tr>';
}
//finish the table
echo '</table>';
}
}
include 'forum_footer.php';
?>
After the while loop I cannot fetch my row['topicsubject'] did I missed something .Can anyone help me with this. Thank you.
Wow so much debate for a small problem. Your first call to fetchColumn() fetches the data from the only row there is. So there is nothing left for fetchAll() to fetch.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I cant fetch my db row using PDO. Currently I am using fetch(PDO::FETCH_ASSOC) but my result is showing blank.
This is my code:
<?php
ini_set('display_errors', 1);
//create_cat.php
include 'dbfunctions.php';
include 'forum_header.php';
$db = getConnection();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$id = $_GET['id'];
$sql = "SELECT
topicid,
topicsubject
FROM
topics
WHERE
topics.topicid = :id ";
$result = $db->prepare($sql);
$result->bindParam(":id", $id, PDO::PARAM_INT);
$result->execute();
$numrows = $result->fetchColumn();
if(!$result)
{
echo 'The topic could not be displayed, please try again later.';
}
else
{
while($topicrow = $result->fetchAll(PDO::FETCH_ASSOC))
{
echo "hello";
//display post data
echo '<table class="topic" border="1">';
echo '<tr>';
echo '<th colspan="2">' . $topicrow['topicsubject'] . '</th>';
echo '</tr>';
//fetch the posts from the database
$posts_sql = "SELECT
posts.topicid,
posts.postcontent,
posts.postdate,
posts.postby,
users.userID
FROM
posts
LEFT JOIN
users
ON
posts.postby = users.userID
WHERE
posts.topicid = :id ";
$posts_result = $db->prepare($posts_sql);
$posts_result->bindParam(":id", $id, PDO::PARAM_INT);
$posts_result->execute();
$posts_numrows = $posts_result->fetchColumn();
if(!$posts_result)
{
echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
}
else
{
while($posts_row = $posts_result->fetch(PDO::FETCH_ASSOC))
{
echo '<tr class="topic-post">
<td class="user-post">' . $posts_row['userID'] . '<br/>' . date('d-m-Y H:i', strtotime($posts_row['postdate'])) . '</td>
<td class="post-content">' . htmlentities(stripslashes($posts_row['postcontent'])) . '</td>
</tr>';
}
}
if(!$_SESSION['CurrentUser'])
{
echo '<tr><td colspan=2>You must be signed in to reply. You can also sign up for an account.';
}
else
{
//show reply box
echo '<tr><td colspan="2"><h2>Reply:</h2><br />
<form method="post" action="forum_reply.php?id=' . $row['topicid'] . '">
<textarea name="reply-content"></textarea><br /><br />
<input type="submit" value="Submit reply" />
</form></td></tr>';
}
//finish the table
echo '</table>';
}
}
include 'forum_footer.php';
?>
After the while loop I cannot fetch my row['topicsubject'] did I missed something .Can anyone help me with this. Thank you.
Wow so much debate for a small problem. Your first call to fetchColumn() fetches the data from the only row there is. So there is nothing left for fetchAll() to fetch.
The error is Fatal error: Call to undefined method stdClass::retrieve_all_accounts() in /home/mjcrawle/public_html/cit0215/assignment5/onlinebanking/viewaccounts.php on line 72
I am not getting any html errors so I will leave that out.
My php code up to the error is:
<?php
require_once('footer_nav/navigation.inc.php');
require_once('../websiteconfig.inc.php');
require_once('../class/person_class.php');
require_once('../class/database.class.php');
/*Start Session*/
session_start();
$currentMember =unserialize($_session['currentMember']);
/*DataBase*/
$db = new Database;
$conn = $db->connection;
?>
<td width="16"> </td>
<td width="595">
</td>
</tr>
</div>
<h2>Accounts</h2>
</td>
<table id="accounts" summary="Bank Account Balance Information">
<thread>
<tr>
<th>Account Number</th>
<th>Account Balance</th>
</tr>
</thead>
<tbody>
<php?
/*Accounts*/
$currentMember->connection = $conn;
this is where I get the error line 72:
$accounts = $currentMember->retrieve_all_accounts();
/* Loop Though Accounts*/
while($account = mysqli_fetch_assoc($account)) {
/* Retrieve Account Balance*/
$bankaccount = new Bankaccount ($account['BankAccountID']);
$bankaccount->connection = $conn;
$balance = mysqli_fetch_assoc($bankaccount->retrieve_current_balance());
echo '<tr>' . "\n";
echo "\t" . '<td class="account_number">' . $account['BankAccountID'] . '</td>' . "\n";
echo "\t" . '<td class="account_balance">$' . number_format($balance['CurrentBalance'], 2) . '</td>' . "\n";
echo '</tr>' . "\n";
}
/*Closed DataBase*/
mysqli_close($db->connection);
?>
First, you are using this, in your first portion of code :
<php?
/*Accounts*/
$currentMember->connection = $conn;
If you really copy-pasted this, you have a problem here : this is not PHP code.
Instead, you should have :
<?php
/*Accounts*/
$currentMember->connection = $conn;
Note that a PHP opening tag is <?php and not <php?
Then, your $currentMember variable is set this way :
$currentMember =unserialize($_session['currentMember']);
It seems this makes $currentMember just on container, and not an instance of a class that would have a retrieve_all_accounts() method.
So, when trying to call that method :
$accounts = $currentMember->retrieve_all_accounts();
You get a Fatal error, because there is no such method in your $currentMember object.