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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14827380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:02:13  来源:igfitidea点击:

What is wrong with this code that uses the mysql extension to fetch data from a database in PHP?

phpmysql

提问by amit kumar

I want to fetch data from a mysql table using php. Please, can anyone tell me what is wrong with this code? What is the correct code to fetch data from a mysql database:

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

       ?>

回答by Code Prank

Variables in php are case sensitive. Please replace your while loop with following:

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;

回答by afro360

Try this

尝试这个

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

?>

Not tested!! *UPDATED!!

未测试!!*更新!!

回答by Shahidul Islam

Change the "WHILE" to "while". Because php is case sensitive like c/c++.

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

回答by Rahul Nanwani

If this is the code you have, then you will get an error because, you are reassigning $row while in the loop, so you would never be able to iterate over the results. Replace

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

$rows = $rows['Name'];

with

$name = $rows['Name']'

So your code would look like

所以你的代码看起来像

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

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

Also I am assuming that the column names in the table users are Name, Address, Email etc. and not name,address, email. Remember that every variable name/field nameh is case sensitive.

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

回答by php

Your syntax is wrong... The correct coding is:

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

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

回答by Marimuthu

  1. Select a database with identifier mysql_select_db("form1",$connect);

  2. Are you getting syntax error? If please put a ; next to $comment = $rows['Comment'].

  3. Also the variables should be case sensitive here

  1. 选择标识符为 mysql_select_db("form1",$connect); 的数据库;

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

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

回答by user6091934

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

回答by test

Code:

代码:

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;

回答by Yogesh Suthar

use this code

使用此代码

  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;

   ?>

回答by Dipesh Parmar

Try

尝试

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

and check if this throw any error.

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

Then use while($rows = mysql_fetch_assoc($query)):

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

And finally display it as

最后将其显示为

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

Do not user mysql_*as its deprecated.

不要使用mysql_*它已被弃用。