mcc_22ri Posted May 16, 2012 Share Posted May 16, 2012 Hi Everyone, Before I ask my question I would like to give everyone a background on myself. I'm currently in the learning stages of PHP and MySql. (I apol. in advance for my lack of tech. terminology) I have a question regarding how to "make" or "produce" certain pages in php code. In my past I've always manually made a pages. For example, aboutus.html, information.html, cats.html etc ... I think you get the idea. Assuming code is being used. How would one go about "making" or "producing" pages that are fully functional php pages? Where people can log in, make edits, write reviews etc ... (a few examples are below) Do the owners/programmers of the below websites manually "make" or "produce" these pages? http://cars.oodle.com/used-cars/san-jose-ca-area/ http://www.yelp.com/biz/lotus-of-siam-las-vegas http://restaurants.uptake.com/california/san_jose/509917137.html http://www.yelp.com/c/san-jose/restaurants My question is, how can I "make" or "produce" these types of pages for my website? Can anyone offer my any tips/tricks on how to do so. All opinions welcome. Thanks everyone in advance for the help. Quote Link to comment https://forums.phpfreaks.com/topic/262627-question-regarding-urls-and-php-code/ Share on other sites More sharing options...
scootstah Posted May 16, 2012 Share Posted May 16, 2012 Do the owners/programmers of the below websites manually "make" or "produce" these pages? Sort of. They are dynamic pages, but the content still has to come from somewhere. In the examples you provided, URL rewriting is being utilized. This means that there isn't necessarily a physical file associated with the URL. The URL is being dynamically rewritten for semantics, and some other benefits. I will give you a very basic example using a MySQL database. First, make a "users" table. CREATE TABLE users ( id int(11) NOT NULL UNSIGNED AUTO_INCREMENT PRIMARY KEY, name varchar(30) NOT NULL, email varchar(255) NOT NULL ); Let's give it some random data. INSERT INTO users (name, email) VALUES ('bob', '[email protected]'), ('john', '[email protected]'), ('mark', '[email protected]'); So now our three users each have a unique ID. We can then create a page to select one of these three users dynamically. <?php // create a new mysql connection $mysql = new mysqli('localhost', 'root', 'root', 'dbname'); // get the user ID from the URL // it is a numerical ID, so cast to an integer $user_id = (int) $_GET['id']; // try to get the user from the database $result = $mysql->query("SELECT * FROM users WHERE id='$user_id'"); // did we find a user? if ($result->num_rows == 1) { $row = $result->fetch_assoc(); echo 'Name: ' . $row['name'] . '<br /> Email: ' . $row['email']; } else { echo 'Sorry, that user was not found'; } EDIT: Forgot this. The URL for this script would be something like: user.php?id=2. The script takes the "id=2" from the URL, and then selects a user with the id "2". Hopefully you'll get the gist of it, anyway. Quote Link to comment https://forums.phpfreaks.com/topic/262627-question-regarding-urls-and-php-code/#findComment-1346057 Share on other sites More sharing options...
rythemton Posted May 16, 2012 Share Posted May 16, 2012 For most PHP pages, once the script is done running, it should be producing a pure HTML page. PHP is just being used to dynamically generate some of the HTML content. The reason I put 'most PHP pages', is because I've used PHP to generate HTML, JavaScript, CSS, XML, JSON, and others. The output just needs to be what the client is expecting, and for most pages that is HTML. Quote Link to comment https://forums.phpfreaks.com/topic/262627-question-regarding-urls-and-php-code/#findComment-1346059 Share on other sites More sharing options...
mcc_22ri Posted May 17, 2012 Author Share Posted May 17, 2012 Hi Everyone, This is a step by step process of what I did. My goal is to extract data from my database and display that information on my website. I'm running into some errors on how I should do that. Perhaps someone can help me out. All opinions welcome! Thanks! >>>>>>>>>>>> The database I made 5 Colums in phpMyAdmin column name user_id (INT) Primary <--Auto Increment first_name (VARCHAR) length 32 last_name (VARCHAR) length 32 address (VARCHAR) length 32 address (VARCHAR) length 32 I then pressed "save" I then inserted data into my phpmyadmin database. (below is my data) user_id first_name last_name address city 1 Bob Jackson 123 I Hope New York 2 Robert Downney 456 I'm Doing Miami 3 Ken Sorenson 789 This Right Las Vegas >>>>>>>>>>>>> I then made a folder on my website called "practice" In that practice folder I also made another folder called "database" In the database folder I made a page called connect.php In the connect.php I made a connection to my database. connect.php connection code code <?php $connect_error = 'Sorry we\'re experiencing connection issues'; mysql_connect("localhost","myname","mypassword") or die($connect_error); mysql_select_db('mydatabase') or die($connect_error); ?> http://whatsmyowncarworth.com/practice/database/connect.php http://whatsmyowncarworth.com/practice/init.php http://whatsmyowncarworth.com/practice/index.php >>>>>>>>>>>>>>>>>>>> In the practice folder I made two pages. Those two pages are called init.php and index.php (the practice folder is acting as my root folder) (I tried using this code -> <?php include 'practice/init.php';?> <- on the index.php but kept getting errors. So I switched to the below code.) index.php code <?php require 'database/connect.php'; echo 'Hello World!'; ?> >>>>>>>>>>>>>>>>>>>> init.php code <?php require 'database/connect.php'; ?> >>>>>>>>>>>>>>>>>>>> I'm assuming up until now everything is going well. Now my major concern is to be able to "pull" or "get" (my apol. for lack of terminology) data from the MySql database. This is where the confusion is starting to come into play. I'm going to give the below code a try on index.php and see if it works. My index.php page code now. How come this didn't work? What am I doing wrong? <?php include 'practice/init.php'; // get the user ID from the URL // it is a numerical ID, so cast to an integer $user_id = (int) $_GET['user_id']; // try to get the user from the database $result = $mysql->query("SELECT * FROM info WHERE id='$user_id'"); // did we find a user? if ($result->num_rows == 1) { $row = $result->fetch_assoc(); echo 'Name: ' . $row['first_name'] . '<br /> Email: ' . $row['city']; } else { echo 'Sorry, that user was not found'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262627-question-regarding-urls-and-php-code/#findComment-1346203 Share on other sites More sharing options...
scootstah Posted May 17, 2012 Share Posted May 17, 2012 It is because in your example, you are using the mysql extension but in my example I am using the mysqli (notice the i) extension. The mysqli extension is the "updated" version of the mysql extension, and they are not backwards-compatible. The mysql extension was made for older versions of PHP, but has stuck around. It no longer has active development, and should really be avoided. Quote Link to comment https://forums.phpfreaks.com/topic/262627-question-regarding-urls-and-php-code/#findComment-1346206 Share on other sites More sharing options...
mcc_22ri Posted May 17, 2012 Author Share Posted May 17, 2012 Hi Scootstah, Thanks for the fast reply. It is because in your example, you are using the mysql extension but in my example I am using the mysqli (notice the i) extension. I changed mysql to mysqli in my connect.php page. It still isn't working. Perhaps I would make the connection string into a variable and it should work? ...... Old Code <?php $connect_error = 'Sorry we\'re experiencing connection issues'; mysql_connect("localhost","myname","mypassword") or die($connect_error); mysql_select_db('mydatabase') or die($connect_error); ?> New Code <?php $connect_error = 'Sorry we\'re experiencing connection issues'; mysqli_connect("localhost","myname","mypassword") or die($connect_error); mysqli_select_db('mydatabase') or die($connect_error); ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/262627-question-regarding-urls-and-php-code/#findComment-1346211 Share on other sites More sharing options...
scootstah Posted May 17, 2012 Share Posted May 17, 2012 In most cases you can simply add an "i" to the function to use the mysqli extension. However, there are a few small differences. Also, the mysqli extension supports both a procedural and an object-oriented API. You have to pick one though, you can't use them interchangeably. In my example I was using the object-oriented API, but in that last snippet you posted you are using the procedural API - which is fine, you just need to update the other things to procedural code. One more thing - the mysqli extension doesn't need the mysqli_select_db() call, because the database is usually selected during the connection. Keep in mind also that you need to carry around the mysqli connection for use with the procedural functions. The connection link identifier must be the first parameter for some of the functions (like mysqli_query()). Quote Link to comment https://forums.phpfreaks.com/topic/262627-question-regarding-urls-and-php-code/#findComment-1346214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.