Textbook php test code giving error

  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

I am just working through this php book and i am getting a red light in dreamweaver and i can not see a problem at all, could someone with a better eye take a look at this and tell me how bad it is :P

Code: [ Select ]
<?php
/* Program: mysql_up.php
* Desc: Connects to MySQL Server and outputs settings.
* Testing Mysql -- page 35
*/
echo "<html>
<head> <title>test MySQL</title></head>
<body>";
$database = "database"; 
$mysql_user = "mysqluser";
$password = "mysqlpassowrd";
$host="host";
$user="mysqlaccount";
$password="mysqlpasssowrd";

$cxn = mysqli_connect($host, $user. $password);
$sql ="SHOW GLOBAL STATUS";
$result = mysql_query ($cxn,$sql);
if($result == flase)
{
      echo "<h4>error: ".mysqli_eror(cxn)."</h4>";
}
else
{
//* Table that displays
    
echo "<table border='1'>
<tr><th>variable_name</tr>";
for($i = 0; $i < mydwli_num_rows($result); $i++)
{
    echo "<tr>";
    $row_array = mysqli_fetch_row($result);
    for($j = 0;$j < mysqli_num_feilds($result);$j++)
    {
        echo "<td>".row_array[$j]."</td>\n";
    }
        
}
echo "</table>";
    }
?>
</body></html>
  1. <?php
  2. /* Program: mysql_up.php
  3. * Desc: Connects to MySQL Server and outputs settings.
  4. * Testing Mysql -- page 35
  5. */
  6. echo "<html>
  7. <head> <title>test MySQL</title></head>
  8. <body>";
  9. $database = "database"; 
  10. $mysql_user = "mysqluser";
  11. $password = "mysqlpassowrd";
  12. $host="host";
  13. $user="mysqlaccount";
  14. $password="mysqlpasssowrd";
  15. $cxn = mysqli_connect($host, $user. $password);
  16. $sql ="SHOW GLOBAL STATUS";
  17. $result = mysql_query ($cxn,$sql);
  18. if($result == flase)
  19. {
  20.       echo "<h4>error: ".mysqli_eror(cxn)."</h4>";
  21. }
  22. else
  23. {
  24. //* Table that displays
  25.     
  26. echo "<table border='1'>
  27. <tr><th>variable_name</tr>";
  28. for($i = 0; $i < mydwli_num_rows($result); $i++)
  29. {
  30.     echo "<tr>";
  31.     $row_array = mysqli_fetch_row($result);
  32.     for($j = 0;$j < mysqli_num_feilds($result);$j++)
  33.     {
  34.         echo "<td>".row_array[$j]."</td>\n";
  35.     }
  36.         
  37. }
  38. echo "</table>";
  39.     }
  40. ?>
  41. </body></html>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • natas
  • PHP Ninja
  • Proficient
  • natas
  • Posts: 308
  • Loc: AFK

Post 3+ Months Ago

Just took a quick glance...

First thing I saw was a missing </th> tag.

Will look more but try that first.


EDIT:

Lines 11 & 14: "password" is spelled wrong on each.

Line 16. You have a period instead of a comma between your delimiters.

Line 19: FALSE is misspelled but that shouldn't give you an error message.

Line 21: "error" is spelled wrong.

Line 33: "fields" is spelled "feilds" in "mysqli_num_fields". That might be the bugger.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

What are the actual errors that Dreamweaver is giving you?
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

line 36 is red in the right side bar.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Have you tried running the php script? I'd ignore a red line and just run the code to see what errors php gives you.
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

i think i hold too much faith in dreamweaver. I will upload it to the server and set up mysql and see what it says
  • demonmaestro
  • Gold Member
  • Gold Member
  • User avatar
  • Posts: 679
  • Loc: Conroe, Texas

Post 3+ Months Ago

well i see a couple of problems...

PHP Code: [ Select ]
<?php
/* Program: mysql_up.php
* Desc: Connects to MySQL Server and outputs settings.
* Testing Mysql -- page 35
*/
echo "<html>"
."<head> <title>test MySQL</title></head> "
."<body>";
$database = "database";  
$mysql_user = "mysqluser";
$password = "mysqlpassowrd";
$host="host";
$user="mysqlaccount";
$password="mysqlpassword";
 
