Re: simple question abt convert to integer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 25, 2011 at 08:57:18AM +0430, Negin Nickparsa wrote:

> $id=(int)$_POST['txt'];
> $query1="select * from patient where id=".$id."";

You're not *thinking* about what you're doing. The above is silly. Think
about it: you're sending a string to MySQL. If $_POST['txt'] returns a
string which looks like the number 1, then

$query1 = "select * from patient where id = $_POST[txt]";

should suffice. If you like, test $_POST['txt'] first by echoing it.


> echo $query1;
> $result1=mysql_query($query1);

Ideally, you should be calling this function with an added "connection"
parameter. Like this:

$link = mysql_connect($connection_stuff);
$result1 = mysql_query($query1, $link);

It's not *necessary*, but advisable.

mysql_query() returns a "resource" object, unless there is a problem. If
there is a problem, then it returns FALSE. You can check what it returns
this way:

if (is_resource($result1))
	print "It's a resource!";
elseif ($result1 === FALSE)
	print "It's false!";
else
	print "I don't know what the heck it is!";

> 
> echo $result1;

I don't know what you'll get from this "echo" if $result1 truly is a
resource. But if it's false, you won't get much. $result1 should be a
"resource" object, which means it's opaque. You can't know what's in it
unless you use a "helper" function like mysql_num_rows(), etc.

> $num2=Mysql_num_rows($result1);
> $num3=Mysql_num_fields($result1);
> 
> still it has previous error
> 
> Here is my output:select * from patient where id=1
> *Warning*: mysql_num_rows() expects parameter 1 to be resource, boolean
> given in
> 
> *Warning*: mysql_num_fields() expects parameter 1 to be resource, boolean
> given in **

Your error messages clearly mean that 1) you're not getting a proper
resource object back from mysql_query(), or 2) you're somehow changing
$result1 into a boolean before passing it to these other functions. But
I suspect it's #1 above.

Please see the documentation for these functions at php.net. It may
even be available in your native language.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux