0

This works:

$row = mysql_fetch_array($result);
$accountID = $row['accountID'];
queryMysql('INSERT INTO accounts (accountID, password) VALUES (' .
           "'$accountID'" . ', \'a\')');

But this doesn't:

$row = mysql_fetch_array($result);
queryMysql('INSERT INTO accounts (accountID, password) VALUES (' .
           $row['accountID'] . ', \'a\')');

Why?

2
  • queryMysql() is not a function and $accountID in undefined, so technically neither should work. Commented Jul 31, 2011 at 5:30
  • Ah sorry, I was using a function I made myself to make MySql queries cleaner. Commented Jul 31, 2011 at 22:57

2 Answers 2

2

Because you're missing another ' right before and after $row['accountId']

$row = mysql_fetch_array($result);
queryMysql('INSERT INTO accounts (accountID, password) VALUES (\'' .
           $row['accountID'] . '\', \'a\')');

If you are beginning PHP and have the required version of PHP (5.1.0) I strongly suggest you start using PDO

http://php.net/pdo

instead of the standard mysql_*

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

2 Comments

Thanks for the PDO pointer, I'll look more into that!. Adding a single quote before and after $row['accountID'] doesn't work though - isn't that a string literal? I originally did it the second way because of w3schools.com/PHP/php_mysql_select.asp..The accountID column is type int.
Yes, to php that is a string literal, but when you pass in to mysql, it has to be a string literal in a string literal. If you are confused by that, start using PDO :P.
0

First, you should always tell us why it doesn't work. Only saying "it doesn't work" is really begging for getting your post ignored.

Second, the cause of the error is most likely the lack of quote, but that would only be necessary if there's another underlying problem. The type of the column accountID seems to be varchar or text. An ID should be an integer.

You have to wrap your value with quotes if it's a string, you don't if it's an integer.

2 Comments

The accountID column is type int; why does it seem like it's varchar?
@Sarah, because it raises an error if you don't use quotes. Quotes aren't necessary for integers. Please tell us WHAT IS THE PROBLEM. Nobody wants to guess, saying "it doesn't work" is really unhelpful.

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.