$cxn = mysql_connect($host, $user. $password);
$sql ="SHOW GLOBAL STATUS";
$result = mysql_query ($cxn,$sql);
if($result == flase)
{
      echo "<h4>error: ".mysql_error(cxn)."</h4>";
}
else
{
//* Table that displays
     
echo "<table border='1'>
<tr><th>variable_name</th></tr>";
for($i = 0; $i < mydwli_num_rows($result); $i++)
{
    echo "<tr>";
    $row_array = mysqli_fetch_row($result);
    for($j = 0;$j < mysqli_num_feilds($result);$j++)
    {
        echo "<td>".row_array[$j]."</td></tr>\n";
    }
         
}
echo "</table>";
    }
?>
</body></html>
 
  1. <?php
  2. /* Program: mysql_up.php
  3. * Desc: Connects to MySQL Server and outputs settings.
  4. * Testing Mysql -- page 35
  5. */
  6. echo "<html>"
  7. ."<head> <title>test MySQL</title></head> "
  8. ."<body>";
  9. $database = "database";  
  10. $mysql_user = "mysqluser";
  11. $password = "mysqlpassowrd";
  12. $host="host";
  13. $user="mysqlaccount";
  14. $password="mysqlpassword";
  15.  
  16. $cxn = mysql_connect($host, $user. $password);
  17. $sql ="SHOW GLOBAL STATUS";
  18. $result = mysql_query ($cxn,$sql);
  19. if($result == flase)
  20. {
  21.       echo "<h4>error: ".mysql_error(cxn)."</h4>";
  22. }
  23. else
  24. {
  25. //* Table that displays
  26.      
  27. echo "<table border='1'>
  28. <tr><th>variable_name</th></tr>";
  29. for($i = 0; $i < mydwli_num_rows($result); $i++)
  30. {
  31.     echo "<tr>";
  32.     $row_array = mysqli_fetch_row($result);
  33.     for($j = 0;$j < mysqli_num_feilds($result);$j++)
  34.     {
  35.         echo "<td>".row_array[$j]."</td></tr>\n";
  36.     }
  37.          
  38. }
  39. echo "</table>";
  40.     }
  41. ?>
  42. </body></html>
  43.  


simple issues i beleaved its fixed now.. try that.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

PHP Code: [ Select ]
<?php
/* Program: mysql_up.php
* Desc: Connects to MySQL Server and outputs settings.
* Testing Mysql -- page 35
*/
 
// Starting page generation
echo "<html>
<head><title>test MySQL</title></head>
<body>\n";
 
// SQL Credentials
$database = "database";
$host = "host";
$user = "mysqlaccount";
$password = "mysqlpassword";
 
// Connecting to the database
$cxn = mysql_connect($host, $user, $password);
$sql = "SHOW GLOBAL STATUS";
$result = mysql_query($sql, $cxn);
 
// Checking for errors
if($result == false)
{
    echo "<h4>error: " . mysql_error($cxn) . "</h4>";
}
else
{
   
    //* Displaying the table
    echo "<table border='1'>
   <tr><th>variable_name</th></tr>\n";
    for($i = 0; $i < mysql_num_rows($result); $i++)
    {
        echo "<tr>\n";
        $row_array = mysql_fetch_row($result);
        for($j = 0; $j < mysql_num_fields($result); $j++)
        {
            echo "<td>{$row_array[$j]}</td>\n";
        }
        echo "</tr>\n";
    }
    echo "</table>";
}
?>
</body></html>
  1. <?php
  2. /* Program: mysql_up.php
  3. * Desc: Connects to MySQL Server and outputs settings.
  4. * Testing Mysql -- page 35
  5. */
  6.  
  7. // Starting page generation
  8. echo "<html>
  9. <head><title>test MySQL</title></head>
  10. <body>\n";
  11.  
  12. // SQL Credentials
  13. $database = "database";
  14. $host = "host";
  15. $user = "mysqlaccount";
  16. $password = "mysqlpassword";
  17.  
  18. // Connecting to the database
  19. $cxn = mysql_connect($host, $user, $password);
  20. $sql = "SHOW GLOBAL STATUS";
  21. $result = mysql_query($sql, $cxn);
  22.  
  23. // Checking for errors
  24. if($result == false)
  25. {
  26.     echo "<h4>error: " . mysql_error($cxn) . "</h4>";
  27. }
  28. else
  29. {
  30.    
  31.     //* Displaying the table
  32.     echo "<table border='1'>
  33.    <tr><th>variable_name</th></tr>\n";
  34.     for($i = 0; $i < mysql_num_rows($result); $i++)
  35.     {
  36.         echo "<tr>\n";
  37.         $row_array = mysql_fetch_row($result);
  38.         for($j = 0; $j < mysql_num_fields($result); $j++)
  39.         {
  40.             echo "<td>{$row_array[$j]}</td>\n";
  41.         }
  42.         echo "</tr>\n";
  43.     }
  44.     echo "</table>";
  45. }
  46. ?>
  47. </body></html>
