need some php help - code

They have: 22 posts

Joined: Oct 2001

ok here is a code bellow which I am using to view news
this is everything which I have figured ou tthat is going wrong

<?php
http
://157.238.47.212/admin/viewnews.php

Warning: Supplied argument is not a valid MySQL result resource
in
/home2/samcoleb/public_html/admin/viewnews.php on line 9


include(\"common.php\");
while(
$n = mysql_fetch_array($result)) {

$id = $n[\"id\"];
$poster = $n[\"poster\"];
$time = $n[\"time\"];
$message = $n[\"message\"];

echo \"<tr><td bgcolor=\\"
#DFDFDF\\" width=\\"100%\\" align=\\"center\\">\";
echo \"<b>\".$headline.\"</b> posted by \".$poster.\" on \".5:33pm.\"</td></tr>\";
echo \"<tr><td bgcolor=\\"
#FFFFFF\\" width=\\"100%\\" align=\\"left\\">\".$message.\"</td></tr>\";
mysql_close($db);

?>

I have been told that the $result means nothign and that it needs to be set by an actual query before I can use it.
My question is what the heck does that mean and can someone please share with me the code and explain what it does?

thanks

==========::::::::===========
=====::: Sam Colebatch :::=====
==========::::::::===========

Busy's picture

He has: 6,151 posts

Joined: May 2001

$result is usually the variable called, $result could be the table in your database which is set to $n, which is then broken down into id, time, poster etc

is that code complete? looks like its missing a closing bracket "}"

the others may be able to shed more light on this, as i'm no expert in php yet

They have: 22 posts

Joined: Oct 2001

Quote: Originally posted by Busy
$result is usually the variable called, $result could be the table in your database which is set to $n, which is then broken down into id, time, poster etc

is that code complete? looks like its missing a closing bracket "}"

the others may be able to shed my light on this, as i'm no expert in php yet

hmm lol you mind explaining that in english a bit more? Smiling

sorry this is the complete code -

<?php
<html>
<
head>
<
title>Display News</title>
</
head>
<
body>
<
table border=\"1\" bgcolor=\"#FFFFFF\" width=\"50%\" cellpadding=\"2\" cellspacing=\"1\">

include(\"common.php\");
while(
$n = mysql_fetch_array($result)) {

$id = $n[\"id\"];
$poster = $n[\"poster\"];
$time = $n[\"time\"];
$message = $n[\"message\"];

echo \"<tr><td bgcolor=\\"
#DFDFDF\\" width=\\"100%\\" align=\\"center\\">\";
echo \"<b>\".$headline.\"</b> posted by \".$poster.\" on \".$time.\"</td></tr>\";
echo \"<tr><td bgcolor=\\"
#FFFFFF\\" width=\\"100%\\" align=\\"left\\">\".$message.\"</td></tr>\";
mysql_close($db);


</
table>
</
body>
</
html>

}
?>

My next question is upon what you said how would I go about fixing that code?
So it will then display the news properly?

thanks for the reply

==========::::::::===========
=====::: Sam Colebatch :::=====
==========::::::::===========

They have: 22 posts

Joined: Oct 2001

edit - sorry the common file has the

<?php
$db connection
?>
init so it is easier to update the connection if I have to move hosts or databases again

thanks

==========::::::::===========
=====::: Sam Colebatch :::=====
==========::::::::===========

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

Quote: Originally posted by killmaster

I have been told that the $result means nothign and that it needs to be set by an actual query before I can use it.
My question is what the heck does that mean and can someone please share with me the code and explain what it does?

$result is just a variable that can be anything. What is in common.php? A query would be just a SELECT fieldname from db_table

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

They have: 22 posts

Joined: Oct 2001

this is the code in the common.php file

<?php
$db
= mysql_connect(\"localhost\",\"samcoleb_kill\",\" *** \");
mysql_select_db(\"samcoleb_news\",
$db);
?>

this is the database connection code in which then I will only have to change one file instead of all if it needs to be updated

==========::::::::===========
=====::: Sam Colebatch :::=====
==========::::::::===========

They have: 22 posts

Joined: Oct 2001

ok here is another script I have decided to create -

http://157.238.47.212/mynews.zip

Except I was wondering as I don't know what the heck is going on I was wondering if someone would be able to debug the code?

Make it work just to post and view news.
Something is also going on when I try and create the table as well but I don't know what

thanks alot for all your help

==========::::::::===========
=====::: Sam Colebatch :::=====
==========::::::::===========

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

Well the first thing that I would do is fix your database. Here is your code:

<?php
$create
= \"CREATE TABLE messages( id (int, value: 11, default: 1, auto_increment, primary)
poster (text)
headline (text)
time(text)
message (longtext)
?>

Several things that I would do. One is to make the primary field, message_id instead of id. This way if you join tables, it avoids same column names. Then you really don't need a default value for the id. It will start at 1 and go up from there. I would make the headline field, probably a varchar of fixed length to prevent a too long headline or wasted space. Time doesn't need to be text either. You could make it an integer, insert the value as a Unix timestamp get you values that way. Poster should also be varchar(30-50).

I didn't really get to look at the other code. What kind of problems are you having?

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

They have: 22 posts

Joined: Oct 2001

hmm thanks for that I will fix the add database code of it
as for the problems I will get back to you on that one gtg be back tommorrow

thanks

They have: 33 posts

Joined: Apr 2002

Hey killmaster,

I looked at common.php and your display news page. Looks like your missing two lines of php code.

Try this in your display news code.

include("common.php");
$sql = "Select * From messages Limit 0,5";
$result = mysql_query($sql) OR die(mysql_error());
while($n = mysql_fetch_array($result)) {

That should select your last 5 inserted messages. If you want more just make the 5 into something bigger.

http://www.cadeh.com - biz (or lack of)
http://me.cadeh.com - wanna see how dorky I am?

They have: 22 posts

Joined: Oct 2001

giv eme a sec and I will let you know

They have: 22 posts

Joined: Oct 2001

Quote: Originally posted by mairving
Well the first thing that I would do is fix your database. Here is your code:

<?php
$create
= \"CREATE TABLE messages( id (int, value: 11, default: 1, auto_increment, primary)
poster (text)
headline (text)
time(text)
message (longtext)
?>

Doesn't work this is wthe whole code im using...

<?php
$db
= mysql_pconnect(\"localhost\",\"samcoleb_kill\",\"trustnoone\");
mysql_select_db(\"samcoleb_news\",
$db);
$create = \"CREATE TABLE messages( id (int, value: 11, default: 1, auto_increment, primary)
poster (text)
headline (text)
time(text)
message (longtext)

;\";
mysql_query(
$create);

?>

this is what I get when I try and view the news
[b]
Table 'samcoleb_news.messages' doesn't exist

http://157.238.47.212/admin/displaynews.php

==========::::::::===========
=====::: Sam Colebatch :::=====
==========::::::::===========

They have: 33 posts

Joined: Apr 2002

If the create table sql isn't working then there obviously isn't going to be a messages table, so thats why the second part won't work. The create table sql you have seems to be missing a parenthesis. You need one more parenthesis at the end to close the one that started after messages(. So the end should look like:

message (longtext))

http://www.cadeh.com - biz (or lack of)
http://me.cadeh.com - wanna see how dorky I am?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

That doesn't look like a correct SQL statement.

mySQL Docs:
6.5.3 CREATE TABLE Syntax

Try this:
CREATE TABLE messages(id int(11) DEFAULT 1 AUTO_INCREMENT PRIMARY KEY, poster TEXT, headline TEXT, time TEXT, message LONGTEXT)

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 22 posts

Joined: Oct 2001

thanks im busy tonight so I can't try that but I have thursday, friday and the weekend of to try everything out then.

Will let you know hoiw it works out

thanks

==========::::::::===========
=====::: Sam Colebatch :::=====
==========::::::::===========

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.