Solved

Displaying a single Database table row in php

Posted on 2011-09-04
5
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:dannybam
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;

    }
0
 

Author Comment

by:PSTCAT
ID: 36481180
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
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];

        }
0
 

Author Comment

by:PSTCAT
ID: 36481199
Perfect. Thank you!
0
 

Author Closing Comment

by:PSTCAT
ID: 36481200
Cheers.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

726 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