/    Sign up×
Community /Pin to ProfileBookmark

i have this code how can i call $result in the while looping who’s in other function ?

[QUOTE]

<?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);
[COLOR=”red”] $result[/COLOR] = mysql_query(“SELECT * FROM $dbtbl”);

}

public function show_my_users(){
while ($row = mysql_fetch_array([COLOR=”Red”]HOW CAN I CALL $result HERE ?[/COLOR])) {
print $row[‘id’];

}
}

}

?>

[/QUOTE]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NoasITAug 10.2011 — Please use the boards [html [/code] [code [/code] and/or [php [/code] tags where applicable when posting code. Also:

[code=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);
$this->result = mysql_query("SELECT * FROM $dbtbl");



}

public function show_my_users(){
while ($row = mysql_fetch_array($this->result)) {
print $row['id'];

}
}

}

[/code]
Copy linkTweet thisAlerts:
@omardoauthorAug 10.2011 — i tried this but i have an error

Warning: mysql_fetch_array() expects parameter 1 to be resource
Copy linkTweet thisAlerts:
@NoasITAug 10.2011 — There's no checking for valid return:
[code=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);
$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'];

}
}

} [/code]


SHould atleast help identify why its not a valid resource ;-)
Copy linkTweet thisAlerts:
@omardoauthorAug 10.2011 — same errrorr ... when every thing is in one function i dont get this error ?
Copy linkTweet thisAlerts:
@NoasITAug 10.2011 — Well this line:
[code=php]
if( !$this->result > 0 ) die('Query returned no results.');
[/code]


shoulda been:
[code=php]
if( !mysql_num_rows($this->result) > 0 ) die('Query returned no results.');
[/code]


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:

[code=php]
<?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
[/code]
×

Success!

Help @omardo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...