Forum Moderators: coopster
I am having trouble as i am new to php. I have connected to the database and I have displayed the fields i want on my page. I need to access certain fields by links to each letter.
For example A will link to the products starting with A and so on.
I am using PHP ad MySQL. The database is however on a different server. I only have the .php file at the moment and want to display the data on a HTML page.
I am just unsure of how to make a link to the product field and the price field and display them within a table.
thanks in advance
mySQL was designed from the ground up to function as a remote database server. Often this is not the case, the mySQL server and the web server are the same machine, in which case it will default to "localhost." If the server is remote, you connect to it via the server name.
Local:
$link = mysql_pconnect('localhost','mysql_user',mysql_pasword') or die ("Could not connect server");
mysql_select_db(DB,$link);
remote:
$link = mysql_pconnect('mysql.your-host-server.com','mysql_user',mysql_pasword') or die ("Could not connect server");
mysql_select_db(DB,$link);
This establishes the connection to the database.
Documentation [us3.php.net]
You don't directly link to a database field, if you did, this could create major security issues. You use the database manipulation methods inherent in PHP to interface with mySQL, using standard mySQL statements. A basic (and insecure, as posted) method is to accept input from the browser and convert that to a select statement.
<a href="your-script.php?article=1234">See article 1234</a>
$article = $_GET['article'];
You can use $_GET, $_POST, or $_REQUEST to collect input values, but don't ignore my insecure comment - unfiltered input data is the cause of most hacked sites, topic for another thread. Once you get this working, search Google for mysql injection PHP site:webmasterworld.com for threads discussing this. (also cross site scripting PHP site:webmasterworld.com or XSS PHP site:webmasterworld.com)
You would then use your cleansed input an create a select statement to query the database:
$query = 'select * from articles_table where art_id=' . $article;
$result=@mysql_query($query);
mySQL Select Statement Index [dev.mysql.com]
mySQL Query Documentation [us3.php.net]
You access the values in "$result" by their array index,
$row=mysql_fetch_array($result);
echo "$row[0] $row[1] $row[2]"; // first three elements in table row
or via an associative array. An associative array is a key/value pair, and the keys are the database table fields. Both are available to the mysql_fetch_array() function.
echo $row['id'] . " " . $row['first_name'] . " " . $row['last_name'];
Documentation [us3.php.net]
I need to access certain fields by links to each letter.
Using the above info, there are a variety of ways to do this, the simplest being to use the mySQL LIKE operator.
$query = "select * from table where title like 'A%'";
The % means "any character" so this means "starts with A and followed by any character." Note that searches on text fields require quoting, integer fields do not.
Should get you rolling . . .
In another file (as an include,) in one file, whatever works best for you, it's a matter of ease of management and coding style.
You can output anything you want with PHP or any other language, there are some methods and classes specifically for managing well formed XML.