Solved

MySQL and PHP question....

Posted on 2002-04-11
15
183 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now