Link to home
Start Free TrialLog in
Avatar of Nick500000
Nick500000

asked on

MySQL and PHP question....

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!
Avatar of lokeshv
lokeshv

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)
ASKER CERTIFIED SOLUTION
Avatar of lokeshv
lokeshv

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hope this helps..


Lk:)
Avatar of Nick500000

ASKER

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!
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)
O.K, thanks alot... I try that now.
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!
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?

And the second script just says, cant connect. Maybe something up with my host!
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
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!
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!

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!
-----
Thanks again!

Nick!!!
Anytime...

thanks for points:o)

Lk