Link to home
Start Free TrialLog in
Avatar of gjirvine3000
gjirvine3000

asked on

PHP script to print records from MySQL database

Hi PHP people,

I have written a simple script to retrieve and print all records from a MySQL database called phonebook, from a table called phonebook.
The script is below:
------------------------------------------
<html>
<title>Phonebook</title>
<body>
<b>Phonebook</b>
<br>

<?php

    $mysql_link = mysql_connect ("192.9.200.190", "", "");
    $testresult = mysql_select_db ("phonebook" ,$mysql_link);
    $query="select * from phonebook";
    $mysql_result = mysql_query ($query,$mysql_link);
        if ( mysql_num_rows ($mysql_result)>0){
            print "<table border=\"0\" width=\"100%\">";
            while ($row= mysql_fetch_row ($mysql_result)){
            print "<td width=\"20%\">$row[1]</td>";
            print "<td width=\"20%\">$row[2]</td>";
            print "<td width=\"20%\">$row[3]</td>";
            print "<td width=\"20%\">$row[4]</td>";
            print "<td width=\"20%\">$row[4]</td>";
                 print "</tr>";
            }
            print "</table>";
        }
?>

</div>

</body>
</html>
--------------------------------------------------
I don't understand why it doesn't work - all it does is print this to the browser:

