I am stumped

ok, I am just learning php and mysql and how to use them to create a database driven website.

I have started on the mysql.com site with the first article in the tech resources tutorial for creating a database driven website using php and mysql. I have followed everything in the instructions, I am now up to article 23 and when I execute the code for the display of DB content in HTML paragraphs, I get nothing, it’s just a blank page with a title and that’s it.

mysql 5.5.12
php 5.3.6

here is the code I am using, I have hand typed it a dozen times, I even copied and pasted it and changed the necessary parameters for it to work, nonetheless, here is the code:


<HTML> 
<HEAD> 
<TITLE> Our List of Jokes </TITLE> 
<HEAD> 
<BODY> 
<?php 

// Connect to the database server 
$dbcnx = @mysql_connect("localhost", 
"root", "*******"); 
if (!$dbcnx) { 
echo( "<P>Unable to connect to the " . 
"database server at this time.</P>" ); 
exit(); 
} 

// Select the jokes database 
if (! @mysql_select_db("jokes") ) { 
echo( "<P>Unable to locate the joke " . 
"database at this time.</P>" ); 
exit(); 
} 

?> 
<P> Here are all the jokes in our database: </P> 
<BLOCKQUOTE> 

<?php 

// Request the text of all the jokes 
$result = mysql_query( 
"SELECT JokeText FROM Jokes"); 
if (!$result) { 
echo("<P>Error performing query: " . 
mysql_error() . "</P>"); 
exit(); 
} 

// Display the text of each joke in a paragraph 
while ( $row = mysql_fetch_array($result) ) { 
echo("<P>" . $row["JokeText"] . "</P>"); 
} 

?> 

</BLOCKQUOTE> 
</BODY> 
</HTML> 

I have also created the ‘jokes’ DB and the “Jokes” table as per the instructions, so far I am at a loss.

cory

Welcome to Sitepoint twinrecords.

Try this, and see how you get on. :slight_smile:


<?php
error_reporting(-1);
ini_set('display_errors', true);

$con = mysql_connect('localhost', 'user', 'pass');

if(false === is_resource($con)){
  echo 'Cannot connect to database: ' . mysql_error();
  exit;
}

if(false === mysql_select_db('database', $con)){
  echo 'Cannot select database: ' . mysql_error();
  exit;
}

$result = mysql_query('SELECT JokeText FROM Jokes;');

if(false === is_resource($result)){
  echo 'Cannot select jokes: ' . mysql_error();
  exit;
}

?>
<html>
  <head>
    <title>My Joke Database</title>
  </head>
  <body>
    <h4>
      We have <?php echo mysql_num_rows($result); ?> jokes in our database.
    </h4>
    <?php while($joke = mysql_fetch_assoc($result)): ?>
      <p>
        <?php echo $joke['JokeText']; ?>
      </p>
    <?php endwhile; ?>
  </body>
</html>

thanks for the help, but I got a fatal error:Call to undefined function mysql_connect() in /var/www/html/index.php on line

oops, I am an idiot, hold on

ok, I went and fixed my mistakes, and I am still getting the fatal error call to undefined function

Okay, it appears you do not have the mysql extension enabled.

Take a peek at these instructions: PHP FAQ - MySQL Forge Wiki

Also, it maybe worth altering the code I offered to:


<?php
error_reporting(-1);
ini_set('display_errors', true);

if(false === function_exists('mysql_connect')){
  echo 'The mysql extension is not enabled';
  exit;
}

$con = mysql_connect('localhost', 'user', 'pass');

if(false === is_resource($con)){
  echo 'Cannot connect to database: ' . mysql_error();
  exit;
}

if(false === mysql_select_db('database', $con)){
  echo 'Cannot select database: ' . mysql_error();
  exit;
}

$result = mysql_query('SELECT JokeText FROM Jokes;');

if(false === is_resource($result)){
  echo 'Cannot select jokes: ' . mysql_error();
  exit;
}

?>
<html>
  <head>
    <title>My Joke Database</title>
  </head>
  <body>
    <h4>
      We have <?php echo mysql_num_rows($result); ?> jokes in our database.
    </h4>
    <?php while($joke = mysql_fetch_assoc($result)): ?>
      <p>
        <?php echo $joke['JokeText']; ?>
      </p>
    <?php endwhile; ?>
  </body>
</html>