Jump to content

Fatal error: Call to a member function fetchALL()


omardavinci

Recommended Posts

Hi i get the follow error.

Fatal error: Call to a member function fetchALL()

Code:

<?php
$db = new PDO("mysql:host=localhost;dbname=dwn;cahset=UTF","root","");
 
$sql = "SELECT * FROM 'users'";
$stmt = $db -> query($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
 
 
$title = "Home";
$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>
</table>
';
 
include 'Template.php';
?>
 

 

 

 

Link to comment
Share on other sites

The entire code is badly broken:

  • “cahset=UTF” probably means charset=utf8
  • You neither have exceptions enabled nor care about return values, which means you're flying blind.
  • Your query is broken. 'user' in single quotes is a string. You can't select from a string.
  • Don't use the root account for an application. This account is strictly for administration tasks.
  • Don't use wildcard selects (SELECT * ...). Always select the specific columns you actually need.
  • Put the database connection part into a separate function or script. Don't repeat it on top of every page.

A sanitized version:

<?php

const DB_HOST = 'localhost';
const DB_USER = '...';
const DB_PASSWORD = '...';
const DB_NAME = '...';
const DB_CHARSET = 'UTF8';



function database_connect()
{
    $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHARSET;

    return new PDO($dsn, DB_USER, DB_PASSWORD, [
        PDO::ATTR_EMULATE_PREPARES => false,                // disable emulated prepared statements for increased security
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,        // enable exceptions
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,   // fetch associative arrays by default
    ]);
}
<?php

require_once '/path/to/database.php';



$database_connection = database_connect();

$users = $database_connection->query('SELECT a_specific_column FROM users')->fetchAll();

$title = 'Home';

// ...
Edited by Jacques1
Link to comment
Share on other sites

sir i tried to call again fetchall and take me error:

<?php
 
require_once 'database.php';
 
 
 
$database_connection = database_connect();
 
$users = $database_connection->query('SELECT * FROM tabla')->fetchAll();
 
$rows = $users->fetchAll();
 
 
 
$title = 'Home';
$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>
<?php foreach($rows as $row): ?>
    <tr>
        <td>echo $row["id"]</td>
    </tr>
<?php endforeach;?>
</table>
';
include 'Template.php';
?>

 

 

Link to comment
Share on other sites

ok sorry

so when i do this:

$users = $database_connection->query('SELECT * FROM users')->fetchAll();

 

<?php foreach($users as $users): ?>
    <tr>
        <td><?php echo $user['id']?></td>
    </tr>
<?php endforeach;?>
 
i included this for showing the tables but not show me nothing i will check more tutorials thx
 
The code finally:
<?php
 
require_once 'database.php';
 
 
 
$database_connection = database_connect();
 
$users = $database_connection->query('SELECT * FROM tabla')->fetchAll();
 
 
 
 
$title = 'Home';
$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>
<?php foreach($users as $users): ?>
    <tr>
        <td><?php echo $user["id"]?></td>
    </tr>
<?php endforeach;?>
</table>
';
include 'Template.php';
?>

 

 

Any suggestion is good received, dissapointments too

Edited by omardavinci
Link to comment
Share on other sites

sorry for users but maybe i forgot to change it.

But anyways when i use this, i get the same:

<?php foreach($users as $user): ?>
        <td><?php echo $user["id"]?></td>
        <?php endforeach;?>
 

 

Yes you are right i took many mistakes with source code so i need to take more care the next time.

Link to comment
Share on other sites

when i do:

var_dump($users);

 

 

i get this message:

C:\wamp\www\www\tabla.php:10:

array (size=0)
empty

 

But it is strange because i created one table called tabla and it have content. And when i select one column like Nombre:

 

 

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Nombre' in 'field list'' in C:\wamp\www\www\tabla.php on line 9

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Nombre' in 'field list' in C:\wamp\www\www\tabla.php on line 9

 

 

And the table i created since database of users, since phpmyadmin, all is right. So maybe i did something wrong.

The table exists like you can see in this image:

https://i.imgsafe.org/5391e48b22.png

 

the code of tabla for you need to check:

<?php
 
require_once 'database.php';
 
 
 
$database_connection = database_connect();
 
$users = $database_connection->query('SELECT # FROM tabla')->fetchAll();
var_dump($users);
 
 
 
$title = 'Home';
$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>
       
    <tr>
        <?php foreach($users as $user): ?>
        <td><?php echo $user["id"]?></td>
        <?php endforeach;?>
    </tr>
 
 
</table>
';
include 'Template.php';
?>
        

 

 

Sorry again for all

Link to comment
Share on other sites

You're horribly confused. You don't seem to understand what a row and what a column is and how tables are structured.

 

What you've done is create an empty(!) table with four columns (not rows!): fija, fijo, f and a. So the table looks like this:

 fija | fijo | f | a
------+------+---+---
(no rows)

I really don't know what to tell you at this point other than: Put the script away and learn the basics.

Link to comment
Share on other sites

Sorry for the mistake i created the rows with insert into, i was confused sorry again. I confused rows with columns;

I fixed this mistake now:

 

My question is very short, some way to include rows since content php, I could only do outside of my template like this:

<?php
 
require_once 'database.php';
 
 
 
$database_connection = database_connect();
 
$users = $database_connection->query('SELECT id FROM coffee')->fetchAll();
var_dump($users);
 
 
 
$title = 'Home';
$content = '
<h4>Title 1</h4>
<table>
<th>
<td>&nbsp&nbsp&nbspName&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspTitle&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbspola&nbsp&nbsp&nbsp</td>
</th>
       
    <tr>
        <?php foreach($users as $user): ?>
        <td><?php echo $user["id"]?></td>
        <?php endforeach;?>
    </tr>
 
 
</table>
';
?>
        
<?php foreach($users as $user): ?>
        <td><?php echo $user['id']?></td>
        <?php endforeach;?>

 

 

Some way to include in content of php? Or some way that show me the table inside of my template thxx

Link to comment
Share on other sites

This looks like a block of code that the OP copied from somewhere. He has never programmed before. Certainly not in PHP. He is going about this thing completely backwards and thinks it's going to be a piece of cake.

 

OP - do some reading. Learn about programming and PHP. Try to understand the concepts before you start borrowing code from somewhere and then want us to solve it for you. Anyone showing your lack of knowledge definitely did NOT compile the complex OOP statement you are using. Try learning how to make a query, how to execute it, how to test the results of it and then how to retrieve and use it. You are showing no knowledge of any of this.

 

Go away until you have a working/reading knowledge of PHP and programming in general. This is not rocket science but it is still a science. Either that or go back to the video game console.

Link to comment
Share on other sites

Ok sorry i will learn more. Sorry again. But my question is very short sir: I did one content var in php because i want to do my webpage in php only and there i include my html content. So i couldn't include my querys since there? This is my question nothing more. Maybe i dont know nothing about php and mysql but this is not my question. Anyways thx for your help

Edited by omardavinci
Link to comment
Share on other sites

I won't argue with you. Anybody who knows anything about PHP and programming would not be demonstrating such lack of skills on this forum. Sorry - but you have a lot to learn about programming. Writing an HTML-only page is NOT in any way a form of programming. It is simply a paint-by-number project.

Link to comment
Share on other sites

My question is only that:

I cant include this code in $content var when i have content of my page:

This was only my question. But i will take your advice thx

Where is there a question in the above?.

 

Again - your lack of understanding of PHP and programming is showing. If you are saying (not asking) that you want those 3 lines of code to be stored in a variable then you are way off the mark. Those 3 lines of code simply iterate through an array and output (to the client) a single value from each retrieved array's contents.

 

If that is NOT what you are saying (not asking), then I have no idea what you are doing.

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.