Link to home
Start Free TrialLog in
Avatar of PSTCAT
PSTCATFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Displaying a single Database table row in php

Ok, bascially, what I am trying to achieve is a piece of text that says

"Parties in CITY now"

The "CITY" is the place the user has entered in their account information and sent it to the database.

So I want to display that city row from the table on my website.

Here is my code for this,

   
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

 $result = mysql_query("SELECT * FROM user");
     
    while($row = mysql_fetch_assoc($result)){
    echo " ".$row['City'].";
    } 
     

Open in new window


I get an error with  
echo " ".$row['City'].

Open in new window


So I replaced the [ ] with ( )  it gets rid of the error
but doesn't display anything on my page.

Any thoughts?
Thanks.
Avatar of Member_2_6280017
Member_2_6280017
Flag of Puerto Rico image

public function get_city_by_user($user) {

        $user = $this->real_escape_string($user);

        $city = $this->query("SELECT City FROM users WHERE user = '" . $user . "'");



        if ($city->num_rows > 0){

            $row = $city->fetch_row();

            return $row[0];

        } else

            return null;

    }
Avatar of PSTCAT

ASKER

Thanks,

but now I get

Fatal error: Using $this when not in object context


ASKER CERTIFIED SOLUTION
Avatar of Member_2_6280017
Member_2_6280017
Flag of Puerto Rico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PSTCAT

ASKER

Perfect. Thank you!
Avatar of PSTCAT

ASKER

Cheers.