omardavinci Posted September 10, 2016 Share Posted September 10, 2016 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>   Name    </td> <td>   Title    </td> <td>   ola   </td> </th> </table> '; include 'Template.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/ Share on other sites More sharing options...
Jacques1 Posted September 10, 2016 Share Posted September 10, 2016 (edited) 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 September 10, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537317 Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 Thx a lot SIR you are very good person thx Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537319 Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 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>   Name    </td> <td>   Title    </td> <td>   ola   </td> </th> <?php foreach($rows as $row): ?> <tr> <td>echo $row["id"]</td> </tr> <?php endforeach;?> </table> '; include 'Template.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537320 Share on other sites More sharing options...
Jacques1 Posted September 10, 2016 Share Posted September 10, 2016 $users is the array of users. You cannot fetch from it again. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537321 Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 A sorry you are right i did since databases.php But why doesnt show me the elements of the table?? Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537322 Share on other sites More sharing options...
Jacques1 Posted September 10, 2016 Share Posted September 10, 2016 Read the code. I'm not your babysitter. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537324 Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 (edited) 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>   Name    </td> <td>   Title    </td> <td>   ola   </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 September 10, 2016 by omardavinci Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537325 Share on other sites More sharing options...
Jacques1 Posted September 11, 2016 Share Posted September 11, 2016 You cannot iterate over $users as $users. You need to concentrate and carefully read the code. Then read it again. And maybe a third time. If you make a new mistake on every line you write and then cannot fix it yourself, programming will be very difficult. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537331 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537333 Share on other sites More sharing options...
Jacques1 Posted September 11, 2016 Share Posted September 11, 2016 So what's the content of $users? var_dump($users); Are you selecting from the right table? Does the table even have rows? Do they have an id column? Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537334 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 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>   Name    </td> <td>   Title    </td> <td>   ola   </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 Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537336 Share on other sites More sharing options...
Jacques1 Posted September 11, 2016 Share Posted September 11, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537337 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 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: https://i.imgsafe.org/541361a450.png 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>   Name    </td> <td>   Title    </td> <td>   ola   </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 Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537338 Share on other sites More sharing options...
ginerjm Posted September 11, 2016 Share Posted September 11, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537340 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 (edited) 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 September 11, 2016 by omardavinci Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537342 Share on other sites More sharing options...
ginerjm Posted September 11, 2016 Share Posted September 11, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537343 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 (edited) Ok sir i will take your advice thx a lot Edited September 11, 2016 by omardavinci Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537344 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 My question is only that: I cant include this code in $content var when i have content of my page: foreach($users as $user): echo $user['id']; endforeach; This was only my question. But i will take your advice thx Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537346 Share on other sites More sharing options...
ginerjm Posted September 11, 2016 Share Posted September 11, 2016 (edited) 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 September 11, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537349 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 (edited) i know that this iterate one array and output to the client but it is not my question. Anyways i will take your advice. I am newbie yes but no one born knowing perfectly php and all. So i wait one new opportunity in my other questions thx. Edited September 11, 2016 by omardavinci Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537351 Share on other sites More sharing options...
Barand Posted September 11, 2016 Share Posted September 11, 2016 You need to familiarize your self with concepts of string variable code You cannot just put code in the middle of a string variable like that, it will just become part of the string. "View source" of your output page to see. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537353 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 thx: foreach($users as $user) { $content = $content . '<td>' . $user["id"] . '</td>'; } $content .= ' I will learn more the basic things. Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537358 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 (edited) I forgot to ask can someone recommend me some guide updated of PHP and mysql thanks for all Edited September 11, 2016 by omardavinci Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537391 Share on other sites More sharing options...
omardavinci Posted September 11, 2016 Author Share Posted September 11, 2016 the best guide http://php.net/manual/ Quote Link to comment https://forums.phpfreaks.com/topic/302138-fatal-error-call-to-a-member-function-fetchall/#findComment-1537393 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.