TryHarder
TryHarder

Reputation: 750

php mysql single record show

Sorry about the stupid question however I can't see/ not good enough to solve this:

I have a MYSQL db and a php form everything works but I can't get out only one record - probably syntax error:

    $SQL = "SELECT * FROM massfelh WHERE user = $uname AND pass = md5($pword)"; - this is working
    $result = mysql_query($SQL); - this is too
    $catg = $result['categ']; - help
    $num_rows = mysql_num_rows($result); - this is ok too
    echo $catg; - for test and I can't print it on screen

DB structure: id, user, pass, categ

I only need the categ (either 1 or 0) but it just won't show on screen.

Any help would be appreciated - and please say I am stupid too:)

Update:

thank you for everyone of you!!!!!!! Helped me a lot. I have fount the best solution for my code however all the reply was correct. Thanks again hop this will help to some one how is just as beginner as me:) thank you again

Upvotes: 0

Views: 9716

Answers (8)

manurajhada
manurajhada

Reputation: 5380

Try This..

$SQL = "SELECT * FROM massfelh WHERE user = $uname AND pass = md5($pword)"; 
    $result = mysql_query($SQL);
    $result = mysql_fetch_assoc($result);
    $catg = $result['categ'];
    $num_rows = mysql_num_rows($result); 
    echo $catg;

Upvotes: 5

billyonecan
billyonecan

Reputation: 20260

mysql_query returns a result resource, you need to pass this to a fetch function in order to access the returned data. For example:

$query = mysql_query($SQL);
$result = mysql_fetch_assoc($query);
$catg = $result['categ'];

Use of mysql_ extensions is discouraged and the community has begun the deprecation process, you should have a read of the mysql_query documentation:

http://php.net/manual/en/function.mysql-query.php

Upvotes: 0

Tobas
Tobas

Reputation: 351

adding a LIMIT 1 to your select is good practice if you expect just one row.

If you only want to select categ then only select it SELECT categ FROM.... Then you could use $catg = mysql_result($result, 0); http://php.net/manual/de/function.mysql-result.php

Upvotes: 1

abhshkdz
abhshkdz

Reputation: 6365

You missed mysql_fetch_assoc there. It should be

$SQL = "SELECT * FROM massfelh WHERE user = $uname AND pass = md5($pword)";
$query = mysql_query($SQL);
$result = mysql_fetch_assoc($query);
$catg = $result['categ'];
$num_rows = mysql_num_rows($result);
echo $catg;

Ofcourse, the query can also be SELECT categ FROM massfelh WHERE user = $uname AND pass = md5($pword)

Upvotes: 0

OptimusCrime
OptimusCrime

Reputation: 14863

$SQL = "SELECT * FROM massfelh WHERE user = $uname AND pass = md5($pword)";
$result = mysql_query($SQL);
$num_rows = mysql_num_rows($result);
$catg   = mysql_fetch_assoc($result);

echo $num_rows.' rows matched';
echo '<pre>';
print_r($catg);
echo '</pre>';

Upvotes: 0

Ravi Kant Mishra
Ravi Kant Mishra

Reputation: 768

Currently you are not using any php function to get out put after executing query.

$result = mysql_query($SQL);
$res   = mysql_fetch_assoc($result);
echo $res['columnName'];

Upvotes: 0

dwjv
dwjv

Reputation: 1227

If you only need 'categ' just select that. Use var_dump on $result too.

SELECT categ FROM massfelh WHERE user = $uname AND pass = md5($pword)

Upvotes: 0

Sergii Stotskyi
Sergii Stotskyi

Reputation: 5390

$result = mysql_query($SQL); - this is too
$catg   = mysql_fetch_assoc($result);
print_r($catg);

mysql_query returns a resource. To fetch data you have to use some of next functions: - mysql_fetch_row (returns result as numerical array) - mysql_fetch_assoc (returns result as assoc array - keys of array are field's name in db table) - mysql_fetch_array (returns both numerical and assoc results if you dont pass second parameter)

But it would have been better if you had used Mysqli extension or PDO_Mysql

Upvotes: 0

Related Questions