Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 PHP MYSQL linking
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
evogre3n




PostPosted: Tue Oct 19, 2004 6:49 pm   Post subject: PHP MYSQL linking

Okay

I have a mysql databse with a table, and I need the URL link to query information from the mysql database...

like, i have 10 rows, and 5 of those rows are type 1,

so how can i query it in the URL adress bar?

like:

http://mydomain.com/index.php?id=x&type=1

Do you understand what i mean? If you do, help is appreciated!
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Tue Oct 19, 2004 7:20 pm   Post subject: (No subject)

Basically what JHanson said under me, only for a bit easier to read and understand I hope.

This is what I thought you ment, but I wasn't sure.
code:
<?
// Make the values shorter and easier to work with
$id = $_GET['id'];
$type = $_GET['type'];

// Connect to the database server
$connection = mysql_connect($server, $username, $password) or die("Unable to connect to server!");

// Select database
$db = mysql_select_db($database, $connection) or die("Can't select database!");

// Build the query
$query = "SELECT id, type FROM database WHERE id=$id AND type=$type";

// Query the server
$result = mysql_query($query);

// Code to process this information
?>
JHanson90




PostPosted: Tue Oct 19, 2004 8:18 pm   Post subject: (No subject)

There's no built in way to just directly let the URL query the database. If that were possible, then every website with a database would have been cracked into right now, and we would all be like AHHHHHHHHHH!

You need actual PHP scripting to handle this query, if that's not something you've already done.
code:
<?php ### this is PHP5 compatible code, by the way
if (isset($_GET['id'])) $id = $_GET['id']; // the isset() makes it PHP5 compatible
if (isset($_GET['type'])) $type = $_GET['type'];
# etc... the rest of the *if isset URL 'name', set variable to that*
$query = "
SELECT your_column1,your_column2,etc FROM table_name
        WHERE id=$id,
                  type=$type;
"; // Or however you're trying to query the db
mysql_connect (/* your connection arguments */);
$result = mysql_query ($query);
### The rest of the page, whatever you want to do with it
?>
The result would be, after you fill in all the necessary information that I left out, that you might type in http://www.yourdomain.com/index.php?id=x&type=y, and that would get a query to the database in any row that the id is x and the type y.
wtd




PostPosted: Tue Oct 19, 2004 8:39 pm   Post subject: (No subject)

In simpler terms...the URL is just a browser's way of sending information to a program running on a webserver. That program then has to deal with the database and build appropriate output. The webserver sends that output to the browser.

Browser <---> Server <---> Database
JHanson90




PostPosted: Tue Oct 19, 2004 9:39 pm   Post subject: (No subject)

cartoon_shark wrote:
Basically what JHanson said under me, only for a bit easier to read and understand I hope.
yeah I'm lazy, plus I haven't coded web database apps in a while since my comp crashed.

And also, I got an IM from my friend, who reminded me to add security to this -.-

Use some functions to clean up the code, like trim() and stuff for the query, as he says.... and some other thing that was, I dunno, misspelled, he said "alwasy use intval around ids." <-- Whatever he said.


Personally, I make functions in a different file to use for connecting to the database and querying and stuff. I put it all somewhere else so that I never have to remember it. Also, the database can easily be changed, e.g.
code:
<?php
function dbconnect () {
        // I forget all that I did in here, but it was something like..
        if (! mysql_connect ("localhost", "root", "password")) {
                die ("Couldn't connect: " . mysql_error());
        }
        if (! mysql_select_db ("whatever")) {
                die ("oh poo: " . mysql_error());
        }
}
?>
So then if I suddenly felt like using a different *cough*better*cough* database, I could just change that single function to:
code:
<?php
function dbconnect () {
        if (! pg_connect("host=localhost dbname=whatever user=root password=password")) {
                die("crap: " . pg_last_error());
        }
}
?>
And then, when I wanna connect, I just say dbconnect() somewhere in my code (so long as the file with the function definitions is included). The point is though, that you would add some security things to any query function you make.
JHanson90




PostPosted: Tue Oct 19, 2004 9:41 pm   Post subject: (No subject)

wtd wrote:
In simpler terms...the URL is just a browser's way of sending information to a program running on a webserver. That program then has to deal with the database and build appropriate output. The webserver sends that output to the browser.

Browser <---> Server <---> Database
To be formal, which I love doing, that's called three tier architecture, used with web database applications. I feel smart now! Very Happy
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: