Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 14th, 2008, 01:57 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2008
Location: New York, NY, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rhieger
Default Cannot Get PHP Code Example in Chapter 1 to Work

I have a moderate amount of experience working with MySQL, having taught myself some of the basics. I can certainly successfully install the server and get it to interface with Apache HTTP Server. In my current configuration, I have successfully installed:

Apache HTTP Server 2.2.9
PHP 5.2.6
5.0.51b-community-nt MySQL Community Edition (GPL)

I also have phpMyAdmin configured and working successfully with existing databases.

My problem is that the embedded PHP script in the HTML file example on p. 35-36 in Chapter 1 does not properly display the two columns of data specified, even though I defined the database according to the specs in the text.

Rather than displaying the columns headed with "Part Name" and "Part Number," the PHP itself is display before and after the table. The table itself displays with the proper header columns, but the cells underneath the header have the following code displayed in them:

" . $row[0] . " "; echo " " . $row[1] . "

The code I entered is identical to that found in the book, Beginning MySQL, other than the fact that I substituted server name, user name and password for those consistent with values on my workstation. Also, rather than using the <b></b> HTML tags within <td></td> tags, I substituted the <th></th> HTML tags. This should not have caused a problem, I would think given that PHP would correctly parse the HTML tags.

Pasted in below is the code from the HTML page as I entered it into Windows Notepad:

================================================== ===================
CONTENTS OF parts.html
================================================== ===================
<html>

<head>
<title>The Parts Table</title>
</head>

<body bgcolor="white">

<?php

// Define the variables necessary to access MySQL.

$host="localhost";
$user="username"
$pw="password";

$db="test";

// Establish a connection to MySQL.

$connection=mysql_connect($host,$user,$pw) or die("Connection failed!");

// Select the MySQL database.

mysql_select_db($db) or die("Unable to connect to database.");

// Create an SQL query.

$query="SELECT PartName, PartID FROM Parts ORDER BY PartName";

// Execute the query and store the query results in memory.

$result=mysql_query($query) or die ("Query failed!");

// Process the query results.

if (mysql_num_rows($result) > 0)

{

  echo "<h2>Data from the Parts Table<br>
            in the MySQL Test database</h2>";

  echo "<br>";

  echo "<table cellpadding=10 border=1>";
  echo "<thead>";
  echo "<tr bgcolor="#ffcccc">
          <th>Part Name</th>
          <th>Part Number</th>
        </tr>";
  echo "</thead>";

  echo "<tbody>";

  while ($row=mysql_fetch_row($result))

  {

    echo "<tr>";
    echo "<td>" . $row[0] . "</td>";
    echo "<td>" . $row[1] . "</td>";
    echo "</tr>";

  }

  echo "</tbody>";

  echo "</table>";

}

else

{

  echo "The Parts table is empty.";

}

// Free the memory used for the query results.

mysql_free_result($result);

// Close the connection to MySQL.

mysql_close($connection);

?>

</body>

</html>
================================================== ===================

Any assistance you can provide is greatly appreciated.

Sincerely,

Robert Hieger
Reply With Quote
  #2 (permalink)  
Old July 24th, 2008, 11:35 AM
Registered User
 
Join Date: Jul 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to luka
Default

Please check your 'username' and 'password'.
If you use localhost, username should be changed to 'root' and password is your database password.If you don't have a database password that field should be a blank.

luka
(jayathilaka@gmail.com)
www.itpathfinders.com
www.maxplayers.blogspot.com
www.fun-lanka.blogspot.com

Good luck
Reply With Quote
  #3 (permalink)  
Old July 24th, 2008, 03:40 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2008
Location: New York, NY, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rhieger
Default

Dear Luka,

Many thanks for your suggestions. I am using localhost. In fact, my user name was set to a username other than root. I have changed the username to "root" per your suggestion. There was no password set for the database. In fact, I'm not sure how to do that. You said that the password field should be blank. I am assuming the resulting PHP code should look like this:

$pw=" ";

Unfortunately, neither of these changes in code result in a display of rows from the two column table in question.

Did you mean I should omit the $pw=" " line altogether?

If you have any further thoughts, it would be greatly appreciated.

Many thanks,

Robert Hieger

Quote:
quote:Originally posted by luka
 Please check your 'username' and 'password'.
If you use localhost, username should be changed to 'root' and password is your database password.If you don't have a database password that field should be a blank.

luka
(jayathilaka@gmail.com)
www.itpathfinders.com
www.maxplayers.blogspot.com
www.fun-lanka.blogspot.com

Good luck
Reply With Quote
  #4 (permalink)  
Old July 26th, 2008, 01:02 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: South San Francisco, CA, USA.
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
Default

