开发者

Beginner question on PHP, is there alternative ways to display data from mySQL than for/foreach/while loop?

I want to create a small application instead of do开发者_JS百科wnloading a source code and use it.

I will display data from the database in many different places in my page. For example, I will have to display the NAME somewhere, the ADDRESS somewhere else and at the end of the page the BIRTH.

The page will display the details of one person only, based on the ID given through a form. Below is the way I know on how to display the details of the person with ID 1.

$query="select * from student WHERE id='1'";

and in every place that will be a personal detail about the person with ID 1 this?

while($nt=mysql_fetch_array($rt)){
echo "$nt[name]<br>";
}

and in another place

while($nt=mysql_fetch_array($rt)){
echo "$nt[address]<br>";
}

and somewhere else

while($nt=mysql_fetch_array($rt)){
echo "$nt[birth]<br>";
}

My question is if there is another way of displaying info, instead of what I posted (including for/foreach loops and one big while loop), to display the details? Something faster maybe, with less repetitive code ?

Thank you.


Since you're only fetching one row, you do not need a loop at all.

Only once, issue your query and assign the first row to a variable.

$query = "SELECT * FROM student WHERE id = 1 LIMT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

Now at one part of your page, echo $row['name'];, somewhere else you can echo $row['address'], and at the end you can echo $row['birth'];.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