How to change Object Oriented Programming to normal php

Discussion in 'PHP' started by baris22, Feb 4, 2011.

  1. #1
    hello all,

    I am trying to adapt this code to mine. The problem is this is done by Object Oriented Programming. I have already got connection for mysql on another file.

    Can you please turn this into normal php. I will then just include my mysql connection.

    
    <?php
    	
    	$db = new mysqli('localhost', 'root' ,'emre', 'c');
    	if(!$db) {
    		// Show error if we cannot connect.
    		echo 'ERROR: Could not connect to the database.';
    	} else {
    		// Is there a posted query string?
    		if(isset($_POST['queryString'])) {
    			$queryString = $db->real_escape_string($_POST['queryString']);
    			if(strlen($queryString) >0) {
    				$query = $db->query("SELECT value FROM countries WHERE value LIKE '$queryString%' LIMIT 10");
    				if($query) {
    					// While there are results loop through them - fetching an Object (i like PHP5 btw!).
    					while ($result = $query ->fetch_object()) {
    						// Format the results, im using <li> for the list, you can change it.
    						echo '<li onClick="fill(\''.$result->value.'\');">'.$result->value.'</li>';
    	         		}
    				} else {
    					echo 'ERROR: There was a problem with the query.';
    				}
    			} else {
    				// Dont do anything.
    			} // There is a queryString.
    		} else {
    			echo 'There should be no direct access to this script!';
    		}
    	}
    ?>
    
    PHP:

     
    Last edited: Feb 4, 2011
    baris22, Feb 4, 2011 IP
  2. jkl6

    jkl6 Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Actually, that is "normal php". All you need to do is copy the code in the else { } statement and edit the database variable names to match your existing.
     
    jkl6, Feb 4, 2011 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3


    I want to take off
    
    $db = new mysqli('localhost', 'root' ,'emre', 'c'); 
    
    PHP:
    because i do not need it i am just going to use
    
    include ("connection.php");
    
    PHP:
     
    baris22, Feb 4, 2011 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Well thats not really OOP, I'm assuming (seeing as you refered to MySQL in your initial post) your meaning the use of MySQL Improved, theirfore to change it to something more globally recognisable:

    <?php
       
     include ("connection.php");
            // Is there a posted query string?
            if(isset($_POST['queryString'])) {
                $queryString = mysql_real_escape_string($_POST['queryString']);
                if(strlen($queryString) >0) {
                    $query = mysql_query("SELECT value FROM countries WHERE value LIKE '$queryString%' LIMIT 10");
                    if($query) {
                        // While there are results loop through them - fetching an Object (i like PHP5 btw!).
                        while ($result = mysql_fetch_assoc($query)) {
                            // Format the results, im using <li> for the list, you can change it.
                            echo '<li onClick="fill(\''.$result['value'].'\');">'.$result['value'].'</li>';
                        }
                    } else {
                        echo 'ERROR: There was a problem with the query.';
                    }
                } else {
                    // Dont do anything.
                } // There is a queryString.
            } else {
                echo 'There should be no direct access to this script!';
            }
    
    ?>
    PHP:
     
    danx10, Feb 4, 2011 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    thank you very much

    What is this called in php? this is not the way i am used to

    
    $db = new mysqli('localhost', 'root' ,'emre', 'c');
    $queryString = $db->real_escape_string($_POST['queryString']);
    $query = $db->query("SELECT value FROM countries WHERE value LIKE '$queryString%' LIMIT 10");
    
    
    PHP:
     
    baris22, Feb 4, 2011 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    MySQLi, basically MySQL however improved (and available in recent PHP versions), its does the same functionality as MySQL etc. but has some new features - most noticably offers you to use your preferred style of coding (whereas MySQL didn't - its prodecural throughout).

    MySQLi can be used in two styles, Object Oriented (OOP) and Procedural. What your code is using is Object Oriented, you can achieve the same result in both styles (but the styles are usually selected by preference).

    So, if I was to use MySQLi to do a query in OOP style would be like:

    $query = $db->query("SELECT value FROM countries WHERE value LIKE '$queryString%' LIMIT 10");
    PHP:
    If I was to use MySQLi to do a query in Procedural style would be like:

    $query = mysqli_query("SELECT value FROM countries WHERE value LIKE '$queryString%' LIMIT 10");
    PHP:

    If I was to use the regular MySQL (this is in Procedural, as thats the only style accepted) to do a query it would be like:

    $query = mysql_query("SELECT value FROM countries WHERE value LIKE '$queryString%' LIMIT 10");
    PHP:
     
    danx10, Feb 4, 2011 IP