Welcome our webmaster and SEO forum
Please enjoy the forum, contribute what you can, and wind up the Moderators!
Results 1 to 4 of 4

Thread: OOPS - MySQL

  1. #1
    gilbertsavier is offline Member
    Join Date
    Jun 2009
    Posts
    31

    Default OOPS - MySQL

    Hello,
    Hey guys,
    I am looking for help with my OOPS code for connecting to MySQL, just for people that don't know what OOPS is Object Oriented Programming.

    I am trying to connect to my database (MSQL) using this technology, however all my coding just does not seem to be working

    The first code I tried was this one, however since i am on a shared server it seems that mysqli does not work??? DONT ask me why.
    and as i am trying to make something for other site owners to use i need it to work with out them having to contact there administrators asking them to allow mysqli
    $mysqli = new mysqli("localhost", "desvisa_v", "******", "desvisa_site");

    /* check connection */
    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
    }


    if ($result = $mysql->query("SELECT title FROM push_content LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
    }

    $mysqli->close();
    My second code, well i did not make the code, just copied and paste, BUT man it is way to confussing and i am just not understanding it.
    class MyDatabase
    {
    // The var that stores the last
    // used SQL statement
    var $SQLStatement = "";

    // The var that stores the error
    // (if any)
    var $Error = "";

    function MyDatabase()
    {
    // Config for the database
    // connection
    $this->DBUser = "desvisa_v";
    $this->DBPass = "*****";
    $this->DBName = "desvisa_site";
    $this->DBHost = "localhost";
    }

    function Connect()
    {
    //Connect to a mysql database
    $this->db = mysql_connect($this->DBHost,
    $this->DBUser, $this->DBPass) or
    die("MYSQL ERROR: ".mysql_error());
    // Select the database
    mysql_select_db($this->DBName,
    $this->db) or die("MYSQL ERROR:
    ".mysql_error());
    }

    // Disconnect from the MYSQL database
    function Disconnect()
    {
    mysql_close($this->db) or die("MYSQL
    ERROR: ".mysql_error());
    }
    }
    So if any one can come up with a OOPS way to connect to a mysql database, I would be forever in your debt.
    Thanks & regards
    Lokananth
    Live Chat Software By miOOt

  2. #2
    Robster is offline Member
    Join Date
    Jan 2008
    Location
    Bristol
    Posts
    43

    Default

    So what's the problem with it? are you gettin an error message, if so what is it?

    Maybe with a bit more info someone can help

  3. #3
    surreypcsupport's Avatar
    surreypcsupport is offline Senior Member
    Join Date
    Nov 2008
    Location
    surrey
    Posts
    565

    Default

    This will connect you to a database - replace [insert....] with whatever I say inside the square brackets. Delete the square brackets too.

    <?php
    function DBConnect()
    {
    $dbh=mysql_connect ("localhost", "[insert db user name]", "[insert db user password]") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("[insert database name]");
    }

    /*call the above function like this*/
    DBConnect();

    /*Example below will loop through a results set and output the values.*/

    function SelectExample()
    {
    $SQL = "[insert your sql query]";
    $result = mysql_query($SQL);

    while($row = mysql_fetch_array($result))
    {
    $var=$row['[insert column name to retrieve]'];
    echo $var;
    }

    }
    ?>

    You could create a paramater For SelectExample() that passes in a SELECT query if you wanted.

    btw this isn't OOP. Whilst PHP does enable object oriented programming we are not using those elements here.

  4. #4
    surreypcsupport's Avatar
    surreypcsupport is offline Senior Member
    Join Date
    Nov 2008
    Location
    surrey
    Posts
    565

    Default

    I think you would do well to work through these PHP tutorials. They helped me a lot when I first started:

    PHP Tutorial

    Also, this site will help you with the mysql related stuff:

    MySQL :: MySQL 5.0 Reference Manual :: 3 Tutorial

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124