-1

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
    $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;

       ?>
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
amit kumar
  • 35
  • 1
  • 1
  • 3
  • 1
    what is the error you getting? – amitchhajer Feb 12 '13 at 07:19
  • 1
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Feb 12 '13 at 07:19
  • what reason to declare those above variables for when you use `mysql_connect("localhost","root","");`? And tell us the result you get from this code – Tepken Vannkorn Feb 12 '13 at 07:21
  • 1
    Don't use the outdated MySQL library; use MySQLi or PDO - if you're just learning how to do this, learn to do it using the right library and with parameterised queries rather than a deprecated library – Mark Baker Feb 12 '13 at 07:25

10 Answers10

6

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;
Code Prank
  • 4,209
  • 5
  • 31
  • 47
1

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!!

afro360
  • 620
  • 3
  • 9
  • 22
1

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

Mohamed Shahid
  • 476
  • 5
  • 18
0
  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

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

$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.

Rahul Nanwani
  • 1,267
  • 1
  • 10
  • 21
0

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;
}
   ?>
php
  • 4,307
  • 1
  • 24
  • 13
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
        }
    ?>
-1

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.

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
-1

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;

   ?>
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • Why should anyone use this code? This extension was removed from PHP. At the time of writing this post it was already in the process of deprecation. – Dharman Jun 24 '19 at 21:23
-1

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;
Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48
test
  • 1