Viewed   79 times

the code is as follow:

Class userinfo {
    function fetchdatabyemail($email) {
        $result=mysql_query(" SELECT * FROM users WHERE email='$email'"); 
        while($row = mysql_fetch_array($result)) {
            $name = $row['name'];
            $num = $row['num'];
            $city = $row['city'];
        }
        $numrows= mysql_num_rows($result);    
    }
}

now to get the info I do this :

$info = new userinfo();
$info->fetchdatabyemail('[email protected]');  
echo $info->city; 

and it doesnt return the info. I think Im doing something wrong any ideas please

 Answers

1

do it

public $numrows;
public function fetchDataByEmail($email) {
        $result=mysql_query(" SELECT * FROM users WHERE email='$email'"); 
        while($row = mysql_fetch_assoc($result)) {
        $fetch[] = $row;
        }
        $this->numrows = mysql_num_rows($result);  
        return $fetch;  
}

then

$info = new userinfo();
$detail = $info->fetchDataByEmail('[email protected]');  
print_r($detail); // return all result array
$info->numrows; // will return number of rows.
Tuesday, November 15, 2022
5

You can just use a "new Mongo()" with the same connection string and it will use the same connection, but I suggest you wrap a singleton around your Mongo connection class to retrieve the same connection object. Probably something like:

<?php
class myprojMongoSingleton
{
    static $db = NULL;

    static function getMongoCon()
    {
        if (self::$db === null)
        {
            try {
                $m = new Mongo('mongodb://'.$MONGO['servers'][$i]['mongo_host'].':'.$MONGO['servers'][$i]['mongo_port']);

            } catch (MongoConnectionException $e) {
                die('Failed to connect to MongoDB '.$e->getMessage());
            }
            self::$db = $m;
        }

        return self::$db;
    }
}

And then call it anywhere else in your application with:

$m = myprojMongoSingleton::getMongoCon();
Sunday, August 7, 2022
 
akos_cz
 
2

The problem is either this:

    public function fetch($sql)
    {        
        $array = mysqli_fetch_array($this->query($sql));          
        return $array;
    }

or this:

 while($array = $connection->fetch($query))

Because you are using the result from the query to query again. Basically, you are doing:

$r = mysqli_query($this->dbc, $sql);
$array = mysqli_fetch_array(mysqli_query($this->dbc, $r));

And you are getting an error, because $r is not a query string. When it's converted to a string, it's a "1" (from your other comment).

Try changing the function to (changed name of variable so you can see the difference):

    public function fetch($result)
    {        
        return mysqli_fetch_array($result);
    }

or just call the function directly.

Wednesday, November 30, 2022
 
reggie
 
3

Well, you could use die(). But that makes all errors fatal. Meaning that you cannot try to recover from the error at all. In some cases that's fine to do.

But in most cases, you may want the ability to "clean up" after the error, or to try another method. This is where exceptions come in handy... They let you chose where and if you want to handle the error. They let you try to gracefully recover from the errors.

For example, let's say you have a method which downloads a file from a remote server: downloadFromRemoteServer($address);

If you use die(), if the download fails, the script terminates. End of story.

But if you use exceptions, you could try another server or even try a different method (HTTP vs FTP, etc):

try {
    $file = downloadFromRemoteServer('http://example.com/foo');
} catch (DownloadFailedException $e) {
    try {
        $file = downloadFromRemoteServer('http://secondtry.example.com/foo');
    } catch (DownloadFailedException $e2) {
        die('Could not download file');
    }
}
return $file;

But remember that Exceptions are useful only for exceptional circumstances. They are not meant to be used for any possible error. For example, if a user doesn't verify their email address correctly, that's not exceptional. But if you can't connect to the database server, or have a conflict in the DB, that would be an exception circumstance...

Tuesday, November 22, 2022
 
5

$this to the object in whose context the method was called. So: $this is $a->getName() is $a. $this in $fooInstance->getName() would be $fooInstance. In the case that $this is set (in an object $a's method call) and we call a static method, $this remains assigned to $a.

Seems like quite a lot of confusion could come out of using this feature. :)

Wednesday, November 30, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :