Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Calling items from a database?
Old 01-18-2005, 10:46 AM Calling items from a database?
Novice Talker

Posts: 12
Trades: 0
Hi

I am new to PHP & MySQL and have been working on learning how to set up a database. Well yesterday I set up a test database (for purposes of this discussion I named it Jokes as was the example in my book) so I named the database Jokes and then created a table called Jokes as well.

Oh before we get to far i am using PHPMyAdmin to create the database and tables.

OK so I created my table called Jokes and this table currently has 3 columns in it. That being

ID | Jokes Text | Jokes date

Now someone gave me a tip that I should connect the words with an underscore IE Jokes_Text Just so you know in case you spot this you'll know I am aware of this already. So anyways I created the table and all seems to be going fine.

I then asked how I add items and he said just to click on the insert tab in PHPMyAdmin so this is easy to do.

OK now I have items added to the database how can I call these up from my webpage.

Example lets say I have 3 jokes in the database.

I then have 3 links on my page to call up items in the database how do I call up a specific joke if the link is pressed IE

link 1 goes to Joke 1
Link 2 goes to joke 3
Link 3 goes to Joke 2

Not a typo listed numbers that way on purpose but how can I set it up to call up items when a link is pressed?

Also in a databse when I am inserting items can I have HTML code as the item? Like if I want to have a list of amazon.com books can I add HTML to the database instead of just plain text that way the links then call up the HTML and then this gets converted to the iframe window on my website when a user calls for an item from the database?

TIA
__________________
Dave

Please login or register to view this content. Registration is FREE
Scottish2 is offline
Reply With Quote
View Public Profile Visit Scottish2's homepage!
 
 
Register now for full access!
Old 01-18-2005, 11:19 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
The way I would do the display of the jokes, is to write a page called say, 'display.php', then you create links in this format:

link 1 goes to Joke 1
Link 2 goes to joke 3
Link 3 goes to Joke 2

becomes

<a href="display.php?joke=1>Link 1</a>
<a href="display.php?joke=3>Link 2</a>
<a href="display.php?joke=2>Link 3</a>

Then in display.php, you pull out the joke and display it:

PHP Code:
// Make database connection

$jokeid $_GET['joke'];

$result mysql_query("SELECT 'Jokes Text' FROM Jokes WHERE ID=$jokeid");
$text mysql_result($result,0,'Jokes Text');

echo 
$text
To use html, you can just put it into your database, although you must be careful with quotes. To make sure they behave properly use addslashes() on the string before inserting it into the db. Also be wary that if you are accepting user input (if you ever have a form where they can input their own jokes ofr example) then if you allow html you leave yourself wide open to all sorts of hijack attacks. Without being careful, people can insert javascript into your database that gets executed when the joke is displayed.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 01-18-2005, 11:42 AM
Novice Talker

Posts: 12
Trades: 0
OK maybe I set up the database wrong but here is what I get with the code you suggested.

http://www.scottish2.com/test/test/test.html

Thoughts?
__________________
Dave

Please login or register to view this content. Registration is FREE
Scottish2 is offline
Reply With Quote
View Public Profile Visit Scottish2's homepage!
 
Old 01-18-2005, 11:53 AM
stephan.gerlach's Avatar
Novice Talker

Posts: 12
Trades: 0
change it to

PHP Code:
<?php

// Make database connection     
$jokeid $_GET['joke'];     

$result mysql_query("SELECT 'Jokes Text' FROM Jokes WHERE ID=$jokeid");   
$text mysql_result($result,0,'Jokes Text');     

echo 
$test
?>
hope that will work
stephan.gerlach is offline
Reply With Quote
View Public Profile
 
Old 01-18-2005, 11:59 AM
stephan.gerlach's Avatar
Novice Talker

Posts: 12
Trades: 0
Sorry. didnt look propperly. You still need a connection to the database.

replace the user and password and incase you host your mysql database on a different server than you php files then change your host too.