Robert,
The book your are referencing to has an older version of MySQL.
Probably 4.1 or lesser.
I you have mentioned you got 5.0 version intalled.
Under the 5.0 version, almost all mysql properties/functions
now uses mysqli.
For example:
$dbc = new mysqli($host, $username, $password, $database);
I hope this helps,
John

Reply With Quote
  #5 (permalink)  
Old July 26th, 2008, 03:44 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2008
Location: New York, NY, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rhieger
Default

Dear John,

Many thanks for your response. It is true that the Beginning MySQL book seems to reference MySQL v4.01. Oddly, this hadn't occurred to me.

I have attempted your suggestion. Unfortunately, this does not seem to solve the problem either. Is it possible that I have PHP configured incorrectly? The phpinfo() command shows that I have support for both mysql and mysqli enabled, so this seems doubtful. Maybe I did not adjust my code appropriately. I am therefore posting my revised code here for your reference. If you see anything wrong with this, or have any further suggestions, your insights are greatly appreciated:

<html>

<head>
<title>The Parts Table</title>
</head>

<body bgcolor="white">

<?php

// Define the variables necessary to access MySQL.

$host="localhost";
$user="username";
$pw="password";
$database="test";

// Make connection to MySQL Database.

$dbc=new mysqli($host, $user, $pw, $database);

// Create an SQL query.

$query="SELECT PartName, PartID FROM Parts ORDER BY PartName";

// Execute the query and store the query results in memory.

$result=mysql_query($query) or die ("Query failed!");

// Process the query results.

if (mysql_num_rows($result) > 0)

{

  echo "<h2>Data from the Parts Table<br>
            in the MySQL Test database</h2>";

  echo "<br>";

  echo "<table cellpadding=10 border=1>";
  echo "<thead>";
  echo "<tr bgcolor="#ffcccc">
          <th>Part Name</th>
          <th>Part Number</th>
        </tr>";
  echo "</thead>";

  echo "<tbody>";

  while ($row=mysql_fetch_row($result))

  {

    echo "<tr>";
    echo "<td>" . $row[0] . "</td>";
    echo "<td>" . $row[1] . "</td>";
    echo "</tr>";

  }

  echo "</tbody>";

  echo "</table>";

}

else

{

  echo "The Parts table is empty.";

}

// Free the memory used for the query results.

mysql_free_result($result);

// Close the connection to MySQL.

mysql_close($connection);

?>

</body>

</html>

================================================== ===================
END OF CODE
================================================== ===================

Please note that for security reasons, I have changed the username and password to generic names.

Again, many thanks for your help.

Sincerely,

Robert Hieger

Quote:
quote:Originally posted by jmaronilla
 Robert,
The book your are referencing to has an older version of MySQL.
Probably 4.1 or lesser.
I you have mentioned you got 5.0 version intalled.
Under the 5.0 version, almost all mysql properties/functions
now uses mysqli.
For example:
$dbc = new mysqli($host, $username, $password, $database);
I hope this helps,
John

Reply With Quote
  #6 (permalink)  
Old July 27th, 2008, 11:16 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: South San Francisco, CA, USA.
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
Default

Do it this way:

<html>

<head>
<title>The Parts Table</title>
</head>

<body bgcolor="white">

<?php

// Define the variables necessary to access MySQL.

$host="localhost";
$user="username";
$pw="password";
$database="test";

// Make connection to MySQL Database.

$dbc=new mysqli($host, $user, $pw, $database);

// Create an SQL query.

$query="SELECT PartName, PartID FROM Parts ORDER BY PartName";

// Execute the query and store the query results in memory.

$result=$dbc->($query) or die ("Query failed!");

// Process the query results.

if ($result->num_rows > 0)

{

  echo "<h2>Data from the Parts Table<br>
            in the MySQL Test database</h2>";

  echo "<br>";

  echo "<table cellpadding=10 border=1>";
  echo "<thead>";
  echo '<tr bgcolor="#ffcccc">
          <th>Part Name</th>
          <th>Part Number</th>
        </tr>';
  echo "</thead>";

  echo "<tbody>";

  while ($row=result->fetch_object())

  {

    echo "<tr>";
    echo "<td>" . $row->PartName . "</td>";
    echo "<td>" . $row->PartID . "</td>";
    echo "</tr>";

  }

  echo "</tbody>";

  echo "</table>";

}

else

{

  echo "The Parts table is empty.";

}

// Free the memory used for the query result.
$result = null;

// Close the connection to MySQL.

$dbc->close();

?>

Try it.
It should work. And also note mysql properties & functions on v4 or less should be changed to mysqli props & functions. But since
mysqli is an object already, I just assign this object to a variable
as $dbc in example above.
Hope it helps.
cheers,
John
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 15 simple-mailer.php doesn't work Corey Wirun BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 June 18th, 2003 03:13 PM



All times are GMT -4. The time now is 06:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.