Jump to content

mysqli OOps or procedural AND .php file naming


Gilbert

Recommended Posts

Hi all,  I have a couple of questions that popped up while studying php; really not questions, but I want to get some confirmation that my deductions are correct from what I've figured out.  I was getting very confused over the differences in the object-oriented mysqli and the procedural mysqli and translating from one to the other.   I think I like using OOps better The reference list on w3 only shows the procedural syntax so I wanted to make sure I was coming up with the correct OOps.  Things like 'mysqli_query($conn,$sql)' becomes '$conn->query($sql)'.  I could see that quite clearly in the tutorial, but some others get a little hazy like 'mysqli_free_result($result)' becomes '$result->free_result'.  Do I have this correct?  and is there a rule about changing from one to the other - like whatever the object of the function is goes first in the OOps syntax, or something.  Now I noticed that when you check for a connection error the OOps syntax says '$conn->connect_err' while the procedural syntax is 'mysqli_connect_error()' with nothing in the parenthesies.  Is that because the connection object hasn't actually been established yet? or some other mysterious reason.
I also had a quick question about .php files in general.  The w3 said that a file with php in it should end with .php, but when I do that the server won't execute it - so I change it to html and its OK.  Now if I have a file with exclusively php code and I don't 'echo' any output, but only 'return' a value, do I still need to write the <!DOCTYPE> and other tags like <head>, etc.  Or can I start the file with <?php and end it with ?>.  Like I said , I think I have these things correctly in my mind, but if I'm not right I want to nip it in the bud and make sure I'm off on a good foundation.   I've experimented with different ideas, but if it doesn't work, sometimes you can't figure out what you did wrong.   Thank you so much for your input and answers!!

Link to comment
Share on other sites

When in doubt, check the manual:

http://php.net/manual/en/book.mysqli.php

Note that there are multiple classes.  There is the mysqli class, the mysqli_stmt class, and the mysqli_result class, among others.

Now I noticed that when you check for a connection error the OOps syntax says '$conn->connect_err' while the procedural syntax is 'mysqli_connect_error()' with nothing in the parenthesies.  Is that because the connection object hasn't actually been established yet? or some other mysterious reason.

The mysterious reason is because that function doesn't take any parameters.  In OOP, it's just a property with a value.  In procedural you call a function to get the value.

http://php.net/manual/en/mysqli.connect-error.php

The w3 said that a file with php in it should end with .php, but when I do that the server won't execute it - so I change it to html and its OK.

If your server is executing .html files as PHP, and not executing .php files, that sounds like a weird way to configure the server.  It's usually the other way around.

Now if I have a file with exclusively php code and I don't 'echo' any output, but only 'return' a value, do I still need to write the <!DOCTYPE> and other tags like <head>, etc.

You output anything that you want PHP to send to the browser.  If you have a doctype, or any other HTML outside of PHP, PHP is just going to send that to the browser as output.  So whether or not you should do that depends on what output you want the browser to get.  Ultimately a request to the server for a web page should result in a single valid HTML document.

Or can I start the file with <?php and end it with ?>.

Most PHP files are only PHP code.  With big projects, output is handled using a template library so there's no HTML mixed around in the PHP code anyway.  The ending PHP tag is optional and is commonly left out of files that only contain PHP code.

Link to comment
Share on other sites

Thank you very much for the good help straightening out my confusions.  In the questions about the ext being .php or .html I should have said my confusion was because w3 uses several examples with a lot of php intertwined with html, but now I see that they should have the .html ext and leave the php ext for purely php files.   On that subject, is it normal for a website to have a lot of little php files which may be called from an xmlhttp request, or something, or could a php file have several user-defined functions and then they are called when needed?    It would seem easier to remember the name of a function and harder to keep track of 10 or 12 individual files.  Can you invoke a specific php function from javascript?    

Link to comment
Share on other sites

No, you use the browser to send a request to the server, that's all.  Like any other request, you can send a get request where you add data to the URL in the querystring, or you can send a post request where you add data to the request body, and PHP can access those values using $_GET or $_POST.  You can write PHP code to look at those values and things and figure out what else to do, like whether to include a file or run a function or whatever based on the request.  All requests are the same, it doesn't matter if they're sent using ajax or just by typing a URL into your browser.

Also, in general, whenever a file has any PHP code at all (or may in the future), it is named with a .php extension or whatever other extension is used by the web server to send files to PHP.  By far the most common is .php because that's how most servers are set up, so if you want people to be able to move your code to their server and run it, it has to be named in a way that they would expect.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...