PHP Code:
<?php

$link 
mysql_connect('localhost''mysql_user''mysql_password');

if (!
$link) {
   die(
'Could not connect: ' mysql_error());
}


$jokeid $_GET['joke'];      

$result mysql_query("SELECT 'Jokes Text' FROM Jokes WHERE ID=$jokeid");    
$text mysql_result($result,0,'Jokes Text');      

echo 
$test

mysql_close
($link);

?>
stephan.gerlach is offline
Reply With Quote
View Public Profile
 
Old 01-18-2005, 12:23 PM
Novice Talker

Posts: 12
Trades: 0
Not sure I edited that right Lets use this example as obviously am not going to give out my info but Lets use the following

User Name: Web
Password: Master

Obviously not the actual log in but how should I edit this as I did get an error when I tried. Below is what I did is this right?

Code:
<?php 

$link = mysql_connect('localhost', 'mysql_web', 'mysql_master'); 

if (!$link) { 
   die('Could not connect: ' . mysql_error()); 
} 


$jokeid = $_GET['joke'];       

$result = mysql_query("SELECT 'Jokes Text' FROM Jokes WHERE ID=$jokeid");     
$text = mysql_result($result,0,'Jokes Text');       

echo $test 

mysql_close($link); 

?>
__________________
Dave

Please login or register to view this content. Registration is FREE
Scottish2 is offline
Reply With Quote
View Public Profile Visit Scottish2's homepage!
 
Old 01-18-2005, 04:13 PM
stephan.gerlach's Avatar
Novice Talker

Posts: 12
Trades: 0
no not quite

try it this way.


PHP Code:
<?php 

$link 
mysql_connect('localhost''web''master'); 

if (!
$link) { 
   die(
'Could not connect: ' mysql_error()); 



$jokeid $_GET['joke'];       

$result mysql_query("SELECT 'Jokes Text' FROM Jokes WHERE ID=$jokeid");     
$text mysql_result($result,0,'Jokes Text');       

echo 
$test 

mysql_close
($link); 

?>

if you go to http://www.php.net you can find almost everything what you need to know.
stephan.gerlach is offline
Reply With Quote
View Public Profile
 
Old 01-18-2005, 04:31 PM
Novice Talker

Posts: 12
Trades: 0
OK made the swap but am getting the following error

Code:
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/scottish/public_html/test/test/display.php on line 17
__________________
Dave

Please login or register to view this content. Registration is FREE
Scottish2 is offline
Reply With Quote
View Public Profile Visit Scottish2's homepage!
 
Old 01-18-2005, 04:39 PM
stephan.gerlach's Avatar
Novice Talker

Posts: 12
Trades: 0
ok do this one

PHP Code:
$result mysql_query("SELECT Jokes_Text FROM Jokes WHERE ID=$jokeid");      
$text mysql_result($result,0,'Jokes_Text'); 
stephan.gerlach is offline
Reply With Quote
View Public Profile
 
Old 01-18-2005, 04:46 PM
Novice Talker

Posts: 12
Trades: 0
Same basic thing

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/scottish/public_html/test/test/display.php on line 18
__________________
Dave

Please login or register to view this content. Registration is FREE
Scottish2 is offline
Reply With Quote
View Public Profile Visit Scottish2's homepage!
 
Old 01-18-2005, 05:12 PM
Novice Talker

Posts: 12
Trades: 0
Now quick question maybe this is database related but unsure. I noticed in my database list for my other SQL programs like chat and my forums that are set up automatically I notice each has a Perl and a PHP connection string listed yet for this new jokes database it does not have these lines. Is this maybe why? if so what have I missed?
__________________
Dave

Please login or register to view this content. Registration is FREE
Scottish2 is offline
Reply With Quote
View Public Profile Visit Scottish2's homepage!
 
Reply     « Reply to Calling items from a database?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.48518 seconds with 12 queries