Jump to content

Recommended Posts

I'm honestly not sure what I'm doing wrong. 

 

I am a VERY novice PHP user.  I can do very basic things by myself, but for everything else, I turn to the Web or to a book.

 

I am trying to build a shopping cart using the example in PHP and MySQL Web Development, a book by Luke Welling and Laura Thompson.

 

I tried using the book as I went along, making modifications as I went, but the script didn't work--no errors, just a blank screen where there should have been output.  Soooo....I went to the CD and uploaded their source files.  Same thing.  Nothing.

 

I'm pretty sure there's nothing wrong with their script, but I can't figure out what's wrong.  I don't know if it's my version of PHP being incompatible, or the fact that I am using Yahoo Small Business.

 

Here are a few snippets:

 

<?php
  include ('book_sc_fns.php');
  // The shopping cart needs sessions, so start one
  session_start();
  do_html_header('Welcome to Book-O-Rama');

  echo '<p>Please choose a category:</p>';

  // get categories out of database
  $cat_array = get_categories();

  // display as links to cat pages
  display_categories($cat_array);

  // if logged in as admin, show add, delete, edit cat links
  if(isset($_SESSION['admin_user']))
  {
    display_button('admin.php', 'admin-menu', 'Admin Menu');
  }
  do_html_footer();
?>

 

It will display the header.  It displays "Please choose a category."  But, when it comes time to call to the functions get_categories(), display_categories(), and display_button(), NOTHING HAPPENS.  The screen is blank from there on down.

 

All of the necessary files to be included have been uploaded, so that's not the problem.  Here's one of those functions:

 

function get_categories()
{
   // query database for a list of categories
   $conn = db_connect();
   $query = 'select catid, catname
             from categories'; 
   $result = @$conn->query($query);
   if (!$result)
     return false;
   $num_cats = @$result->num_rows;
   if ($num_cats ==0)
      return false;  
   $result = db_result_to_array($result);
   return $result; 
}

 

Okay.  So--what am I missing?  It's not that my database doesn't work, because on my own example, I can list the categories by doing this:

$query = "SELECT * FROM categories"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
 $catid = $row['catid'];
 $catname = $row['catname'];
 $display_block .= "<li> <a href=\"show_cat.php?catid=$catid\">$catname</a></li>"; 
}

$output .= "<ul>$display_block</ul></td></tr></table>";

print $output;

 

I have a phpinfo file that I will leave up while I'm trying to figure this out:  http://www.mickiclark.com/book_sc/phpinfo.php

 

Thanks in advance for any clarification you can provide.  I am trying to teach myself, but when the examples don't work, I don't know enough to understand why! 

 

Edit to add:  I have also tested a simple function and using include vs require, and it worked.

function writeName()
{
echo "Suzie Q. Public";
}

echo "My name is ";
writeName();

Link to comment
https://forums.phpfreaks.com/topic/199791-problems-with-functions/
Share on other sites

You should be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that all the errors that php detects will be reported and displayed. If you don't have access to the master php.ini, you can set the equivalent settings in a local php.ini (when php is running as a CGI application) or in a .htaccess file (when php is running as an Apache Module.)

 

You should also be doing this on a local development system, as constantly uploaded code to a live server to check what a change does, wastes a lot of time.

In a perfect world, yes, I would.  In the real world, I am a high school teacher that's just trying to add one simple functionality to my site.  I'm not trying to learn it all--just enough to get by.

 

I have run a debug of each file in my PHP editor (PHP Designer 2007) and do not get any error output.  I'm going to assume that's not that much different than what you suggested.  That's the only reason the files are being uploaded to my server for testing.  According to my PHP editor, there *are* no errors.  What that makes me think is either I have some sort of problem with the PHP version OR that my host doesn't support the things I am trying to do.  I'm just trying to figure out which it is before I spend hours trying to come up with a new solution (like I ended up doing with the categories).

The end of life of php4 was over two years ago. Your web host should have provided a way of upgrading to php5, either through a choice in your hosting control panel or through a setting in a .htaccess file.

 

There are very few incompatible changes between php4 and php5. Unless the code is using mysqli or php5's OOP syntax, it should work under php4.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.