Jump to content

Warning: mysql_fetch_array() - Can't find error


JamesB

Recommended Posts

hey, im currently working on a basic forum in php/mysql and cant figure out why i keep getting this error.The error is:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\forum\reply.php on line 47Line 47 in reply.php:

$result2 = mysql_fetch_array($result2);

reply.php:

<?phprequire 'settings.php';if ((isset($_COOKIE['ForumUsername'])) && (isset($_COOKIE['ForumPassword']))) {	$username = $_COOKIE['ForumUsername'];	$password = $_COOKIE['ForumPassword'];	if (($username != "") && ($password != "")) {	  $username = mysql_real_escape_string($username, $con);	  $password = mysql_real_escape_string($password, $con);	  	  $query = "SELECT * FROM Accounts WHERE Username='$username' && Password='$password'";	  $result = mysql_query($query, $con);	  	  if (mysql_num_rows($result) >= 1) {		$loggedin = 1;	  }	  else {		die();	  }	}	else {		die();	}}else {	die();}if ((isset($_POST['ReplySubmit'])) && (isset($_POST['ReplyText'])) && (isset($_POST['TopicID']))) {	//Store data sent to page in variables	$replyText = $_POST['ReplyText'];	$topicID = $_POST['TopicID'];		//Fetch info about the topic and store info in variables	$query = "SELECT * FROM Topics WHERE TopicID='$topicID'";	$result = mysql_query($query, $con);	$result = mysql_fetch_array($result);		$categoryID = $result[0];	$sectionID = $result[1];	$topicName = $result[3];		//Retrieve the next post ID		$query2 = "SELECT * FROM settings WHERE Option='PostLatestID'";	$result2 = mysql_query($query2, $con);	$result2 = mysql_fetch_array($result2);	$postLatestID = $result2[1];		//Insert post data into MySQL	$query3 = "INSERT INTO Posts (CategoryID, SectionID, TopicID, PostID, Username, Post) VALUES ('$categoryID', '$sectionID', '$topicID', '$postID', '$username', '$replyText')";	mysql_query($query3, $con);		//Set the next post ID + 1, and store it in MySQL	$postLatestID++;	$query4 = "INSERT INTO settings (PostLatestID) VALUES ('$postLatestID')";	mysql_query($query4, $con);		//header ("Location: index.php?display=topic&topic=$topicID");}?>

basically 3 $_POST vars are sent to reply.php and stored in vars,then it fetches info about the topic and stores that in vars too.then the problem occurs, trying to retrieve the next post ID (each post will have a unique id)I cant understand why it cant retrieve this value fromm MySQL.the MySQL table called settings has 2 columns, Option and Value both setup with varchar(20):Option ValuePostLatestID 2Any help is really appreciated!thanksoh and heres the settings.php file:

<?php$mysqlHost = "127.0.0.1";$mysqlUsername = "root";$mysqlPassword = "";$mysqlDatabase = "forum";$con = mysql_connect($mysqlHost, $mysqlUsername, $mysqlPassword);$db = mysql_select_db($mysqlDatabase, $con);if (!$con) {	die ("Unable to connect to MySQL.");}if (!$db) {	die ("Unable to select the database in MySQL.");}?>

edit:Maybe it's because I can't use WHERE with SELECT in the MySQL query?edit2:Just saw WHERE used with SELECT on the w3schools tutorial, so that can't be the problem.

Link to comment
Share on other sites

It's obviously something in the SQL query:SELECT * FROM settings WHERE Option='PostLatestID'The first question:1. Is there a table called "settings" in your database?And then:2. Is there a field called "Option"?If there is:3. Maybe the uppercase O is the probem.And if that's not the problem, than it might be that "Option" is a numerical field and only accepts a number as a value (a number doesn't go between quotes)

Link to comment
Share on other sites

It's obviously something in the SQL query:SELECT * FROM settings WHERE Option='PostLatestID'The first question:1. Is there a table called "settings" in your database?And then:2. Is there a field called "Option"?If there is:3. Maybe the uppercase O is the probem.And if that's not the problem, than it might be that "Option" is a numerical field and only accepts a number as a value (a number doesn't go between quotes)
1. Yep.2. Yep.3. I just tried changing 'Option' to 'option' as the MySQL field name and in the reply.php script, I got the exact same error.The 'option' field is setup as 'varchar'.
Link to comment
Share on other sites

Don't guess what the problem is, just print out the error message.

$result = mysql_query($query, $con);if (!$result)  exit(mysql_error());

You can also print the query out and run it in something like phpMyAdmin to see the error. It might be that option is a reserved word, I'm not sure if it is or not.

Link to comment
Share on other sites

Don't guess what the problem is, just print out the error message.
$result = mysql_query($query, $con);if (!$result)  exit(mysql_error());

You can also print the query out and run it in something like phpMyAdmin to see the error. It might be that option is a reserved word, I'm not sure if it is or not.

The mysql error echod:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option='PostLatestID'' at line 1I changed the field name from 'option' to 'abc' in MySQL and the script, works perfecly, no errors at all.Thanks for your help guys.
Link to comment
Share on other sites

One thing you need to watch for is words that might be preselected names for something. I have had this problem a lot. If you keep your field names unique but still relevant to what their holding, you'll be fine.

Link to comment
Share on other sites

You could also have used `backquotes` to indicate that the word was a field name and not a keyword.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...