开发者

Weird error in mysql query [closed]

Closed. This quest开发者_运维知识库ion needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

I want to obtain the last value that I put in.

I am trying to obtain the number on which my article is. So I use this query:

 $totalPages= mysql_query("SELECT page FROM `easy_db`.`article` ORDER BY article_id DESC LIMIT 1")  or die(mysql_error());

The value that I get is 6.

Why 6? I truncated the table..and tried again and I get 6 again.. It should be 1.

Another question, I want to enforce types in php..

I try to enforce an Int, but it doesnt seem to work:

 $page=(int) $totalPages/10;


mysql_query returns a resource handle, not the result of the query itself. See the PHP manual for some basic examples of how to query a database. For example:

$result= mysql_query("SELECT page FROM `easy_db`.`article` ORDER BY article_id DESC LIMIT 1")  or die(mysql_error());

if ($row = mysql_fetch_assoc($result)) {
   $totalPages=$row['page'];
}

With regard to your second question, pay attention to the operator precedence rules - the (int) operator has a higher precedence than division, so is carried out before the division. Thus, you can get a floating point result. Alternatively try one of these

$page = (int)($totalPages/10); #rounds down

$page=round($totalPages/10); #rounds up or down as appropriate

Also checkout floor() and ceil() as alternatives to round, as they are often useful when writing pagination calculations.


You can use mysql_insert_id() function in php to retrieve the id of the last inserted row.

mysql_insert_id


You should use mysql_insert_id(); immediately after your insert statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