--------------
Phonebook
0){ print ""; while ($row= mysql_fetch_row ($mysql_result)){ print " $row[1] "; print " $row[2] "; print " $row[3] "; print " $row[4] "; print " $row[4] "; print " 
"; } print " "; } ?>
-------------
Can anyone please help?

Thanks
Avatar of pulupul
pulupul

Could it be some kind of problem with not using parenthesis in the print calls and the quotation marks inside the string to be printed?. Try adding the parenthesis to the print calls.
Try this:

<html>
<title>Phonebook</title>
<body>
<b>Phonebook</b>
<br>

<?php

    $mysql_link = mysql_connect ("192.9.200.190", "", "");
    $testresult = mysql_select_db ("phonebook" ,$mysql_link) or die("DB Selection error: " . mysql_error());

    $query="select * from phonebook";

    $result = mysql_query ($query) or die("SQL Error : " . mysql_error());   // NOTE: changed $mysql_result to $result because mysql_result is a PHP fn. and you migth not be able to use it as a variable.

    if ( mysql_num_rows($result) > 0 )  {                            

            print "<table border=\"0\" width=\"100%\">";

            while ($row= mysql_fetch_row($result)) {
               print "<tr>";
               print "<td width=\"20%\">$row[1]</td>";
               print "<td width=\"20%\">$row[2]</td>";
               print "<td width=\"20%\">$row[3]</td>";
               print "<td width=\"20%\">$row[4]</td>";
               print "<td width=\"20%\">$row[4]</td>";
               print "</tr>";
            }
       
            print "</table>";
        }
?>

</div>

</body>
</html>
Avatar of gjirvine3000

ASKER

Thanks ldbkutty

This gives me the following output in the browser:

Phonebook
"; while ($row= mysql_fetch_row($result)) { print ""; print "$row[1]"; print "$row[2]"; print "$row[3]"; print "$row[4]"; print "$row[4]"; print ""; } print ""; } ?>
<TR> is missing..try this..

<html>
<title>Phonebook</title>
<body>
<b>Phonebook</b>
<br>

<?php

    $mysql_link = mysql_connect ("192.9.200.190", "", "");
    $testresult = mysql_select_db ("phonebook" ,$mysql_link);
    $query="select * from phonebook";
    $mysql_result = mysql_query ($query,$mysql_link);
        if ( mysql_num_rows ($mysql_result)>0){

            print "<table border=\"0\" width=\"100%\">";

            while ($row= mysql_fetch_row ($mysql_result)){
         
            print "<tr>";
            print "<td width=\"20%\">$row[1]</td>";
            print "<td width=\"20%\">$row[2]</td>";
            print "<td width=\"20%\">$row[3]</td>";
            print "<td width=\"20%\">$row[4]</td>";
            print "<td width=\"20%\">$row[4]</td>";
                 print "</tr>";
            }
            print "</table>";
        }
?>

</div>

</body>
</html>
Sorry Identical to ldbkutty ..
Or, Try with echo as follows:

  echo "<table border='0' width='100%'>";

            while ($row = mysql_fetch_row($result)) {

               echo "<tr>";
               echo "<td width='20%'>" . $row[1] . "</td>";
               echo "<td width='20%'>" . $row[2] . "</td>";
               echo "<td width='20%'>" . $row[3] . "</td>";
               echo "<td width='20%'>" . $row[4] . "</td>";
               echo "<td width='20%'>" . $row[4] . "</td>";
               echo "</tr>";
            }
       
            echo "</table>";
        }


Or even simpler, do like this:

<html>
<title>Phonebook</title>
<body>
<b>Phonebook</b>
<br>

<?php

    $mysql_link = mysql_connect ("192.9.200.190", "", "") or die("DB Connection error: " . mysql_error());
    $testresult = mysql_select_db ("phonebook" ,$mysql_link) or die("DB Selection error: " . mysql_error());

    $query="select * from phonebook";

    $result = mysql_query ($query) or die("SQL Error : " . mysql_error());
    $count = mysql_num_rows($result);

    if ( $count > 0 )  {                            
 
    ?>
            <table border="0" width="100%">

    <?
            while ($row= mysql_fetch_row($result)) {
    ?>
               <tr>
               <td width="20%"><?php echo $row[1]; ?> </td>
               <td width="20%"><?php echo $row[2]; ?> </td>
               <td width="20%"><?php echo $row[3]; ?> </td>
               <td width="20%"><?php echo $row[4]; ?> </td>
               <td width="20%"><?php echo $row[4]; ?> </td>
                </tr>
    <?
            }
    ?>
       
            </table>
    <?
        }

      else {
          echo "No rows found";
      }

    ?>

</body>
</html>
Thanks - this just gives me the word Phonebook as the output, with no table or phonebook records.

Does this perhaps mean I am not pointing PHP to the right area for my database?

My database is set up in mysql on my local machine, it is called phonebook, and has 6 columns, firstname, secondname, location, dial, mobile, landline.

I log in using mysql -u root -p with a blank password

thanks
Put this at the top and tell me if you get any errors/warnings.
<?php
 // Report all PHP errors (bitwise 63 may be used in PHP 3)
 error_reporting(E_ALL);
?>

Nope, sorry, nothing, still just the word "Phonebook"
have you tried any other simple PHP scripts? Like:

<?php
  echo "Hello <br> World";
?>

if the above works try to echo some content between MySQL stmts.
But, I dont think you have any error with DB connection because normally mysql_error() will show you the error if you have any problem with the DB.

More problems... it seems I cannot get any .php file to open in Internet Explorer, apart from the phonebook.php file that outputs the word "Phonebook".
Whatever I try, opening from IE, dragging into IE, telling the file to open in IE only, the .php files will only open in notepad.
aaaarrrgh!
ASKER CERTIFIED SOLUTION
Avatar of AlanJDM
AlanJDM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

PHP was running fine yesterday, as was Apache and mySQL, and I had run php tests with no problem.

Today I cannot even open some php files in IE - if I try to drag them into IE they open in notepad, if I tell them to always open in IE they don't open in anything.
"PHP was running fine yesterday"

Well, I say again.... it sounds like PHP isn't running. It may have been running fine yesterday, but if it isn't running now, that's a problem. I would try restarting the server/services as a start. Then, until you can make the script I posted above work, you know PHP isn't running.


Alan
Hello again,

I've had some success. There was indeed a problem with my PHP installation so I have installed again. I now get the following output when I run the phonebook.php file:
-------------
Phonebook
DB Connection error: Unknown MySQL Server Host 'c' (11001)
-------------

which is more useful. Do you know how I can change the code to point to the right place? I am running all of this on one machine, so should I just point the code to c:\mysql\bin rather than the IP address of the machine?

Thanks
Apologies - this is the problem:

When I use the IP address of my machine in the code, I get:

---------------------
Phonebook
DB Connection error: Host 'BWDESK0001.fujikura.co.uk' is not allowed to connect to this MySQL server
---------------------

but when I use c:\mysql\bin in the code I get:

---------------------
Phonebook
DB Connection error: Unknown MySQL Server Host 'c' (11001)
----------------------

Thanks
For 'localhost', just give

$mysql_link = mysql_connect ("localhost", "username", "password") or die("DB Connection Error : " . mysql_error());

You should *not* use your mysql location(C:\mysql\bin) in the host adress.
hi,

didnt noticed that you accpeted your answer. Have you solved the problem ?