opp help !
i have this code how can i call $result in the while looping who's in other function ?
<?php
class project0 {
public function mysql_connection() {
$host = 'localhost';
$user = 'root';
$password = '';
$db = 'om';
$dbtbl = 'omtbl';
$connetion = mysql_connect($host,$user,$password);
$select = mysql_select_db($db);
$result = mysql_query("SELECT * FROM $dbtbl");
}
public function show_my_users(){
while ($row = mysql_fetch_array(
HOW CAN I CALL $result HERE ? )) {
print $row['id'];
}
}
}
?>
Please use the boards [html [/html] [code [/code] and/or [php [/php] tags where applicable when posting code. Also:
PHP Code:
class project0 { public function mysql_connection () { $host = 'localhost' ; $user = 'root' ; $password = '' ; $db = 'om' ; $dbtbl = 'omtbl' ; $connetion = mysql_connect ( $host , $user , $password ); $select = mysql_select_db ( $db ); $this -> result = mysql_query ( "SELECT * FROM $dbtbl " ); } public function show_my_users (){ while ( $row = mysql_fetch_array ( $this -> result )) { print $row [ 'id' ]; } } }
i tried this but i have an error
Warning: mysql_fetch_array() expects parameter 1 to be resource
There's no checking for valid return:
PHP Code:
class project0 { public function mysql_connection () { $host = 'localhost' ; $user = 'root' ; $password = '' ; $db = 'om' ; $dbtbl = 'omtbl' ; $connetion = mysql_connect ( $host , $user , $password ); $select = mysql_select_db ( $db ); $this -> result = mysql_query ( "SELECT * FROM $dbtbl " ); if( ! $this -> result ) die( 'Query failed - (' . mysql_errno (). ') ' . mysql_error ()); if( ! $this -> result > 0 ) die( 'Query returned no results.' ); } public function show_my_users (){ while ( $row = mysql_fetch_array ( $this -> result )) { print $row [ 'id' ]; } } }
SHould atleast help identify why its not a valid resource ;-)
same errrorr ... when every thing is in one function i dont get this error ?
Well this line:
PHP Code:
if( ! $this -> result > 0 ) die( 'Query returned no results.' );
shoulda been:
PHP Code:
if( ! mysql_num_rows ( $this -> result ) > 0 ) die( 'Query returned no results.' );
However I think you should have a function to query separate from the connection, then call that function in the display user function. i would rearrange things a bit as such. Tell me if you still get problems:
PHP Code:
<?php class project { private $host = 'localhost' ; private $user = 'root' ; private $password = '' ; private $db = 'om' ; private $dbtbl = 'ombtbl' ; private $conn ; public function __construct () { $this -> conn = mysql_connect ( $this -> host , $this -> user , $this -> password ); mysql_select_db ( $this -> db ); } public function get_users () { $qry = "SELECT * FROM " . $this -> dbtbl ; if( !( $result = mysql_query ( $qry , $this -> conn )) ) die( 'Query Failed - (' . mysql_errno () . ') ' . mysql_error ()); if( ! mysql_num_rows ( $result ) > 0 ) return FALSE ; return $result ; } public function show_my_users (){ if( !( $result = $this -> get_users ()) ) trigger_error ( 'No results returned.' , E_USER_ERROR ); while ( $row = mysql_fetch_array ( $result )) { print $row [ 'id' ]; } } } // end class project
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks