Error problems in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Error problems in PHP

Post by kkurkowski »

Hello, I added a news script for people to signup and add news that thye want. Well, I am haveing problems viewing the news that is added into the tables of MySQL. This is the error.

Warning: Supplied argument is not a valid MySQL result resource in view_news.php on line 6

I tried so much stuff. I have played with the code and played around in MySQL. Does anyone know what is up with this. This is the code that, that error is from.

-------------------
<?
require ("news_connect.php");

$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news ORDER BY timestamp DESC LIMIT 5");

while ($data = mysql_fetch_array ($result))
{
?>
-------------------

Thanks if anyone can help!!!
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Volka answered my statement.
Last edited by infolock on Mon Dec 09, 2002 5:19 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

nope, he wants $data to be the result of mysql_fetch_array in each while-iteration. mysql_fetch_array will return FALSE if no more recordsets are available -> $data will be FALSE -> the loop discontinues.
probably the mysql_query failed an so $result is no result-resource that mysql_fetch_... can use to retrieve the data. Try

Code: Select all

$query = 'SELECT headline,news,link,user,timestamp FROM news ORDER BY timestamp DESC LIMIT 5';
$result = mysql_query ($query) or die(mysql_error());
to see what mysql thinks about the query ;)
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

I just tried that and it just sits on the internet explorer and loads and loads and then errors out my internet explorer.
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

I just tried that new code that you gave me and it just tries to connect to the wrong table.

Table 'kkurkowski.news' doesn't exist
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

by wrong table do you mean wrong database?
Table 'kkurkowski.news' doesn't exist
kkurkowski is the database, news the table (as you have ordered mysql to use in your query ;) ).
Take a look at http://www.php.net/manual/en/function.m ... ect-db.php
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

Ok, I got rid of the error, but it does not want to display what is suppose to do. I figured all that other stuff out. There is information in the MySQL database, but doesn't want to display it. I will keep on trying and see what goes on.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

well, in that case, more info needed. post your code where you are querying the data...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I would even assume that he had no select_db at all and php was configured to use kkurkowski (probably the account) as default database ;)
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

What do you mean by querying???
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

mysql_query: send a query to the mysql-server ;)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

K: just post your code in here ( be sure though, to edit out your login/password for your mysql database, as well as the correct path ).

We'll tear it apart, and tell ya what you need to do differently.


Again, make SURE to remove your correct username/password in the script for security reasons.....
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

This should be it. It is in the forms page when you fill out all of the text boxes and then hit submit. It sends it to the MySQL database.

Code: Select all

<?
                        require("news_connect.php");
						if(@$action=="submit")
                        &#123;
						if ((!$news_headline) || (!$news_news) || (!$news_link)) &#123;
	                    echo "Please fill in all fields<br>\n";
						&#125; else &#123;
                        mysql_query("INSERT INTO news_news (headline,news,link,user,timestamp) VALUES ('$news_headline','$news_news','$news_link','$user',".time().")");
                        echo "News added<br>\n";
                        &#125;&#125;
                        ?>
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

I have a user database and people can sign up in that. It works and people can login in after they signup. It is also MySQL. That works it is just it won't display and I can't figure out why not.
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

news_connect.php

Code: Select all

<?
$database_server="localhost";
$database_user="username";
$database_password="password";
$database="database";

$db=mysql_connect($database_server, $database_user, $database_password);
mysql_select_db($database);
?>
view_news.php

Code: Select all

<?
global $data;
require ("news_connect.php");
$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news_news ORDER BY timestamp DESC LIMIT 5");

while ($data == mysql_fetch_array ($result))
&#123;
?>
add_news.php

Code: Select all

<?
                        require("news_connect.php");
						if(@$action=="submit")
                        &#123;
						if ((!$news_headline) || (!$news_news) || (!$news_link)) &#123;
	                    echo "Please fill in all fields<br>\n";
						&#125; else &#123;
                        mysql_query("INSERT INTO news_news (headline,news,link,user,timestamp) VALUES ('$news_headline','$news_news','$news_link','$user',".time().")");
                        echo "News added<br>\n";
                        &#125;&#125;
                        ?>
Thats all there is for the news script. The username one has more code.
Post Reply