1

I have a database with 2 columns, one of type int and other of type varchar.

When i try to get tha name with id=1 for example, the query works fine:

$name = mysql_query("SELECT Name FROM stations_id WHERE id=1");
$a = mysql_fetch_array($name);
echo $a['Name'];

I try to do the opposite but i cant get the integer value. For example:

$id = mysql_query("SELECT id FROM stations_id WHERE Name='Mike'");
$a = mysql_fetch_array($id);
echo $a['id'];

I want to get the type as an integer so i can use it in a function. Can somebody help me plz?

10
  • 1
    @Stepo the OP has provided what he have tried. Commented Mar 17, 2013 at 14:41
  • Can you explain what goes wrong, because at first sight the query looks ok Commented Mar 17, 2013 at 14:42
  • PHP doesn't really care if it's in a string, if you use it as an integer (+ or / operators and such) it will work. Why do you care about it? (also why wont just cast it with (int)?) Commented Mar 17, 2013 at 14:42
  • The problem is when i do echo $a['id']; as in last command i dont get any output :S I dont know why. Commented Mar 17, 2013 at 14:44
  • If you don't get any output from echo $a['id'], then no record exists with a Name of 'Mike'. On a development server, you should also increase your error_reporting and turn on display_errors. That would have warned you about an "Undefined index 'id' ...". Commented Mar 17, 2013 at 14:48

2 Answers 2

1

Perhaps you do not have 'Mike' as name in your table

try:

$id = mysql_query("SELECT id FROM stations_id WHERE Name like '%Mike%'");
Sign up to request clarification or add additional context in comments.

Comments

1

If you absolutely need an integer, you have to cast $a['id'] to integer yourself. This is, however, rarely necessary. If you use $a['id'] in an integer context (e.g. in arithmetic operation) it will be cast to integer automatically.

1 Comment

The problem is not one of type. It's that no record exists with a Name of 'Mike'. See the comment I added to your question.

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.