简体   繁体   English

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

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

I want to fetch data from a mysql table using php. 我想使用php从mysql表中获取数据。 Please, can anyone tell me what is wrong with this code? 拜托,有人可以告诉我这段代码有什么问题吗? What is the correct code to fetch data from a mysql database: 什么是从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;

       ?>

Variables in php are case sensitive. php中的变量区分大小写。 Please replace your while loop with following: 请用以下内容替换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;

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!! *更新!!

Change the "WHILE" to "while". 将“ WHILE”更改为“ while”。 Because php is case sensitive like c/c++. 因为php与c / c ++一样区分大小写。

  1. Select a database with identifier mysql_select_db("form1",$connect); 选择一个标识符为mysql_select_db(“ form1”,$ connect)的数据库;

  2. Are you getting syntax error? 您是否收到语法错误? If please put a ; 如果请放; next to $comment = $rows['Comment']. $ comment = $ rows ['Comment']旁边。

  3. Also the variables should be case sensitive here 另外,变量在这里应该区分大小写

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. 如果这是您拥有的代码,那么您将得到一个错误,因为在循环时您正在重新分配$ row,因此您将永远无法遍历结果。 Replace 更换

$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. 请记住,每个变量名称/字段名称h都区分大小写。

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

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_*因为它已被弃用。

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;

   ?>

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM