1

Why is my query not returning any results? I can run the query SELECT * FROMusersWHERE wid = 'worker_040' and it returns a result just fine. Am I missing something in this?

$wid = $_POST['username'];
            $con = mysql_connect("11.88.3.2","XXXX","XXXXX");
            if (!$con)
                {
                    die('Could not connect: ' . mysql_error());
                }

            mysql_select_db("chit", $con);

            echo "Form details<br />";

            $result = mysql_query("SELECT * FROM `users` WHERE wid = '$wid'");
            while($row = mysql_fetch_array($result)){
            echo $row['FirstName']. " - ". $row['LastName'];
            echo "<br />";
            }

        mysql_close($con);
4
  • It should be $_POST['username']. Also, are you sure $wid has a value when you call the query? What does the completed SQL actually look like right before you call mysql_fetch_array()? Commented Jul 17, 2011 at 16:15
  • @CharlesSprayberry Sorry I had that in my code, just didnt copy for some reason. Yes above this code just for testing I do echo $wid and it outputs something like worker_040 Commented Jul 17, 2011 at 16:17
  • I know this is nit-picking but I would take your query out of the $result variable, put it in it's own $query variable then echo out the whole, complete SQL statement to ensure the entire thing matches against what works. As long as the ID exists in your DB I don't see why this shouldn't be returning results. Commented Jul 17, 2011 at 16:23
  • After your query, print the output of mysql_error, and if there's no error in there, do a var_dump($wid); to check what is in there (do look at the source, not as an HTML page in a browser) (might be just a stray space that fouls things up for you). Commented Jul 17, 2011 at 16:30

2 Answers 2

1

You could take some time to debug.

For example, make sure your script is accessed via an HTTP POST request and 'username' is sent in the request ($_POST['request'] is set and has a valid value which could/would return results).

Get some info after executing your queries (you could use mysql_error()), get the number of rows returned etc. Output your sql query before sending it for execution.

Print the value of each row in your while loop.

These are some hints/tips you could use for finding out what might be going on. There are also other ways but you could give the above a try.

Sign up to request clarification or add additional context in comments.

Comments

0

You are calling your result rows as a numeric array but referencing them as an associative array - change your while loop to use mysql_fetch_assoc

Edit - this probably isn't it!

mysql_fetch_array returns the array as both an associative and numeric array by default, so shouldn't affect your code above.

1 Comment

thats what I thought at first too, but then I read up on the mysql_fetch_array and read the same thing. Thats why I am perplexed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.