Jump to content


  • Posts

  • Joined

  • Last visited

  • Days Won


Everything posted by kicken

  1. Your foreach loop will run to completion (ie, print every row) before your while loop condition gets re-evaluated. In pseudo-code, what you wrote is this: while $counter < 8 for each result $row print $row increment $counter end end Where as what you want to write, is while $counter < 8 fetch next result $row if $row exists print $row end increment $counter end There are multiple ways to write that. One way is to fetch a row at a time rather than everything. Another is to fetch it all like you are now, but break; after the 8th row.
  2. Could be a firewall problem (error 113 = no route to host). Talk to your host, or check your firewall if you're managing your own hosting.
  3. You need to use a delegated event which involves Adding the event to some parent element that will always exist and Adding a selector argument when calling the .on method. $('#tableDiv').on('click', '#edit', function(){ alert('Hi!'); });
  4. The complexity here come from trying the add/remove your one-time cron jobs. Cron isn't really designed for this type of usage so making it work this way would take a fair bit more effort on your part than doing the traditional way. Have you considered using at rather than cron? It seems to fit your scenario better.
  5. No, you're still executing your query as part of the loop condition: which means you run it again on every loop iteration. You need to run the query once before the loop, then call fetch_assoc() on each iteration of your result loop.
  6. You're not trying to use a return value from your query method in that code. Notice the line containing the call to query doesn't contain a $something = in front of it. You're DB::getInstance() method does return a value, which gets assigned to $user in that code. In the single-line version of the code, the value returned by DB::getInstance() is not captured, just used temporarily for the call to query. If you wanted a multi-line equivalent of your single line version it would be: $tmp = DB::GetInstance(); $user = $tmp->query('...'); if($user) { echo "success -> "; } else { echo "fail -> "; }
  7. Log your dataResult variable before you try and parse it. console.log(dataResult); Sounds like it's incomplete, you need to figure out why.
  8. Your query method does not return anything, so $user will be NULL.
  9. What is your code for DB::getInstance() and your query() methods?
  10. Sounds like you need to either split the functions into different pages or learn enough Javascript to do background requests and update the current page. The Javascript you need wouldn't be that hard probably. Either load up jQuery and use it's ajax functions or learn how to use fetch() and update the page with the DOM. There probably is, but as of yet we don't really have enough info about the overall process to be of much help.
  11. Create a script that checks the database for the content. If the content exists, display it in whatever manner you want. If the content doesn't exist, have the page refresh itself after 5 seconds.
  12. You can't get just decide to access an instance-level property as if it were a static property. The whole thing with static properties and methods is that there is no instance so instance-level stuff cannot be used. You either need to setup a static property for your connection or pass the connection into your static method as a parameter.
  13. Based on how you're trying to use it, you would just pass along the data to PDO's prepare method. public function prepare($sql){ return $this->conn->prepare($sql); } There's not much benefit to that over just dealing with PDO directly. This is why I suggested earlier that you might be better off for now just dealing with PDO and drop the whole idea of having a Database class. All your database class is managing to do so far is get in your way and confuse you.
  14. Make it a named function, then pass the name to your event handlers. function loadData() { $.ajax({ type: 'POST', url: 'returnPDO.php', dataType: "json", data: { id: "1", rows: "7" }, success: function (data) { ...CODE.... } }); } $("#searchTbl").keypress(loadData); $('#go').on('click', loadData);
  15. 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');
  16. 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.
  17. 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.
  18. 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.
  19. 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.
  20. 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.
  21. 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.
  22. 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.
  23. 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.
  • 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.