当前位置:编程学习 > php >>

如何在PHP中调用MYSQL数据并将其显示在页面中?

数据库表为pre_forum_forum 其中字段fid,posts 如何让fid里指定的字段 显示其posts呢? 例如: fid posts 1 123 2 111 3 112 4 115 我只想在PHP页面里显示fid4里面的posts字段 也就是显示 115 请问达人们如何写这段代码?
追问:<?php
$db = mysql_connect("xxx.xxx.xxx.xxx","xxxxxxx","xxxxxx"); //连接mysql服务器
$db=mysql_select_db("xxxxx"); //选择数据库
mysql_query("set names utf8",$link); //设定编码方式
$sql = mysql_query('select * from pre_forum_forum where fid = '88'');
while ($row=ysql_fetch_array($sql)){
echo  $row['posts'];
} ?>

我是这样连接的 不着调哪里不对 网页都无法打开了<?php
$conn=mysql_connect('123.123.123.123','abc','xxxx');
$db=mysql_select_db('abc');
$sql="select * from pre_forum_forum where fid=88";
$result=mysql_fetch_array(mysql_query($sql,$conn)); 
$result['posts']?>

这样写对吗  因为我数据库里有两个库<?php $conn=mysql_connect('1.1.1.1','abc','123');
$db_selected = mysql_select_db("abc",$conn);
$sql="select fid from pre_forum_forum where fid=88";
$result=mysql_fetch_array(mysql_query($sql,$conn)); 
$result['posts'] ?>

这次出现的错误!
Notice: Undefined index: posts in D:\freehost\guochao\web\ershou\111.php on line 5可以把连接数据库的代码也写上吗?
答案:$conn=mysql_connect('localhost','username','userpassword');
$db_selected = mysql_select_db("你的数据库名",$conn);
$sql="select * from pre_forum_forum where fid=xxx";
$result=mysql_fetch_array(mysql_query($sql,$conn)); 
$result['posts']就是你摇的值了。
其他:此处省略连接数据库

$sql = mysql_query('select * from pre_forum_forum where fid = '4'');
while ($row=ysql_fetch_array($sql)){
echo  $row['posts'];
} 楼上两位已经说的很清楚 了.
1.$row["title"] 需要加引号, 在这里加单引号和双引号都行.
2.$row 里没数据.
--
能循环四次肯定是加载了4条数据,但是$row里有多少数据,数据时什么,可以通过下边的三句话来看..
 <?php
  $sql="select * from bbstb";
   $query=mysql_query($sql);
   while($row=mysql_fetch_array($query))
   {
  ?>
<!-- -----------  加上这3行可以看看$row 的数组布局是怎样的 ------------- -->
<pre>
 <?php print_r($row); ?>
</pre>
 

上一个:58同城 是用什么脚本编写的? php 还是 java 还是.NET ? 它后台用的什么数据库? mysql? 求高手解答
下一个:我的zend studio 8 PHP视图没有了?点击PHP视图出来的是白版,PHP视图界面出不来了,怎么办?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,