Solved

MySQL and PHP question....

Posted on 2002-04-11
15
186 Views
Last Modified: 2008-02-20
I am a complete novice at MySQl, and really all I want to do is:

-> Connect to the db server.
-> Select a db.
-> Select a message to print to the user based on an id supplied to the script via the query string.
ie /script.php?id=1 .

I have set up a MySQL table called 'txts', and this table has three fields: 'id', 'heading', and 'message'.


Firstly, I want to display a page which lists all the messages available, in a table like this:

-------------------------------------
| Message Heading (field 'heading') |
-------------------------------------
| The actual message |
| (field 'message') |
-------------------------------------
| A link to another page giving the |
| message id via the query string |
| (using the 'id' field) |
-------------------------------------

Then, on the next page, I would like to be able to put the message into a variable ($message) which
I can use later on.... the message will be selected from the table using the ?id= parameter to get the
right message.

I know that this is a lot to ask, but can anyone give advice on how to do this. I will be extremely
grateful for any help at all.

Thanks,
Nick!
0
Comment
Question by:Nick500000
  • 9
  • 6
15 Comments
 
LVL 4

Expert Comment

by:lokeshv
ID: 6934628
connect to db server

$conn=mysql_pconnect("host","user","password");

mysql_select_db("database_name");

$query="selct ur msg here where msg_id=$id";

$qid=mysql_query($query);



just a small learifiactions..

if u post here what excatly want to do and ur code..i will post the code here..

Hope this help

Lk :o)
0
 
LVL 4

Accepted Solution

by:
lokeshv earned 205 total points
ID: 6934664
i can got that u want to display all the messages on first page...but what on next page ?


for first page use this ..


<?
$conn=mysql_pconnect("urserver","username","password");
if(!$conn){
        echo"cant connect";
        die;
}

mysql_select_db("database_name");

$qid=mysql_query("select * from txts");

while($row=mysql_fetch_array($qid);$i++){
?>
<html>
<table>
<tr><td><?=$row[heading]?></td></tr>
<tr><td><?=$row[message]?></td></tr>
<tr><td><a href="your_file.php?message_id=<?=$row[id]?>">Your Link</a></td></tr>
</table>

<br><br>
<?}?>
</html>
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6934667
hope this helps..


Lk:)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Nick500000
ID: 6934754
Thanks for your help on the first page... i'll get to work on that straight away.

For the second page, I have already found a script for that, but I need to add some more code which will simply place the text from the mysql field 'message' into a variable (possibly called 'message'!). But the message must be selected according to the 'id', Which will have been passed by the link scripted into your example above...

So really I want.

$message = AND THEN THE MYSQL SYNTAX HERE.....

Thanks,
Nick!
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6934795
ok here is ur PHP code for second page

<?
$id=$message_id; //this is got from first page

$query="select message from txts where id=$id";

$qid=mysql_query($query);

if(!$qid){
       echo 'Error:'.mysql_error();
}

$row=mysql_fetch_array($qid);

//your varaible

$message=$row[message];

?>


hope this will solve ur problem..

Lk :o)
0
 

Author Comment

by:Nick500000
ID: 6934824
O.K, thanks alot... I try that now.
0
 

Author Comment

by:Nick500000
ID: 6934963
I am sure that there is nothing wrong with your code, but all I get is a blank out put from the query.... even though I have set up some test values.... I have checked this using phpmyadmin. Maybe there is some thing wrong with my set up. Do I need some Unique key/index? Here is the structure of my table:

#
# Table structure for table txts
#

CREATE TABLE txts (
   id varchar(5) NOT NULL auto_increment,
   heading mediumtext NOT NULL,
   message longtext NOT NULL,
   cat text NOT NULL,
   UNIQUE id (id)
);

Is there something wrong with this?

any ideas would be helpful.

Thanks,
Nick!
0
 

Author Comment

by:Nick500000
ID: 6935009
Well,

it's not strictly blank, all I get is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY></BODY></HTML>

So the conditions of the while loop are being met straigth away, resulting in it not running?

0
 

Author Comment

by:Nick500000
ID: 6935036
And the second script just says, cant connect. Maybe something up with my host!
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6935856
first page ..

replace while($row=mysql_fetch_array($qid);$i++)


from

while($row=mysql_fetch_array($qid))

and check you have any data in the tbale or not ?


second page...

add on top


$conn=mysql_pconnect("urserver","username","password");
if(!$conn){
       echo"cant connect";
       die;
}



if this still doesnt solve ur prblm ...post ur code here ..

Lk
0
 

Author Comment

by:Nick500000
ID: 6937052
I am getting 'can't connect' on all the scripts now. I am sure that this something wrong with my host. I'll try on another host.... If that doesn't work i'll post the code!
0
 

Author Comment

by:Nick500000
ID: 6937072
Hey,

I don't know whether this is relevant, but a script I found on php.net, has this as the connect string:

$conn = mysql_connect("localhost", "username", "password");

but your script has:

$conn = mysql_pconnect("localhost","username","password");

Notice the extra 'p'.... is this what may be causing the 'can't connect'?

Nick!

0
 

Author Comment

by:Nick500000
ID: 6937092
Yes, that's what was causing it.

I removed the 'p', and the script works perfectly.... I emailed support, and they told me that using pconnect was specifically banned on their servers. So not your fault at all!

Thanks so much.

Here's your points....

-----
Nick!
-----
0
 

Author Comment

by:Nick500000
ID: 6937101
Thanks again!

Nick!!!
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6938017
Anytime...

thanks for points:o)

Lk
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question