All it was was spelling errors in functions and a boolean. A few missing dollar signs ($) that indicates a variable... a misplaced </tr>...

I even formatted the code for you :)

Here, instead of just giving you the fixed code, I'll run you through the code you posted and tell you exactly where you went wrong
PHP Code: [ Select ]
<?php
/* Program: mysql_up.php
* Desc: Connects to MySQL Server and outputs settings.
* Testing Mysql -- page 35
*/
echo "<html>
<head> <title>test MySQL</title></head>
<body>";
$database = "database";  
$mysql_user = "mysqluser";
$password = "mysqlpassowrd";
$host="host";
$user="mysqlaccount";
$password="mysqlpasssowrd";
  1. <?php
  2. /* Program: mysql_up.php
  3. * Desc: Connects to MySQL Server and outputs settings.
  4. * Testing Mysql -- page 35
  5. */
  6. echo "<html>
  7. <head> <title>test MySQL</title></head>
  8. <body>";
  9. $database = "database";  
  10. $mysql_user = "mysqluser";
  11. $password = "mysqlpassowrd";
  12. $host="host";
  13. $user="mysqlaccount";
  14. $password="mysqlpasssowrd";

That has nothing bad in it... maybe could be formatted a bit better. The only thing I see there is that you got two password variables in there ($password)

PHP Code: [ Select ]
$cxn = mysqli_connect($host, $user. $password);
$sql ="SHOW GLOBAL STATUS";
$result = mysql_query ($cxn,$sql);
  1. $cxn = mysqli_connect($host, $user. $password);
  2. $sql ="SHOW GLOBAL STATUS";
  3. $result = mysql_query ($cxn,$sql);

You are using mysqli_connect but mysql_query... remove that "i" from mysqli_connect and you should be good.

Another thing... you put the link identifier ($cxn) first... contrary to what you put there, the $sql should be the first argument passed into mysql_query (mysql($sql, $cxn))

PHP Code: [ Select ]
if($result == flase)
{
  1. if($result == flase)
  2. {

That is supposed to be false... not flase

PHP Code: [ Select ]
       echo "<h4>error: ".mysqli_eror(cxn)."</h4>";
}
  1.        echo "<h4>error: ".mysqli_eror(cxn)."</h4>";
  2. }

Here is one place where you missed the dollar sign... that cxn is supposed to be $cxn

PHP Code: [ Select ]
else
{
//*  Table that displays
   
echo "<table border='1'>
<tr><th>variable_name</tr>";
  1. else
  2. {
  3. //*  Table that displays
  4.    
  5. echo "<table border='1'>
  6. <tr><th>variable_name</tr>";

You are missing a </th> right before that </tr> (Wouldn't throw a PHP error... the HTML might look funky though.

PHP Code: [ Select ]
for($i = 0; $i < mydwli_num_rows($result); $i++)
{
  1. for($i = 0; $i < mydwli_num_rows($result); $i++)
  2. {

I've never heard of a mydwli_num_rows function... unless you declared that function somewhere, it's supposed to be mysql_num_rows Probably typing too fast :)

PHP Code: [ Select ]
   echo "<tr>";
   $row_array = mysqli_fetch_row($result);
   for($j = 0;$j < mysqli_num_feilds($result);$j++)
   {
  1.    echo "<tr>";
  2.    $row_array = mysqli_fetch_row($result);
  3.    for($j = 0;$j < mysqli_num_feilds($result);$j++)
  4.    {

Those mysqli_* should be mysql_* (mysql_fetch_row($result) and mysql_num_fields($result)

You also misspelled fields... you spelled it feilds. Typing too fast again (I think).

PHP Code: [ Select ]
      echo "<td>".row_array[$j]."</td>\n";
   }
     
}
echo "</table>";
   }
?>
</body></html>
  1.       echo "<td>".row_array[$j]."</td>\n";
  2.    }
  3.      
  4. }
  5. echo "</table>";
  6.    }
  7. ?>
  8. </body></html>

Another instance where you forgot that dollar sign... that row_array[$j] should have a dollar sign right in front of it. So it should be $row_array[$j] ... the fact that the variable is an array doesn't change anything.

That line could be typed a bit better too... using curly brackets.
PHP Code: [ Select ]
      echo "<td>{$row_array[$j]}</td>\n";

That saves some space :)

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: Liamw411 and 75 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.