Whats wrong with my query?

Posted on 2011-05-02
Last Modified: 2012-05-11
Hi, its telling me that my query is failing though I cannot understand why.  Can anyone see whats wrong with it?

(I put it in my phpmyadmin and it works fine!)

string(43) "SELECT * FROM users ORDER BY first_name ASC" Query failed:

function view_users() {
	$query = "SELECT * FROM users ";
	$query .= "ORDER BY first_name ASC";
	$result = mysql_query($query, $conn);
$output = <<<HERE
<table border="1px" cellspacing="0" cellpadding="5px">
	while($users = mysql_fetch_assoc($result)) {
		$output .= "<tr>";
		$output .= "<td>" . $users['username'] . "</td>";
		$output .= "<td>" . $users['first_name'] . " " . $users['first_name'] . "</td>";
		$output .= "<td>" . $users['address'] . "," . $users['city'] . "," . $users['state'] . "</td>";
		$output .= "<td>" . $users['phone'] . "</td>";
		$output .= "<td>" . $users['admin'] . "</td>";
		$output .= "</tr>";
	$output .= "</table>"; 
	return $output;

Open in new window

Question by:FairyBusiness
    LVL 82

    Accepted Solution


    function view_users() {
    global $conn;
          $query = "SELECT * FROM users ";
          $query .= "ORDER BY first_name ASC";
          $result = mysql_query($query, $conn);

    Author Closing Comment

    Thanks again!
    LVL 82

    Expert Comment

    and make sure you are connecting to the DB BEFORE you call the function:

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    728 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

    17 Experts available now in Live!

    Get 1:1 Help Now