Page 1 of 2
Error problems in PHP
Posted: Mon Dec 09, 2002 4:44 pm
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!!!
Posted: Mon Dec 09, 2002 5:04 pm
by infolock
Volka answered my statement.
Posted: Mon Dec 09, 2002 5:09 pm
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

Posted: Mon Dec 09, 2002 5:13 pm
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.
Posted: Mon Dec 09, 2002 5:18 pm
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
Posted: Mon Dec 09, 2002 5:25 pm
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
Posted: Mon Dec 09, 2002 5:28 pm
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.
Posted: Mon Dec 09, 2002 5:29 pm
by infolock
well, in that case, more info needed. post your code where you are querying the data...
Posted: Mon Dec 09, 2002 5:34 pm
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

Posted: Mon Dec 09, 2002 5:35 pm
by kkurkowski
What do you mean by querying???
Posted: Mon Dec 09, 2002 5:36 pm
by volka
mysql_query: send a query to the mysql-server

Posted: Mon Dec 09, 2002 5:40 pm
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.....
Posted: Mon Dec 09, 2002 5:41 pm
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")
{
if ((!$news_headline) || (!$news_news) || (!$news_link)) {
echo "Please fill in all fields<br>\n";
} else {
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";
}}
?>
Posted: Mon Dec 09, 2002 5:51 pm
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.
Posted: Mon Dec 09, 2002 5:58 pm
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))
{
?>
add_news.php
Code: Select all
<?
require("news_connect.php");
if(@$action=="submit")
{
if ((!$news_headline) || (!$news_news) || (!$news_link)) {
echo "Please fill in all fields<br>\n";
} else {
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";
}}
?>
Thats all there is for the news script. The username one has more code.