Jump to content

using PHP to display a specific field from a MySQL database


sydewyndr

Recommended Posts

I am very new to PHP and MySQL, and here is my problem.  Should be pretty simple for experienced programmers.

 

I need to create a link on a website, and I don't want to hardcode the text.  The link itself will take the user to another website.  What the link needs to say in the browser, is actually the text that is contained in a specific field, of a specific table, of a specific database.  There will be multiple records ... more being added all the time, so it needs to display only the field's text of the newest record. 

 

database = CTW_BM_db

table = wp_posts

field = post_title

 

There is also a field called post_date, to use in order to get the newest record.

 

I have looked online and in a PHP book for help, but I have not been successful yet.  Can somebody please help me?

Link to comment
Share on other sites

<?

$host='host'; 

$username='user';

$pass='pass';

$database='CTW_BM_db';

 

mysql_connect ($host, $username, $pass);

 

mysql_select_db($database) or die('Could not connect to the Database');

 

$info = mysql_query("SELECT post_title FROM wp_posts");

while ($r = mysql_fetch_array($info)) {

echo "<a href=the link>$r[post_title]</a>"; }

?>

Link to comment
Share on other sites

<?
$host='host';  
$username='user'; 
$pass='pass'; 
$database='CTW_BM_db'; 

mysql_connect ($host, $username, $pass); 

mysql_select_db($database) or die('Could not connect to the Database');

$info = mysql_query("SELECT post_title FROM wp_posts");
while ($r = mysql_fetch_array($info)) {
echo "<a href=the link>$r[post_title]</a>"; }
?>

 

stupid [/url] thingy in that 1

Link to comment
Share on other sites

That worked pretty well, but it's giving me some more problems.

 

First of all, it's listing all of the post_title's, instead of just the newest ones.

 

Second, it's giving me this error message at the top of my screen:

Table 'CTW_BM_db.client_module_customization' doesn't exist

 

SELECT search, `replace` FROM client_module_customization WHERE client_module=50 AND active=1

 

And lastly, it's giving me these error messages at the bottom of my screen, after the results:

 

Table 'CTW_BM_db.page_view' doesn't exist

 

SELECT ID FROM page_view WHERE month='2007-02' AND file_name='index' AND publication=5 AND client=1 AND source='module'

 

Table 'CTW_BM_db.page_view' doesn't exist

 

INSERT INTO page_view (month, file_name, publication, client, source, count) VALUES ('2007-02', 'index', 5, 1, 'module', 1)

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.