Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL and PHP question....

Posted on 2002-04-11
15
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 820 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

636 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