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

前端 未结 10 696
别跟我提以往
别跟我提以往 2021-01-17 06:18

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:

相关标签:
10条回答
  • 2021-01-17 06:32

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

    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 讨论(0)
  • 2021-01-17 06:37
    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

    0 讨论(0)
  • 2021-01-17 06:39
    <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 讨论(0)
  • 2021-01-17 06:48

    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)):

    And finally display it as

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

    Do not user mysql_* as its deprecated.

    0 讨论(0)
  • 2021-01-17 06:51

    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;
    
    0 讨论(0)
  • 2021-01-17 06:53

    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;
    }
       ?>
    
    0 讨论(0)
提交回复
热议问题