Solved

Displaying a single Database table row in php

Posted on 2011-09-04
5
422 Views
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?
Thanks.
0
Comment
Question by:PSTCAT
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:dannybam
Comment Utility
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
 

Author Comment

by:PSTCAT
Comment Utility
Thanks,

but now I get

Fatal error: Using $this when not in object context


0
 
LVL 7

Accepted Solution

by:
dannybam earned 500 total points
Comment Utility
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
 

Author Comment

by:PSTCAT
Comment Utility
Perfect. Thank you!
0
 

Author Closing Comment

by:PSTCAT
Comment Utility
Cheers.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now