Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    Hi I'm trying to open and select from a database in an OO style... Am I going about it the right way? IE using mysqli, and why am I getting an error?... is there a good example of this kind of coding out there?

    Thanks -Hagen

    Fatal error: Call to a member function query() on a non-object in /home/junior/public_html/class-lib.php on line 44

    class Page {
    public $mysql;

    function __construct(){

    $config_obj = new JConfig();
    $this->mysql = new mysqli($config_obj->host, $config_obj->user, $config_obj->password, $config_obj->db);
    if ($this->mysql->connect_error) die('Connect Error ('.$this->mysql->connect_errno.')'.$this->mysql->connect_error);

    $this->mysqli->query("SELECT id from bookings where id = $id");

    }
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  2.  

  3. #2
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    got it...! typo should be mysql...

    still am i going about this in the right way overall?

    thanks -Hagen
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    There's a change happening now ... from "mysql" to "mysqli", which is faster and more secure. You'll have to find out if your webhost has the ability to use "mysqli" and that you should be using that.


  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    The mysql library is deprecated. So mysqli is correct.

    What you're failing to do is set your resultset to a variable... (and also you should use prepared statements to prevent sql injection)

    Code:
    $id = (int) $id;
    $stmt = $this->mysqli->prepare("SELECT id FROM bookings WHERE id = (?)");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
      print_r($row);
    }

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    smoseley,
    What happens to all of my existing websites that use WordPress or other mysql applications? Do I have to go back and edit them all ... changing from mysql to mysqli, or is that something that happens seamlessly on the shared webhost's server?


  7. #6
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    mlseim, if you upgrade to PHP 5.4, you'll get a DEPRECATED notice if you're using the mysql extension. This won't show unless you enable E_STRICT error reporting. However, it's always a good idea to have a plan to eliminate deprecated code from your codebase. In the case of Wordpress, that means upgrading the software to a version that's done so.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:44 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com