Solved

PHP script to print records from MySQL database

Posted on 2004-08-13
18
313 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:gjirvine3000
  • 7
  • 6
  • 2
  • +2
18 Comments
 
LVL 3

Expert Comment

by:pulupul
ID: 11791497
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11791525
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
 

Author Comment

by:gjirvine3000
ID: 11791532
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
 
LVL 26

Expert Comment

by:ushastry
ID: 11791534
<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
 
LVL 26

Expert Comment

by:ushastry
ID: 11791537
Sorry Identical to ldbkutty ..
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11791561
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
 

Author Comment

by:gjirvine3000
ID: 11791584
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11791606
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
 

Author Comment

by:gjirvine3000
ID: 11791619

Nope, sorry, nothing, still just the word "Phonebook"
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 32

Expert Comment

by:ldbkutty
ID: 11791669
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
 

Author Comment

by:gjirvine3000
ID: 11791742

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
 
LVL 9

Accepted Solution

by:
AlanJDM earned 250 total points
ID: 11791784
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
 

Author Comment

by:gjirvine3000
ID: 11791892

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
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11791932
"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
 

Author Comment

by:gjirvine3000
ID: 11792522
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
 

Author Comment

by:gjirvine3000
ID: 11792541
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11793561
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11793579
hi,

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now