views:

77

answers:

5
$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?

+1  A: 

Use mysql_fetch_assoc instead

ShimmerTroll
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 :/
Hristo
A: 

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

Babiker
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 :/
Hristo
A: 

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.

Col. Shrapnel
no errors and no id. var_dump results in NULL :(
Hristo
I wasn't connecting to the database... now I am and its giving an error
Hristo
@Hristo well that means either you can see no errors or you have no such record in your database. Ensure both things
Col. Shrapnel
will you be so kind to share this error's text?
Col. Shrapnel
Thanks. The only error information I have is from FireBug and it says `500 Internal Server Error`. I'll keep trying. Thanks
Hristo
@Hristo to see actual error message you have to refer toweb-server error_log. Can you find one?
Col. Shrapnel
/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: http://localhost/jQueryChat/chat.php"
Hristo
line 5 from db.php is "define(PASSWORD, 'password');"
Hristo
I'm getting a password denied for my user. That is my problem. "Access denied for user 'Hristo'@'localhost' (using password: NO)"
Hristo
@Hristo nice. have you solved this connection problem already? Though you should use my code anyway, cause I have many errors repaired.
Col. Shrapnel
I haven't figured it out yet... had a meeting with da boss. I'm back to it now. Can you explain your use of `mysql_real_escape_string($_POST['from']);`? Whats wrong with doing `$from = $_POST['from'];`?
Hristo
It turned out that I wasn't connecting to the database even though I thought I was... I deleted require_once("db.php") by accident with Ctr-Z. Works now!
Hristo
@Hristo nice. as for the code, I have answered recently: http://stackoverflow.com/questions/3047393/how-do-i-prevent-sql-injection-with-php-and-mysql/3047407#3047407
Col. Shrapnel
A: 

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)
Sarfraz
I am now connected to the DB and $from, $to, $message are all correct!
Hristo
@Hristo: Still no result? What errors if any you get?
Sarfraz
Access denied for user 'Hristo'@'localhost' (using password: NO)
Hristo
@Hristo: This means you are not connected to the database. Make sure that you are specifying the correct database info. Also see: http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/connect-to-mysql-database.aspx
Sarfraz
@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.
Hristo
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
Hristo
A: 

Hristo,

Could you possible check your error_log file and see the detailed information about the error ? If your host uses cPanel it usually is within your folders (or at the panel itself) if that is on your local computer you webserver should have it.

Do you have a connection to the database prior the query execution ? Considering you are searching for user_name, is $from a user_name and not an ID ? (just to make sure)

Add the follow to the top of your code error_reporting(E_ALL);

Please report back the information about your error_log.

Prix
Thanks for your response. It turned out that I wasn't connecting to the database even though I thought I was... I deleted `require_once("db.php")` by accident with Ctr-Z. Works now!
Hristo
glad you managed it :) keep us posted for any further questions
Prix