Subject: Cannot Get PHP Code Example in Chapter 1 to Work
Posted By: rhieger Post Date: 7/14/2008 1:57:26 PM
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 By: luka Reply Date: 7/24/2008 11:35:59 AM
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 By: rhieger Reply Date: 7/24/2008 3:40:48 PM
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:
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 By: jmaronilla Reply Date: 7/26/2008 1:02:42 AM
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 By: rhieger Reply Date: 7/26/2008 3:44:35 PM
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:
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 By: jmaronilla Reply Date: 7/27/2008 11:16:27 AM
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



Go to topic 73023

Return to index page 5
Return to index page 4
Return to index page 3
Return to index page 2
Return to index page 1