使用mysql扩展从PHP中的数据库获取数据的代码有什么问题?

问题描述 投票:0回答:10

我想用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;

       ?>
php mysql
10个回答
5
投票

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;

0
投票

码:

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;

2
投票

试试这个

<?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>";      
} 

?>

没测试!! *更新!!


1
投票

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


1
投票
<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
        }
    ?>

0
投票

尝试

$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_*


0
投票

使用此代码

  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;

   ?>

0
投票
  1. 选择标识为mysql_select_db的数据库(“form1”,$ connect);
  2. 你收到语法错误了吗?如果请放一个; $ comment = $ rows ['Comment']旁边。
  3. 此处变量也应区分大小写

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'];

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


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;
}
   ?>
© www.soinside.com 2019 - 2024. All rights reserved.