Jump to content
Larry Ullman's Book Forums

Frustrating Mysql Error!


Recommended Posts

At this point, I imagine Larry and Matt are the only ones viewing this post, but here's an update:

 

After discussing the site in more detail with Matt, we decided that it would be best to create the galleries from the index.php pages directly, and not via a separate function being called. With that said, we created the index.php files so that if changes ever need to be made, we don't need to edit the index.php files.

 

Here's the flow of the index.php files (the pseudocode):

 

<?php

 // Include config file.

 // Include DB connection.

 // Make DB query. The appropriate DB query is automatically constructed when the index.php file is made.

 // Include header file. The header file is equipped with the logic to handle a failed DB query as well. (This more or less equates to handling the page title, etc.)

 // If the DB query is successful, include the HTML page that loads the content based on the query results.

 // Otherwise, load an error HTML file.

 // Outside of the if statement, include the footer.

?>

 

That made the most sense to us, and it's very similar to the view files you use for your second example site in the book, Larry. Any thoughts are appreciated, but at this point, it more or less seems fine to us.

Link to comment
Share on other sites

That seems to make a lot of sense to me.

 

As for using a function vs. an included file, HartleySan has a point, from a theory perspective. However, whether you use a function or an includable file, you only have one bit of code to ever edit. I would argue that creating a script that just defines a function and calls it is the same thing as creating an includable file that does the same work, except that if you define a function, you require a lot of extra memory overhead for something that only gets done once. Seems wasteful and unnecessary to me, but I can appreciate the desire to wrap functionality in a function. That's why I suggested creating a page that's run directly that does all the work: it seems less kludgey.

 

Glad you figured it out!

Link to comment
Share on other sites

Larry,

 

