Jump to content

Question regarding URLS and PHP Code


mcc_22ri

Recommended Posts

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.

 

Link to comment
Share on other sites

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', 'bob@example.com'), 
('john', 'john@example.com'), 
('mark', 'mark@example.com');

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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';
}

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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()).

Link to comment
Share on other sites

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.