2
$from = $_POST['from'];
$to = $_POST['to'];
$message = $_POST['message'];

$query  = "SELECT * FROM Users WHERE `user_name` = '$from' LIMIT 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $fromID = $row['user_id'];
} 

I'm trying to have $formID be the user_id for a user in my database. Each row in the Users table is like:

user_id | user_name | user_type
   1    |  Hristo   |   Agent

So I want $from = 1 but the above code isn't working. Any ideas why?

6
  • Doesn't look to me like you have a live connection to the DB. Can you show us the code where you used mysql_connect? Commented Jun 15, 2010 at 17:45
  • You are right. I wasn't connecting to the DB. Now I am and its giving an error. FireBug says 500 Internal Server Error Commented Jun 15, 2010 at 17:47
  • Can you post the code you used? Commented Jun 15, 2010 at 17:49
  • 2
    What if my username is 1'; DROP TABLE Users; -- Commented Jun 15, 2010 at 17:52
  • From what I see from your comments, could it be that you never reach this part of your code at all? Your echo doesn't print anything, and you get 500 errors. Try to place something like die('Reached line: '.__LINE__); somewhere between your db-connect and this query, and see if that message appears. If it does, place it after the query and see if it still appears... Commented Jun 15, 2010 at 17:56

4 Answers 4

6

Try this:

$from = mysql_real_escape_string($_POST['from']);
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);

$query  = "SELECT * FROM Users WHERE user_name = '$from' LIMIT 1";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
    $fromID = $row['user_id'];
}

Also, make sure that:

  • You have connected to the database
  • You do get data from the post, try var_dump with your vars eg var_dump($from)
Sign up to request clarification or add additional context in comments.

5 Comments

I am now connected to the DB and $from, $to, $message are all correct!
Access denied for user 'Hristo'@'localhost' (using password: NO)
@Hristo: This means you are not connected to the database. Make sure that you are specifying the correct database info. Also see: php-mysql-tutorial.com/wikis/mysql-tutorials/…
@Sarfraz... I believe I am connected to the database because I can successfully display usernames in another section of the code. I'm still investigating it and it is probably a stupid mistake. But I'll go back and check the database connection code. Thanks.
You were right... I apologize. I THOUGHT I was connected to the database because I remember putting in the require_once("db.php") but I probably deleted it with Ctr-Z. Works now :) Thanks
1

Use mysql_fetch_assoc instead

1 Comment

doesn't seem to be working... I think I'm never getting inside the loop... I put an echo $fromID after the assignment inside the while loop and it didn't print anything :/
0

while($row =mysql_fetch_assoc($result)){ $fromID = $row['user_id']; }

1 Comment

doesn't seem to be working... I think I'm never getting inside the loop... I put an echo $fromID after the assignment inside the while loop and it didn't print anything :/
0

though it should. try this code

$from    = mysql_real_escape_string($_POST['from']);
$to      = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);

$query  = "SELECT * FROM Users WHERE `user_name` = '$from' LIMIT 1";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$fromID = $row['user_id'];
echo $fromID;

if it will throw no errors but still print no id, add this line

var_dump($row);

and post here it's output

not that you shouldn't use a user name but user id to address particular user.

11 Comments

I wasn't connecting to the database... now I am and its giving an error
@Hristo well that means either you can see no errors or you have no such record in your database. Ensure both things
Thanks. The only error information I have is from FireBug and it says 500 Internal Server Error. I'll keep trying. Thanks
@Hristo to see actual error message you have to refer toweb-server error_log. Can you find one?
/private/etc/apache2/error_log says "[Tue Jun 15 12:49:44 2010] [error] [client ::1] PHP Notice: Use of undefined constant PASSWORD - assumed 'PASSWORD' in /Library/WebServer/Documents/jQueryChat/db.php on line 5, referer: localhost/jQueryChat/chat.php"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.