I`ve implemented the changes and everything appears to work fine!

 

What I've done is written the directory name to a variable in the index.php file, so that this can be used by the database query to grab all the data for that specific gallery.

I also use it when an artist visits another gallery which isn't his/her own. In the sidebar menu, there is a conditional which compares this variable with the 'directory_name' pulled from the database when he/she logs in. If they are different, then the sidebar will only display a link to his/her gallery. If they are the same, then the menu will show links for updating their profile, uploading images, etc...

What do you think of this?

 

Here is an example index file written to a folder called "MattsGallery":

<?php
require ('../../includes/config.inc.php');
require (MYSQL);
$dir = 'MattsGallery';
require ('../../includes/show_gallery.inc.php');
?>

 

Here is the show_gallery.php file:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
include ('../../includes/gallery_login.inc.php');
}

$q = "SELECT user_id, first_name, last_name, image, gallery_name, description FROM profiles WHERE directory_name='$dir'";
$r = mysqli_query ($dbc, $q);

// Get the number of rows returned:
$rows = mysqli_num_rows($r);

if ($rows == 1)
{
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
}

// Include the header file:
include ('../../includes/gallery_header.html');

echo '<div id="content">';

/* PAGE CONTENT STARTS HERE! */
include('../../includes/gallery_content.html');

echo '</div>';

include ('../../includes/gallery_footer.html');

// Omit the closing PHP tag to avoid 'headers already sent' errors!
?>

 

What do you think Larry? Is this what you were recommending?

 

Thanks,

 

Matt

Link to comment
Share on other sites

Not sure about the following:

 

// Omit the closing PHP tag to avoid 'headers already sent' errors! 
?>

 

But in all seriousness, I would do a few things differently:

1) I would not put any HTML in the file directly. The opening and closing div tags should go in the header and footer respectively.

2) I think you need the if/else conditional to also serve up an errors.html page if the database query is not successful.

3) I think the logic for the login should be in the header.html file, although I have not admittedly thought this through thoroughly yet.

4) You could just use the PHP $_SERVER superglobal to get the part of the URL you want for the directory name, instead of setting it statically. (Optional)

5) I still think it's unnecessary to include the show_gallery script. Seems a lot easier to just put the code in the index files directly. If you design them properly, they'll never have to be edited anyway.

 

Was gonna wait for Larry to post, but decided to just chime in anyway.

Link to comment
Share on other sites

My only thoughts on what you've posted is that I'd use more constants and absolute paths. You include some files as constants (like MYSQL) but then many other things using ../../ When I have a site that I know will have lots of directories and directories within other directories, absolute paths are going to work more reliably. Also, if you define, for example, the gallery script as a constant, you can change the value of that constant in the configuration file and the change will be reflected everywhere.

Link to comment
Share on other sites

My only thoughts on what you've posted is that I'd use more constants and absolute paths. You include some files as constants (like MYSQL) but then many other things using ../../

Thank you so much Larry! I agree about using absolute paths. I just did it this way when I was developing because it was the only way that worked without too many headaches! I had originally tried to use absolute paths in the include statements, but kept getting errors.

 

Doing the following:

include(BASE_URL . 'includes/something.php');

...always caused errors! I will try it again and see if I was overlooking something.

 

Should I use 'BASE_URI' instead? Which is better? This can get quite confusing!

 

Or, should I define a constant like this:

define('INCLUDES','www.example.com/includes/');

Also, I did a Google search for including files with absolute paths and it gave a few different results for how to do this, and it illustrated what a hideous and overcomplicated mess people have made using php to set up "absolute paths"! The most common solution seemed to be:

define('ROOT', $_SERVER['DOCUMENT_ROOT');

and in the including file

include_once(ROOT."/includes/someFile.php");

...or by defining the path to the 'includes' directory by putting the following code in a config file in that directory:

define(‘ABSPATH’, dirname(__FILE__).’/');]

...and then including the config file and another file:

include('config.php');
require_once(ABSPATH . 'somefile.php’);

I personnaly think your way of hard coding the constants in a config.inc.php file is the best, but I just want to get your take on these other ways of doing it. It just seems like people try to make things more complicated than they need to be.

 

Thanks Larry,

 

Matt

Link to comment
Share on other sites

You would use BASE_URI, because you're including a file on the system, not BASE_URL, which is a URL to a Web page.

 

As for those more fluid options, the benefit is that they allow you to move the code from one site to another without modification. The downside is that PHP will need to do a bit more of extra work determining the current location or document root or whatever on each page call. Also, what, specifically, you use will depend upon what script has that code in it. If you want simple and reliable, I would just hardcode it.

Link to comment
Share on other sites

Thanks Larry! Sorry I didn't respond sooner.

You would use BASE_URI, because you're including a file on the system, not BASE_URL, which is a URL to a Web page.

Just to clarify, for each include I would do the following:

include (BASE_URI . 'html/includes/gallery_content.inc.php');

 

There is also the paradox of how to include the config file.

 

Should I use:

include ('home/tueslcom/html/includes/config.inc.php');

or

include ('../../includes/config.inc.php');

I know that most of this is just semantical, but I think it is still important and others can learn from it.

 

Thanks again Larry,

 

Matt

Link to comment
Share on other sites

My inclination would be to use BASE_URI to help you create a constant and then include that constant in every file.

 

As for your second question, the first option is ALMOST an absolute path, it's just missing the initial /. The second option is a relative path. When you have a site with lots of subdirectories, there's a big advantage to using absolute paths.

Link to comment
Share on other sites

Jonathon and Larry,

 

Thanks a lot for your help!

 

Ok, I'll convert everything to absolute paths! Also, Larry, when you say create a constant, you mean one for the "includes" folder right?

 

My "includes" folder is getting pretty filled up with files. I'm starting to think I should also create a separate one for the gallery files (i.e. "gallery_includes"). This way the site will be more compartmentalized and better organized.

 

I much prefer the look of relative, but when working with .htaccess in particular, it just really complicates the issue.

Yeah Jonathon, I heard that you can set up include paths in .htaccess files. Is this what you were referring to?

Link to comment
Share on other sites

No, I'm suggesting you create constants representing key files that are included, just like I do with the MySQL connection script. By defining the entire path to the file, including the file's name itself, as a constant, you'd only ever have to change one line in one configuration file should you need to make a big change later.

Link to comment
Share on other sites

No, I'm suggesting you create constants representing key files that are included, just like I do with the MySQL connection script. By defining the entire path to the file, including the file's name itself, as a constant, you'd only ever have to change one line in one configuration file should you need to make a big change later.

 

I've got it! I think I'm going to have about 8 constants for the various gallery includes (header, footer, content,etc...), but I don't think this will be a problem.

 

Thanks Larry!

Link to comment
Share on other sites

 Share

×
×
  • Create New...