darksoul90
darksoul90

Reputation: 145

How to get the value of only one column with mysql?

I have an info table witch contains the settings of the web page in only one row.

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)

What is the best way to get the number of posts? I have never nedded to get a value from only one row of a database.

I always used this:

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

Should I use this with WHERE id=1? Or what is the best way to get the value of that column?

Upvotes: 0

Views: 81

Answers (3)

Sh.Sab
Sh.Sab

Reputation: 1

I am using ADODB for php library for long time. It has some nice features for working with databases. For your question, there is getOne function which I beleive is an answer to your need.

Upvotes: 0

fejese
fejese

Reputation: 4628

You can use limit to only get one result but. However you might want to reconsider your approach. Stoeing the settings as key value pairs allows you to add other configuration settings without altering the database for example.

Upvotes: 0

John Conde
John Conde

Reputation: 219794

You don't need to loop when there is only one row returned:

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

Upvotes: 1

Related Questions