Displaying a single Database table row in php

Posted on 2011-09-04
Last Modified: 2012-05-12
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?
Question by:PSTCAT
  • 3
  • 2

Expert Comment

ID: 36481172
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;


Author Comment

ID: 36481180

but now I get

Fatal error: Using $this when not in object context


Accepted Solution

dannybam earned 500 total points
ID: 36481192
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];


Author Comment

ID: 36481199
Perfect. Thank you!

Author Closing Comment

ID: 36481200

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using winfows 7 and 10 download en install MYSQL server from a batch 13 124
Linux Scripting 3 122
Where do I learn ES6? 3 69
powershell md/mkdir/New-item   -Quiet 10 45
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to count occurrences of each item in an array.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question