Dismiss Notice
Welcome to Our Community
Wanting to join the rest of our members? Feel free to sign up today.


Discussion in 'Programming' started by gilbertsavier, Aug 4, 2009.

  1. gilbertsavier

    gilbertsavier New Member Webmaster

    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());

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

    /* free result set */

    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
    $this->db) or die("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.
  2. Robster

    Robster New Member Webmaster

    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. surreypcsupport

    surreypcsupport New Member Webmaster

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

    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*/

    /*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. surreypcsupport

    surreypcsupport New Member Webmaster


Featured Resources (View All)

Share This Page