Jump to content

kicken

Gurus
  • Posts

    4,704
  • Joined

  • Last visited

  • Days Won

    179

Everything posted by kicken

  1. The line $conn->conn; by itself does absolutely nothing. Fatal error: Uncaught Error: Call to undefined method Database::prepare() in /opt/lampp/htdocs/qcic/assets/class/User.php:10 ------------------------------------------------------^^^^^^^^^^ Stack trace: #0 /opt/lampp/htdocs/qcic/assets/class/User.php(135): User->__construct(Object(Database), 'TechnoDiver') ------------------------------------------------------------------------^^^^^^^^^^^^^^^^ #1 /opt/lampp/htdocs/qcic/newsnet/assets/initializations.php(3): require('/opt/lampp/htdo...') #2 /opt/lampp/htdocs/qcic/newsnet/index.php(1): require('/opt/lampp/htdo...') #3 {main} thrown in /opt/lampp/htdocs/qcic/assets/class/User.php on line 10 Your error is because your passing $conn to your user class, which as has been said several times is an instance of your Database class. See the areas of the error I pointed to. Your database class doesn't have a prepare method, that's why you get an error. Your User class code is expecting to receive an instance of the PDO class. That exists as the conn property on your Database class, referenced by $conn->conn. $conn = new Database(); $user = new User($conn->conn, 'TechnoDiver');
  2. You're missing a $conn = new Database(); in that snippet of code. $conn doesn't exist at the point you're trying to access $conn->conn.
  3. When you have code like: $conn = new Database(); then $conn will be an instance of your Database class, with it's properties and methods. You can assign properties and call methods and such in the constructor, but you cannot return a value to be assigned to $conn. Inside your constructor and methods $this is a reference to the current instance of the class that is being operated on, so when you do: $this->conn = new PDO(...); You're assigning the instance of PDO that you create to the conn property of your Database class. To access if from code outside of the Database class (which requires the property to be public), you'd use $conn->conn. You might be getting confused by the use of the name conn everywhere, so maybe rename the property to something else like pdo. class Database { private $dbhost = "localhost"; private $dbuser = "root"; private $dbpassword = ""; private $dbname = "qcic"; public $pdo; public function __construct() { $dsn = "mysql:host=" . $this->dbhost . ";dbname=" . $this->dbname; $this->pdo = new PDO($dsn, $this->dbuser, $this->dbpassword); } } Don't bother catching the exception here, especially if you're just going to kill the script. The default action for an exception is for PHP to kill the script. If you want to handle it in some way, do it where you initiate your Database class. Now, in the rest of your code, you'd initialize your class with new Database() and access the pdo property to work with your PDO connection. $conn = new Database(); $stmt = $conn->pdo->prepare('SELECT * FROM ...'); ... For your other classes, you need to decide then if you want to pass your Database object ($conn) or your PDO connection ($conn->pdo) to their constructors and go from there.
  4. In fact, you cannot return something from a __construct function. The return value is automatically the newly created object. When you do $conn = new Database() then $conn is an instance of your Database class. You can't return some other value from the constructor like with other functions. Your PDO connection would then be $conn->conn. I would suggest for learning PDO that you drop the entire Database class and just deal with PDO directly. Once your more familiar with PDO and OOP in general you can look into creating a database class if you find a need for one. The PDO is pretty decent already, there's not a lot of benefit to creating your own class on top of it.
  5. When looking for variables to interpolate in a string, PHP identifies them by the $. Your self:: or Post:: is not being considered as part of the variable because they are before the $. So PHP is looking for a local variable $table which doesn't exist and your query would be "SELECT * FROM self:: WHERE ...". To use your static variable, you need to concatenate it, not rely on interpolation. $query = mysqli_query($this->conn, "SELECT * FROM ".self::$table." WHERE type='breaking' ORDER BY RAND()"); They aren't. Your class has only one variable named $table, but it's "class-level" rather than "instance-level". You use $this-> to access instance-level variable and a static reference (ie, self::) to access class-level variables. Using $this-> to access a static variable is improper.
  6. As I said, you're doing it wrong, from step 1. Downloading the library is the "old" way of managing your libraries, there's no need for downloading it manually if you're using composer. If you want to use composer then you need to start by creating your own composer.json file for your project. Use composer init to create your initial json file. mkdir chenxiu-doghouse cd chenxiu-doghouse composer init Then you require the library you want to use by it's identifier. It's identifier should exist in its composer.json file. The name may also be mentioned in the readme or found on packagist.org. composer require doghouse/doghouse-master-api If the library is not listed on packagist.org (ie, private/proprietary library) then you may need to add the repositories entry into your composer.json file before you can require it.
  7. The idea behind composer is that you don't download the package from github. You add the package name to your composer.json file as a requirement and let composer download it for you. The package name is often the same as the github repository, but not always. Check the package's composer.json file for the name you need. If the package doesn't install after adding the name to your composer.json file, then it may not listed on packagist.org. If that's the case, you need to also add the github repository to your composer.json file's list of repositories so it can find the package to download it. The whole idea behind using something like composer is that you should never need to manually download and install your libraries. You just add them to the list (and optionally the version requirement) and let composer handle everything for you.
  8. If it works for you, and you're the only one that needs it working then by all means go ahead with the plan. If you need a general solution that works for any potential user then you're better off finding another way, as there may be other buffers you have no control over. For example, the user's browser or they may be using a proxy server that buffers.
  9. That may or may not work as you expect. There are lots of potential buffers besides PHP's that can get in the way of trying to stream a response like that. If you want to provide incremental updates/progress notifications to a user you really need to design a system that will work with buffering enabled. Usually this takes the form of executing the task in the background on your server by some means and then outputting a page that uses Javascript to request periodic updates.
  10. The general name for this is a WYSIWYG editor. There are many implementations you can use, but two popular ones are CKEditor (used here I think) and TinyMCE (what I typically use)
  11. When storing data, you just need to ensure you don't fall victim to SQL Injection. Using bound parameters rather than inserting values directly into your SQL takes care of that, so there's no need for things like mysqli_real_escape_string. In addition, you shouldn't be modifying your input with things like nl2br to htmlspecialchars etc. Do that kind of manipulation at the time you output the data, not when it's received. If you modify it before you store it, you open yourself up to future problems such as if you decide tomorrow you want to output that data to a PDF file rather than HTML, or save it in a CSV file or include it in an image, etc. If you store the data with the HTML specific manipulations then you'll have to find some way to undo all those manipulations before using the data in another context.
  12. Time to re-evaluate then. Your function doesn't return anything. In that case, trying to capture a return value results in NULL which when used in an if statement equates to false. If you want to return a value from a function, you need to do so explicitly with the return keyword. So you need to either modify your function to return true or false appropriately or just remove your if statement where you call the function.
  13. Have you verified if $comment_obj->addComment is returning a true value so that your $message = true; line will be executed?
  14. You need to read the documentation for whatever tree view library you are using and figure it out. There is no standard tree view widget so there's no standard way to do what you want. If you can't figure it out from the documentation, then at the very least you need to specify which library you are using to get help with it.
  15. Ah, but have you tried array_fill_keys? See also, this post: Notice: Undefined index: driving me insane.
  16. You can have a single login system with roles if you want, just have a role that enables access to the admin section of the site and if either no user is logged in or the current user does not have that role then redirect them to the login page.
  17. It's possible he means you will need the CLI version of PHP in addition to whatever is used by your webserver (fpm / apache module). These are sometimes split into different packages by linux distributions so you can install only the specific one you need. The CLI version might be used for background tasks for example.
  18. So far, in my experimenting with it the character count ever exceeds 43 per line. In the instances where its different than Firefox it's only 42 characters in the line. It seems when character #44 is a space, chrome takes the last character from the previous line as well rather than just having the next line start with a space. Dunno if that's due to a bug or intended behavior but there's probably nothing you can do to fix it other than implementing your own wrapping mechanism. If it's important that every line have exactly 43 characters then might as well handle it yourself to be sure.
  19. I dunno, works for me. Added a min-width compared to above because otherwise I couldn't shrink the browser enough. Tried in Firefox and Chrome. The textarea shrinks until it hits the minimum width, then the text shrinks and starts showing the ellipsis.
  20. You can accomplish this by using flexbox. <div class="container"> <textarea></textarea> <div> The quick brown fox <br> jumped over the lazy dog </div> </div> CSS .container { display: flex; height: 100px; border: 1px solid red; } textarea { flex-grow: 1; flex-basis: 300px; } .container > div { flex-basis: 200px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
  21. The driver provides that function. If the driver is not installed and working, then that function does not exist and you'd get a undefined function error when trying to run code that uses it. Even if the driver is installed and working, it's possible your editor may not recognize it. I'm not familiar with Visual Studio Code but it may not know that you've installed that driver and as such doesn't recognize the function. That's why I asked if you were getting the error when actually running your code or just in your IDE. If the error is only in your IDE, but the code works when you actually run it then you'll have to figure out why the IDE is having an issue (or just learn to ignore it).
  22. Do you get the error actually running the script, or just as a warning in your IDE? Your php_sqlsrv_80_ts_x64.dll probably shouldn't be in your htdocs folder. I'm not familiar with XAMPP's layout but look for the folder where PHP is and an ext subfolder and put it there.
  23. It doesn't. If you don't provide a path when including a file, php will search for the file in the locations defined in the include_path. I'd guess that your public_html directory is part of this. var_dump(get_include_path());
  24. It's binary data. If the server says it's text/html, probably it's compressed text/html. The compression method would be indicated by a different header (Content-Encoding I think).
×
×
  • 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.