8 Replies - 340 Views - Last Post: 07 February 2012 - 04:32 PM Rate Topic: -----

Topic Sponsor:

#1 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Query Must Not Be Working.

Posted 27 January 2012 - 08:36 PM

I have a query set in a string:

$query="UPDATE `all` SET FilterToggle='0' WHERE id=" . $_SESSION['id']; 


When I run mysql_query() it returns false when I call the mysql_num_rows function. However I check it before the _num_rows function:

if ($_POST['filter'] == true) 
	{
		$query="UPDATE `all` SET FilterToggle='0' WHERE id=" . $_SESSION['id']; 
		$res=mysql_query($query) or die(mysql_error()); 
		if ($res) { 
			echo mysql_num_rows($res);
		} 
		else 
		{ 
			echo 'bad query'; 
		} 
	}

So I figured it's either a problem with my logic of the code or the query. It says mysql_num_rows() expects the first argument/parameter to be a resource and not a boolean.

Is This A Good Question/Topic? 0
  • +

Replies To: Query Must Not Be Working.

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1940
  • View blog
  • Posts: 7,294
  • Joined: 08-August 08

Re: Query Must Not Be Working.

Posted 27 January 2012 - 09:26 PM

I'm guessing $_SESSION['id'] isn't set. See what your query looks like after it's built:
echo $query;

or
error_log($query);

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • No Sugar Coding Here!
  • member icon

Reputation: 4683
  • View blog
  • Posts: 20,361
  • Joined: 23-August 08

Re: Query Must Not Be Working.

Posted 28 January 2012 - 07:16 AM

Yes, always use this pattern when running a query that you're assembling from user-input data:

$sql = "UPDATE `all` SET FilterToggle='0' WHERE id=" . $_SESSION['id'];
$result = mysql_query($sql);
if (!$result) {
    // If debugging, then die here.
    die("Query {$sql} failed: " . mysql_error());

    // If this is exposed to the outside world, log the error somewhere
    // instead of using it in a die()
}

Was This Post Helpful? 0
  • +
  • -

#4 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: Query Must Not Be Working.

Posted 07 February 2012 - 09:33 AM

		if ($_POST['filter'] == true) 
		{
			$query="UPDATE `all` SET FilterToggle='0' WHERE id='" . $_SESSION['id'] . "'"; 
			echo $query; 
			$res=mysql_query($query); 
			if (!$res) { 
				die("Query {$sql} failed: " . mysql_error()); 
			} 
			else 
			{ 
				echo "<br /> Query $query is fine! [$res]"; 
			} 
		}


Returned:

Quote

UPDATE `all` SET FilterToggle='0' WHERE id='30'
Query UPDATE `all` SET FilterToggle='0' WHERE id='30' is fine! [1]

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,426
  • Joined: 08-June 10

Re: Query Must Not Be Working.

Posted 07 February 2012 - 09:39 AM

note that SQL INTEGER field values are passed without quotes.
Was This Post Helpful? 0
  • +
  • -

#6 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: Query Must Not Be Working.

Posted 07 February 2012 - 09:42 AM

		if ($_POST['filter'] == true) 
		{
			$query="UPDATE `all` SET FilterToggle=0 WHERE id=" . $_SESSION['id']; 
			echo $query; 
			$res=mysql_query($query); 
			if (!$res) { 
				die("Query {$query} failed: " . mysql_error()); 
			} 
			else 
			{ 
				echo "<br /> Query: <br /> <div style='background-color:#970026; width: 350px; height: 100px; border: solid black 2px;'>{$query}</div> <br /> All is good! {$res}";
				echo mysql_num_rows($res); 
			} 
		}



Quote

UPDATE `all` SET FilterToggle=0 WHERE id=30
Query:
UPDATE `all` SET FilterToggle=0 WHERE id=30

All is good! 1
( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\___________\filterc.php on line 27

This post has been edited by SittingonDucks: 07 February 2012 - 09:46 AM

Was This Post Helpful? 0
  • +
  • -

#7 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: Query Must Not Be Working.

Posted 07 February 2012 - 09:52 AM

It seems to return true, but it doesn't give a resource...
Was This Post Helpful? 0
  • +
  • -

#8 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: Query Must Not Be Working.

Posted 07 February 2012 - 10:50 AM

Got it working. Nevermind ;]
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • No Sugar Coding Here!
  • member icon

Reputation: 4683
  • View blog
  • Posts: 20,361
  • Joined: 23-August 08

Re: Query Must Not Be Working.

Posted 07 February 2012 - 04:32 PM

The reason, which you may already know but I'll add it here for posterity, is mysql_num_rows only works with SELECT statements, i.e., those queries that return rows of data. For other queries, i.e. those where you're changing data already within the database, the proper call is mysql_affected_rows. This info is available in the excellent documentation:

Quote

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1