Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Mysql Craziness

  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Mysql Craziness

    I am trying to program something to keep a log of my homework assignments and ive just begun but i am stumped by something that should be so easy, i get the weirdest output when i try a foreach loop.
    My Code

    index.php
    Code:
    //index.php
    include('inc/header.php');
    echo "This is a work in progress </br>";
    $sql = "SELECT * FROM `assi` LIMIT 0, 30 ";
    $results = $db->query($sql);
    echo $results . "<br>";
    $results = mysql_fetch_array($results);
    $assino = 1;
    foreach ($results as $res) {
    /* @var $res <type> */
        echo $assino . "    ::    " . $res['0'] . "</br>";
        $assino ++;
    }
    db.php (included with the index.)
    Code:
    <?
    class MySQLDatabase {
    	
    	private $connection;
    	public $last_query;
    	//private $magic_quotes_active;
    	//private $real_escape_string_exists;
    	
      function __construct() {
        $this->open_connection();
    		//$this->magic_quotes_active = get_magic_quotes_gpc();
    		//$this->real_escape_string_exists = function_exists( "mysql_real_escape_string" );
      }
    
    	public function open_connection() {
    		$this->connection = mysql_connect('***', '**', '*********');
    		if (!$this->connection) {
    			die("Database connection failed: " . mysql_error());
    		} else {
    			$db_select = mysql_select_db('hw', $this->connection);
    			if (!$db_select) {
    				die("Database selection failed: " . mysql_error());
    			}
    		}
    	}
    
    	public function close_connection() {
    		if(isset($this->connection)) {
    			mysql_close($this->connection);
    			unset($this->connection);
    		}
    	}
    
    	public function query($sql) {
    		$this->last_query = $sql;
    		$result = mysql_query($sql, $this->connection);
    		$this->confirm_query($result);
    		return $result;
    	}
    	
    //	public function escape_value( $value ) {
    //		if( $this->real_escape_string_exists ) { // PHP v4.3.0 or higher
    //			// undo any magic quote effects so mysql_real_escape_string can do the work
    //			if( $this->magic_quotes_active ) { $value = stripslashes( $value ); }
    //			$value = mysql_real_escape_string( $value );
    //		} else { // before PHP v4.3.0
    //			// if magic quotes aren't already on then add slashes manually
    //			if( !$this->magic_quotes_active ) { $value = addslashes( $value ); }
    //			// if magic quotes are active, then the slashes already exist
    //		}
    //		return $value;
    //	}
    	
    	// "database-neutral" methods
      public function fetch_array($result_set) {
        return mysql_fetch_array($result_set);
      }
      
      public function num_rows($result_set) {
       return mysql_num_rows($result_set);
      }
      
      public function insert_id() {
        // get the last id inserted over the current db connection
        return mysql_insert_id($this->connection);
      }
      
      public function affected_rows() {
        return mysql_affected_rows($this->connection);
      }
    
    	private function confirm_query($result) {
    		if (!$result) {
    	    $output = "Database query failed: " . mysql_error() . "<br /><br />";
    	    //$output .= "Last SQL query: " . $this->last_query;
    	    die( $output );
    		}
    	}
    	
    }
    
    $database = new MySQLDatabase();
    $db =& $database;
    
    ?>

    I end up with output of
    This is a work in progress
    Resource id #6
    1 :: 1
    2 :: 1
    3 :: M
    4 :: M
    5 :: E
    6 :: E
    7 :: F
    8 :: F
    9 :: E
    10 :: E
    11 ::
    12 ::
    13 :: 1
    14 :: 1

    Anybody have any clue? ive been stuck for days

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You're fetching is incorrect. MySQL results are not iterable and cannot be used in a foreach command.
    PHP Code:
    while ($res mysql_fetch_array($results))
    {
    /* @var $res <type> */
        
    echo $assino "    ::    " $res[0] . "</br>";
        
    $assino ++;

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    jman888 (02-09-2010)

  • #3
    New Coder
    Join Date
    Apr 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you so much, any clue how i use a foreach loop with sql or should i always do while ?, also what was up with the output it gave?
    Last edited by jman888; 02-09-2010 at 11:37 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Always use either a while or a for in conjunction with the mysql_num_rows. The only way to use a foreach is to use a while or for first and fetch it into an array. Writing a custom wrapper would solve this issue, but I don't think I'd bother with the work.
    The results you see are related to the connection results. What they are is undeterminable. They are bits and pieces of the actual resultset itself, but not the data it represents.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by jman888 View Post
    Thank you so much, any clue how i use a foreach loop with sql or should i always do while ?
    You need the while loop. Whichever of the functions you use, (fetch_row, fetch_assoc or fetch_array), you're only grabbing one row per pass from the table for your result set. You need the while loop to pull all of the rows.


  •  

    Posting Permissions

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