Solved

MySQL and PHP question....

Posted on 2002-04-11
15
188 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSRF session and form tokens never match when using php/AJAX 4 57
A responsive image gallery using flexbox 6 76
How can I split a variable 19 45
PHP processing webform 25 44
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

732 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