Jump to content

[SOLVED] mysql_num_rows(): supplied argument is not a valid MySQL result resource


DanielHardy

Recommended Posts

Hi,

 

I am gettng this error when trying to run a script that simply hanges the color of a checkbox when the user clicks the button. I know it is normally caused by missing ;'s or 's but I can't seethem in my script. Please Help?

 


<html>
<head>
<title>Regal Theatre - Online Ticket Booking System</title>
<style>
	* {
		font-size: 14px;
		font-family: arial;
	}
</style>
</head>
<body>

<center>
<br/>
<br/>
<br/>
<?php

if (isset($_POST['seats']))
{
	$user = $_COOKIE['ID_my_site'];

	$newStatusCode = $_POST['newStatusCode'];
	$oldStatusCode = $_POST['oldStatusCode'];

	// open database connection
	$linkID = @ mysql_connect("localhost", "0604274", "ah3482") or die("Could not connect to MySQL server");
	@ mysql_select_db("db0604274") or die("Could not select database");

	// prepare select statement
	$selectQuery = "SELECT rowId, columnId from seats where (";
	$count = 0;
	foreach($_POST['seats'] AS $seat) {
		if ($count > 0) {
			$selectQuery .= " || ";
		}
		$selectQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
		$selectQuery .= " and columnId = " . substr($seat, 1) . " ) ";
		$count++;
	}
	$selectQuery .= " ) and status = $oldStatusCode";
	if ($oldStatusCode == 1) {
		$selectQuery .= " and updatedby = '$user'";
	}

	//echo $selectQuery;

	// execute select statement
	$result = mysql_query($selectQuery);
	//echo $result;

	$selectedSeats = mysql_num_rows($result);
	//echo "<br/>" . $selectedSeats;

	if ($selectedSeats != $count) {
		$problem = "<h3>There was a problem executing your request. No seat/s were updated.</h3>";
		$problem .= "Possible problems are:";
		$problem .= "<ul>";
		$problem .= "<li>Another process was able to book the same seat while you were still browsing.</li>";
		$problem .= "<li>You were trying to Confirm an unreserved Seat.</li>";
		$problem .= "<li>You were trying to Cancel an unreserved Seat.</li>";
		$problem .= "<li>You were trying to Reserve a reserved Seat.</li>";
		$problem .= "<li>There was a problem connecting to the database.</li>";
		$problem .= "</ul>";
		$problem .= "<a href='seats.php'>View Seat Plan</a>";
		die ($problem);
	}

	// prepare update statement
	$newStatusCode = $_POST['newStatusCode'];
	$oldStatusCode = $_POST['oldStatusCode'];

	$updateQuery = "UPDATE seats set status=$newStatusCode, updatedby='$user' where ( ";
	$count = 0;
	foreach($_POST['seats'] AS $seat) {
		if ($count > 0) {
			$updateQuery .= " || ";
		}
		$updateQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
		$updateQuery .= " and columnId = " . substr($seat, 1) . " ) ";
		$count++;
	}
	$updateQuery .= " ) and status = $oldStatusCode";
	if ($oldStatusCode == 1) {
		$updateQuery .= " and updatedby = '$user'";
	}

	// perform update
	$result = mysql_query($updateQuery);
	$updatedSeats = mysql_affected_rows();

	if ($result && $updatedSeats == $count) {
		//$mysql->commit();
		echo "<h3>";
		echo "You have successfully updated $updatedSeats seat/s: ";
		echo "[";
		foreach($_POST['seats'] AS $seat) {
			$rowId = substr($seat, 0, 1);
			$columnId = substr($seat, 1);
			echo $rowId . $columnId . ", ";	
		}
		echo "]";
		echo "...</h3>";
	} else {
		//$mysql->rollback();
		echo "<h3>There was a problem executing your request. No seat/s were updated.</h3>";
		echo "Possible problems are:";
		echo "<ul>";
		echo "<li>Another process was able to book the same seat while you were still browsing.</li>";
		echo "<li>You were trying to Confirm an unreserved Seat.</li>";
		echo "<li>You were trying to Cancel an unreserved Seat.</li>";
		echo "<li>You were trying to Reserve a reserved Seat.</li>";
		echo "<li>There was a problem connecting to the database.</li>";
		echo "</ul>";
	}

	echo "<a href='seats.php'>View Seat Plan</a>";

	// Enable the autocommit feature
	//$mysqldb->autocommit(TRUE);

	// Recuperate the query resources
	//$result->free();

	mysql_close();
}
?>
</center>
</body>
</html>

 

Cheers

 

Dan

Link to comment
Share on other sites

You may want to do what revraz suggested and echo out all of your queries to ensure all the column names you're referencing exist.

 

Looks like your error is triggered in the WHERE clause which is generated by the foreach loop from your POST vars.  You can print the array of POST vars:

 

print_r($_POST);

 

or you can go back to the form where this information is coming from and see what's being inputted.

Link to comment
Share on other sites

Echo out your queries and tell me exactly what they output.

 

echo $selectQuery;
//and: 
echo $updateQuery;

 

Unknown column 'A13' in 'where clause'

 

It thinks that A13 is a column when you are actually asking - WHERE rowID = 'A13' - correct?

Link to comment
Share on other sites


Array ( [oldStatusCode] => 1 [newStatusCode] => 2 [seats] => Array ( [0] => sA13 ) ) SELECT rowId, columnId from seats where ( ( rowId = 's'SELECT rowId, columnId from seats where ( ( rowId = 's' and columnId = A13 ) Unknown column 'A13' in 'where clause'

 

Theres what you asked for.

 

It is recognising things fine except it doesnt seem to want to find the columnID's

 

Do i need to add a columnId = 'A13' ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.