MikeMakos
MikeMakos

Reputation: 71

How to get the integer from sql database?

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?

Upvotes: 1

Views: 1058

Answers (2)

Oswald
Oswald

Reputation: 31647

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.

Upvotes: 1

Amir
Amir

Reputation: 4111

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

try:

$id = mysql_query("SELECT id FROM stations_id WHERE Name like '%Mike%'");

Upvotes: 1

Related Questions