2014-03-29 38 views
0

我有一個信息表女巫只包含一個網頁的設置。如何獲得只有一列與MySQL的價值?

INFO 
id 
np (number of posts in one page) 
ngbp (number of guestbook posts in one page) 
title (title (header h1) of the website) 
footer (text of the footer(copyright)) 
lat (latitude for the google maps) 
lng (longitude for the google maps) 
mail (email of the contact page) 

什麼是獲得帖子數量的最佳方式?我從來沒有定義過從數據庫的唯一一行獲取值。

我一直用這個:

$sql="SELECT np FROM info WHERE id=".$id; 
$result=mysql_query($sql); 
while ($row = mysql_fetch_assoc($result)) 
$numberofposts=$row['np']; 

我應該用這個與WHERE id=1?或者什麼是獲得該專欄價值的最佳方式?

回答

1

時,只有一個返回的行你並不需要循環:

$sql="SELECT np FROM info WHERE id=".$id; 
$result=mysql_query($sql); 
$row = mysql_fetch_assoc($result); 
$numberofposts=$row['np']; 
0

您可以使用限制只得到一個結果,但。但是,您可能想重新考慮您的方法。將設置作爲鍵值對允許您添加其他配置設置,而無需更改數據庫。

0

我長時間使用ADODB for php library。它具有一些用於處理數據庫的好功能。對於你的問題,有getOne功能,我相信是你的需要的答案。