0

I am getting the error Notice: Use of undefined constant num - assumed 'num' my code is following

$query = "SELECT COUNT(*) AS num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

how to fix this ?

i haven't got suited title so please don't min it an of possible change it to suited

3 Answers 3

4

@AkhileshBChandran gave one correct solution. In the future, if you're referring to the column, you need to put it in quotes.

$total_pages = $total_pages['num'];
Sign up to request clarification or add additional context in comments.

2 Comments

Is this going to work with mysql_fetch_array? If he had used mysql_fetch_assoc I know it would work.
@AkhileshBChandran mysql_fetch_array() returns both numeric and associative keys unless you specify MYSQL_NUM or MYSQL_ASSOC
1

$total_pages = $total_pages[0];

2 Comments

it working now but would you explain it so i can understand what it the problem?
Since you are fetching the result as an array, the first element would contain that 'num' field. So, we are accessing the first element using the index 0 :)
0
$total_pages = $total_pages['num'];

Comments

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.