Webmaster Key - Discussion Forums


Welcome, Guest. Please login or register.
Did you miss your activation email?
February 09, 2012, 06:36:31 PM

Login with username, password and session length
Welceome to Forums!

Important information for guests and new members:

In order to understand the full benefits of becoming an active member of this forum, please review the following information on guest and new member restrictions. These forum changes have been prompted by an overwhelming and unreasonable amount of bot postings and incoherent guest spam messages. We wish to prevent these events from happening in the future and make our community a more comfortable place for all of our members.

For guests:

Guests are not allowed to open new topics, polls, or posts attachments.
If you wish to open up new discussions on this forum, we encourage you to register.

For new members:

New members with less than five posts are not allowed to modify additional profile information such as avatars, contact information, biographies, and signatures. However, new members are encouraged to post their own topics or reply to topics initiated by other members. Become active on the forums and 5 posts should be an easy task!

We are a diverse community with members from all over the world. We encourage new ideas and interesting conversation. Do not be afraid to post webmaster/computer-related questions or problems, as our active members are always willing to help when they are able. Interested? Join us.

+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ Coding Talk
| | | |-+ call data from database in php
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print
Author Topic: call data from database in php  (Read 3133 times)
stu
Key Keeper
Member
**
Posts: 83



WWW
« on: June 27, 2008, 03:29:47 PM »

Hi

i have created a database and want to call data from the table table layout below

+-----------------+-------+-------+
| panname          | stock   | price |
+-----------------+-------+-------+
| coppercasserole |    20   |  2199 |
| coppersaucepan |    10  |  1988 |
| coppersaute      |    20   |  1299 |
| steelcasserole   |     8    |  2099 |
| steelfrying        |    22    |   899 |
+-----------------+----- --+-------+

i want to be able to call the price of say the coppercasserole pan price and i want it coverted from pence into pounds. I just carnt work out how to do this. i am new to database and these scripts. here is what i have so far The database table is called pans

i can connect to the database fine its the scripts to pick that exact table and the price for that exact pan.


i have put this in the head section of the php document

<?php

/*
 * ========================================================================
 *                       connect_db
 * ========================================================================
*/
function connect_db($localhost, $database id, $password) {
   $connection = @mysql_connect($localhost, $database id, $password)
      or die('DB connection error: ' . mysql_error());
   mysql_select_db($database id, $connection)
      or die('DB selection error: ' . mysql_error());
   
   return $connection;
}
php $selectedTable=$_GET["pans"]
   
?>

the above script which connects to the database should pick which database to connect to and pick which table to connect to in the database.

now i need to call the pan price

this would be in the body of the php document were i need it to display. I just dont know were to start or finish with this. the reading i'm doing to find this out is just sending me around in complete circuls.

<p>Copper Casserole Pan<br />
Capacity 4 Litres</p><?php echo $price; ?>


any help or pointers would be appreciated.

many thanks

stuart



Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #1 on: June 28, 2008, 05:26:02 AM »

Try this:
Code:
<?php

$sql 
"SELECT * FROM pans WHERE panname = '$pan_name'";
$result mysql_query ($sql);
if (
mysql_num_rows($result) == 1) {
  
$row mysql_fetch_assoc($result
  
$price $row['price'];
  echo 
"Price: $price";
} else echo 
"Item not found!";
?>

Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #2 on: June 28, 2008, 01:17:10 PM »

Hello Stu,

the parse error is where you have missed a semi colon off a line or the quotes don't match up etc.

Where you see the error: Parse error: syntax error, unexpected T_VARIABLE in ...

It should show you the bit of code where the syntax is not correct. It's usually a simple typo error. But you left out the part that shows the line number etc.

Also, please use the code highlighting feature of this forum and add <?php before your PHP code when you post and ?> at the end so it shows up with syntax highlighting to make it easier to visualize on the forum i.e. with color and indents etc.

If you use NotePad++ it is a nice editor that does this for you as you write code.
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #3 on: June 28, 2008, 02:40:14 PM »

Code:
<?php

// Connect to the database and select the correct database.
   
???

$sql "SELECT * FROM pans WHERE panname = '$coppercasserole'";
$result mysql_query ($sql);
if (
mysql_num_rows($result) == 1) {
  
$row mysql_fetch_assoc($result)
  
$price $row['price'];
  echo 
"Price: $price";
} else echo 
"Item not found!";

?>



Stu, I just highlighted the code in this reply. Can you spot the place where there is a semi colon missing at the end of a line of code?

If you use Notepad++ you can easily find the line number where the error is.

To highlight your code in posts you need to use the # symbol icon above the smilies  Smiley And Preview your posts before you Save them.
« Last Edit: June 28, 2008, 02:58:09 PM by Andy » Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #4 on: June 28, 2008, 02:46:25 PM »

Hi

yes spotted it straight away. It now reads the database and doesnt come back with any errors the page displays item not found which is the 2nd echo so its not finding the row price in the database. sorry i no your busy and i should no this as its probably just a very simple mistake.

This is the first time i have done databases

thanks

stuart
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #5 on: June 28, 2008, 02:51:17 PM »

Great, now you are finding your feet with database stuff.  Grin All the best!

Debugging database stuff can be confusing, I know. But usually problems are due to incorrect syntax with the query or no rows of data being found.
Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #6 on: June 28, 2008, 06:51:11 PM »

Thank your for your help

stuart
Report to moderator   Logged

Pages: [1] Go Up Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print 
+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ Coding Talk
| | | |-+ call data from database in php

Jump to:  
« previous next »


Our Partners
RelmaxTOP Ranking System Web Hosting RelmaxTOP Ranking System
Staff Sites
12Noon[12Noon Gallery] Andy[Urgentclick]
Tamuril[Tamuril's Digital Art Exhibit] Sensovision
Powered by MySQL Powered by PHP We are hosted by Relmax Inc. |Our Privacy Policy | Sitemap
Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC
Forum design by Tamuril © 2005.
Valid XHTML 1.0! Valid CSS!