自己自学php+mysql遇到了大麻烦,希望各位帮帮我,我实在不知道我错在什么地方了
情况:
这是依据书本上的实例结合来的,我的mysql我设置了密码:root
下面这个图应该可以证明它有密码,不过也有可能是我理解错误(这个不是密码?)我在apache上的my homepage上运行这个文时,这个页面什么也不显示,我不知道是数据库连接错误(错误的话应该要显示错误信息的),还是我的语法出错了。这个程序非常简单,但是我自己找不出来问题的处处,问同学他们说是他们的代码是网上获得的,自己也不知道,所以,只能到这里求助了,希望大家帮帮我,我才刚刚开始学,有什么问题,希望大家严厉的帮我指出来,我一定认真改正。<!--文件名:prd.php
--> <html>
<head>
<title>显示数据库里的内容</title>
</head>

<body>
<?php
//连接数据库
$link_ID=mysql_connect("localhost","root","root")or die("错误。。 没有联接");
mysql_select_db("creategoods");
$str="select * from goods where name='$name';";
$list=mysql_query($str,$link_ID);
//输出数据库中每一件商品的购买信息,包括名称。价格等
mysql_close($link_ID);
$result=mysql_fetch_array($list);
};
?>
请输入要查询的货物名称:
<form action="prd.php" method="post">
    价格:<input type="text" name="name">
<input type="submit" value="查询">
</form>
<h2>结果:</h2>
<?php
echo "名称:";echo $result[2];
?>
</body>
</html>
这是我为了检测我的页面为什么不能正确显示而写的测试代码,下面这个代码就是我正式的代码了。正常的话他显示了我creategoods里的goods表里的所有信息,但是同样的,显示不出来,自己对照参考书也看不出个所以然来。所以只有来问了,希望大家帮我指正一下错误。
==================================================================================================================
<!--文件名称:product_table.php
--> <html>
<head>
<title>显示数据库里的内容</title>
</head>

<body>
<?php
//连接数据库
$link_ID=mysql_connect('localhost','root','root')or die("错误。。 没有联接");
mysql_select_db('creategoods');
$str="select * from goods where amount>0;";
$list=mysql_query($str,$link_ID);
//输出数据库中每一件商品的购买信息,包括名称。价格等
$sn_index=mysql_num_rows($list)
for($index=1;$index<=$sn_index;$index++)
{
$t[$index]=mysql_fetch_array($list);
};
?>
<h2 align="center">产品目录:</h2>
<table align="center" border="5">
<tr align="center">
<td width="120">
产品编号
</td>
<td width="300">
产品名称
</td >
<td width="120">
价格
</td>
<td width="120">
库存
</td>
</tr>
<form action="product_table.php" method="post">
<?php
for($index=1;$index<=$sn_index;$index++)
{?>
<tr align="center">
<td width="120"><?php echo $t[$index][id]?></td>
<td width="300"><?php echo $t[$index][name]?></td >
<td width="120"><?php echo $t[$index][price]?></td>
<td width="120"><?php echo $t[$index][amount]?></td>
</tr>
<?php };?>
</form>
</table>
<input type="submit" value="显示">

</body>
</html>==============================================================================================================
上面的所有的句子语法都是看了参考书后参照参考书写的,虽然自己知道自己要表达什么,可是程序出错了。

解决方案 »

  1.   

    啊,太抱歉了,刚刚才发现我还没申请相册,所以,我把图片传到我的QQ空间。如果遇到密码询问就找“我的密码”这项,输入:123,即可进去。
    图片在“PHP+MYSQL问题”这个相册里面
    麻烦大家了。
    图片有两张,图一是phpmyadmin进去后要求输入密码的图,图二是在mysql里的creategoods数据库和goods表的具体信息
      

  2.   

    把$link_ID=mysql_connect("localhost","root","root")or die("错误。。 没有联接"); 
    修改为:$link_ID=mysql_connect("localhost","root","")or die("错误。。 没有联接"); 试试。
    第mysql_connect的第3个参数为密码。
      

  3.   

    <html> 
    <head> 
    <title>显示数据库里的内容 </title> 
    </head> <body> 
    <?php 
    $name=$_POST['name'];
    //连接数据库 
    $link_ID=mysql_connect("localhost","root","root")or die("错误。。 没有联接"); 
    mysql_select_db("creategoods"); 
    $str="select * from goods where name='$name';"; 
    $list=mysql_query($str,$link_ID); 
    //输出数据库中每一件商品的购买信息,包括名称。价格等 
    mysql_close($link_ID); 
    $result=mysql_fetch_array($list);  
    ?> 
    请输入要查询的货物名称: 
    <form action="prd.php" method="post"> 
        价格: <input type="text" name="name"> 
    <input type="submit" value="查询"> 
    </form> 
    <h2>结果: </h2> 
    <?php 
    echo "名称:";echo $result[2]; 
    ?> 
    </body> 
    </html> 
    这样就行了,你的php部分没有接受$name
      

  4.   

    product_table.php二个文件里面$sn_index=mysql_num_rows($list)后面少了个分号;
      

  5.   

    真是谢谢你,我自己不仔细,现在可以显示了,不过我由一个疑问:mysql里面不支持中文吗?现在我数据库里面的产品名称都变成问号,之前就出现过这样的情况了,我在mysql里建了一个中文数据库,结果后来我看到他好像变成乱码了,数据也不能插入。
      

  6.   


    回答是:支持。
    估计是你的字符集设置的问题,阅读一下这篇文章。http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx
    MySQL 中文显示乱码
      

  7.   

    <html> 
    <head> 
    <title>显示数据库里的内容 </title> 
    </head> <body> 
    <?php 
    $name=$_POST['name']; 
    //连接数据库 
    $link_ID=mysql_connect("localhost","root","root")or die("错误。。 没有联接"); 
    mysql_select_db("creategoods"); 
    $str="select * from goods where name='$name';"; 
    $list=mysql_query($str,$link_ID); 
    //输出数据库中每一件商品的购买信息,包括名称。价格等 
    mysql_close($link_ID); 
    $result=mysql_fetch_array($list);  
    ?> 
    请输入要查询的货物名称: 
    <form action="prd.php" method="post"> 
        价格: <input type="text" name="name"> 
    <input type="submit" value="查询"> 
    </form> 
    <h2>结果: </h2> 
    <?php 
    echo "价格:";echo $result[2]; 
    ?> 
    </body> 
    </html> 很谢谢你,现在我的页面显示出来了但是我查询之后,页面上依然没有显示出我要的结果。很抱歉,这里的$result[2]应该是对应 价格。因为没有显示结果,所以,我对程序做了一点调整,$str="select * from goods where name like '$name';"; 用来代替上面的查询语句;echo $result[2]修改为echo $result[price],可是还是没有效果。你可以再给我提点建议吗?我愿意尝试一下。^^
      

  8.   

    你的sql语句你看下$str="select * from goods where name like '$name';"; 应该是$str="select * from goods where name like '$name'"; 你多了个分号
      

  9.   

    我也试过了,就可以查出来,建议你好好的看下sql,你可以这样先把你的sql语句echo出来,然后贴到mysql里面,看看有没有结果!你就在你的代码中加一句echo $str;查不出来的原因就是sql的问题!