-1

我想使用 php 从 mysql 表中获取数据。拜托,谁能告诉我这段代码有什么问题?从 mysql 数据库中获取数据的正确代码是什么:

 <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $connect=mysql_connect("localhost","root","");

    // connect to databsase 

    mysql_select_db("form1",);

        enter code here

    // query the database 

    $query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");

    // fetch the result / convert resulte in to array 

    while ($rows = mysql_fetch_array($query)):

       $rows = $rows['Name'];
       $address = $rows['Address'];
       $email = $rows['Email'];
       $subject = $rows['Subject'];
       $comment = $rows['Comment'];

       echo "$Name<br>$Address<br>$Email<br>$Subject<br>$Comment<br><br>";

       endwhile;

       ?>
4

10 回答 10

6

php 中的变量区分大小写。请将您的 while 循环替换为以下内容:

while ($rows = mysql_fetch_array($query)):

           $name = $rows['Name'];
           $address = $rows['Address'];
           $email = $rows['Email'];
           $subject = $rows['Subject'];
           $comment = $rows['Comment']

           echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";

           endwhile;
于 2013-02-12T07:21:47.627 回答
1

尝试这个

<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'database name';

// 2. Create a database connection
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connection) {
    die("Database connection failed: " . mysql_error());
}

// 3. Select a database to use 
$db_select = mysql_select_db($dbname,$connection);
if (!$db_select) {
    die("Database selection failed: " . mysql_error());
}

$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");

while ($rows = mysql_fetch_array($query)) { 
   $name = $rows['Name'];
   $address = $rows['Address'];
   $email = $rows['Email'];
   $subject = $rows['Subject'];
   $comment = $rows['Comment']

   echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";      
} 

?>

没测试!!*更新!!

于 2013-02-12T07:39:22.277 回答
1

将“WHILE”更改为“while”。因为 php 像 c/c++ 一样区分大小写。

于 2016-05-12T03:12:15.953 回答
0
  1. 选择标识符为 mysql_select_db("form1",$connect); 的数据库

  2. 您是否收到语法错误?如果请放一个; 在 $comment = $rows['Comment'] 旁边。

  3. 这里的变量也应该区分大小写

于 2013-02-12T07:26:12.437 回答
0

你的语法是错误的......正确的编码是:

 <?php
mysql_connect("localhost","root","");
mysql_select_db("form1");
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
while($rows = mysql_fetch_array($query))
{

   $rows = $rows['Name'];
   $address = $rows['Address'];
   $email = $rows['Email'];
   $subject = $rows['Subject'];
   $comment = $rows['Comment']
   echo $rows.'</br>'.$address.'</br>'.$email.'</br>'.$subject.'</br>'.$comment;
}
   ?>
于 2013-12-12T10:10:22.660 回答
0

如果这是您拥有的代码,那么您将收到一个错误,因为您在循环中重新分配 $row,因此您将永远无法迭代结果。代替

$rows = $rows['Name'];

$name = $rows['Name']'

所以你的代码看起来像

WHILE ($rows = mysql_fetch_array($query)):

   $name = $rows['Name'];
   $address = $rows['Address'];
   $email = $rows['Email'];
   $subject = $rows['Subject'];
   $comment = $rows['Comment'];

另外我假设表用户中的列名称是名称、地址、电子邮件等,而不是名称、地址、电子邮件。请记住,每个变量名/字段名都区分大小写。

于 2013-04-25T04:24:11.583 回答
0
<table border="1px">

    <tr>
        <th>Student Name</th>
        <th>Email</th>
        <th>password</th>
    </tr>

        <?php

            If(mysql_num_rows($result)>0)
            {
                while($rows=mysql_fetch_array($result))
                {  

        ?>
    <?php echo "<tr>";?>
                    <td><?php echo $rows['userName'];?> </td>
                    <td><?php echo $rows['email'];?></td>
                    <td><?php echo $rows['password'];?></td>

    <?php echo "</tr>";?>
        <?php
                }
            }

    ?>
</table>
    <?php
        }
    ?>
于 2016-10-21T05:11:56.893 回答
-1

尝试

$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ")or die(mysql_error());

并检查这是否会引发任何错误。

然后使用while($rows = mysql_fetch_assoc($query)):

最后显示为

echo $name . "<br/>" . $address . "<br/>" . $email . "<br/>" . $subject . "<br/>" . $comment . "<br/><br/>" . ;

不要使用mysql_*它已弃用。

于 2013-02-12T07:20:03.477 回答
-1

使用此代码

  while ($rows = mysql_fetch_array($query)):

   $name = $rows['Name'];
   $address = $rows['Address'];
   $email = $rows['Email'];
   $subject = $rows['Subject'];
   $comment = $rows['Comment'];

   echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";

   endwhile;

   ?>
于 2013-02-12T07:23:10.820 回答
-1

代码:

while ($rows = mysql_fetch_array($query)):
       $name = $rows['Name'];
       $address = $rows['Address'];
       $email = $rows['Email'];
       $subject = $rows['Subject'];
       $comment = $rows['Comment']
       echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;
于 2016-06-22T09:51:54.413 回答