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
gjirvine3000Asked:
Who is Participating?
 
AlanJDMConnect With a Mentor Commented:
This sounds to me like PHP isn't even running on your server. Have you ensured it is there, running, and configured properly?

This will tell you if it is or not.

<?php

   php_info();

?>

If you have php this should produce a screen like this one...

http://www.lunargrid.com/info.php

If you don't see this then PHP isn't there.



Alan
0
 
pulupulCommented:
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.
0
 
ldbkuttyCommented:
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>
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
gjirvine3000Author Commented:
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 ""; } ?>
0
 
UmeshMySQL Principle Technical Support EngineerCommented:
<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>
0
 
UmeshMySQL Principle Technical Support EngineerCommented:
Sorry Identical to ldbkutty ..
0
 
ldbkuttyCommented:
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>
0
 
gjirvine3000Author Commented:
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
0
 
ldbkuttyCommented:
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);
?>
0
 
gjirvine3000Author Commented:

Nope, sorry, nothing, still just the word "Phonebook"
0
 
ldbkuttyCommented:
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.
0
 
gjirvine3000Author Commented:

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!
0
 
gjirvine3000Author Commented:

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.
0
 
AlanJDMCommented:
"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
0
 
gjirvine3000Author Commented:
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
0
 
gjirvine3000Author Commented:
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
0
 
ldbkuttyCommented:
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.
0
 
ldbkuttyCommented:
hi,

didnt noticed that you accpeted your answer. Have you solved the problem ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.