Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can we append/concat query string someway 1

Status
Not open for further replies.

heavenprince

Programmer
Sep 13, 2006
3
0
0
CA
Can we append/concat query string someway, because the followng code is giving a warning rather than executing the query including the concatenated part:

Code:
<?php 
	require_once ('conn_db.php');
	$query = "select * from pm where city_id = 7";
	$query = $query . "and type = 4";
	$num = mysql_num_rows (mysql_query ($query)); // line 5
	echo $num;
?>

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in test.php on line 5

 
I myself discovered the answer, simply put a space before the " and type=4"

bcz all the clauses in a query be seperated by a whitespace.

so it should be:
$query = $query . " and type = 4";
not:
$query = $query . "and type = 4";

 
For debugging purposes it is always recommended to echo out the query to make sure its as expected.

had you done

echo $query;

before trying to run it, it would have been evident that the second part of the query was lacking a space.

You could then add the space to the end of the first part of the query or the beggining of the second.

Also for clarity concatenation can be done like this:

Code:
$query = "select * from pm where city_id = 7";
    $query[red].[/red]=" and type = 4";

Notice the red period before the equals sign this tells PHP to concatenate the string to the variable.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top