• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

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.
0
PSTCAT
Asked:
PSTCAT
  • 3
  • 2
1 Solution
 
dannybamCommented:
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;

    }
0
 
PSTCATAuthor Commented:
Thanks,

but now I get

Fatal error: Using $this when not in object context


0
 
dannybamCommented:
i took that function from a class,  it might need to be adapted...   try this

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$user = "test";
 $result = mysql_query("SELECT City FROM users WHERE user = '" . $user . "'");
       if ($result->num_rows > 0){

            $row = $result->fetch_row();

            echo $row[0];

        }
0
 
PSTCATAuthor Commented:
Perfect. Thank you!
0
 
PSTCATAuthor Commented:
Cheers.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now