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

OOPS - MySQL

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

  1. gilbertsavier

    gilbertsavier Affiliate affiliate

    31
    0
    0
    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.
     
  2. monetizer
  3. Robster78

    Robster78 Affiliate affiliate

    60
    0
    0
    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
     
  4. surreypcsupport

    surreypcsupport Affiliate affiliate

    593
    3
    0
    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.
     
  5. surreypcsupport

    surreypcsupport Affiliate affiliate

    593
    3
    0

Featured Resources (View All)

MI