lofaifa Posted February 20, 2012 Share Posted February 20, 2012 this is working fine : while($tags=mysql_fetch_array($d_b->sql_answer,MYSQL_ASSOC)){ echo $tags['tag_name'] . "<br/>"; } display : html css javascript c++ php but when i try to do some sql statements inside like this : while($tags=mysql_fetch_array($d_b->sql_answer,MYSQL_ASSOC)){ $sql="SELECT * FROM user WHERE user_languages LIKE '% {$tags[tag_name]} %'"; //to apply the $sql $d_b->apply($sql); //number of results $num=$d_b->num_rows(); $sql="UPDATE tags SET tag_folowers={$num}"; $d_b->apply($sql); } it return an error : Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/ Share on other sites More sharing options...
scootstah Posted February 20, 2012 Share Posted February 20, 2012 The error is because the query failed due to an error, and thus returned FALSE. You can see what the error is with mysql_error(). Also, putting queries inside loops is generally not a good idea. You can most likely do what you want with a single query. Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/#findComment-1319348 Share on other sites More sharing options...
lofaifa Posted February 20, 2012 Author Share Posted February 20, 2012 its the same "$d_b->sql_answer" of example 1 , why is it working in case 1 and not the other ? Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/#findComment-1319352 Share on other sites More sharing options...
scootstah Posted February 20, 2012 Share Posted February 20, 2012 I don't know, print the error and find out. Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/#findComment-1319353 Share on other sites More sharing options...
lofaifa Posted February 20, 2012 Author Share Posted February 20, 2012 in fact $d_b->sql_answer==true .. now i dont know what to do its still a boolean as the error said Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/#findComment-1319355 Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2012 Share Posted February 20, 2012 The error message is most likely because your database class does not return a result resource/result object to the calling code, but instead holds the result of the query internally. This means that it cannot execute more than one query at a time and each query overwrites the result of the previous query. What's the code of your database class? Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/#findComment-1319357 Share on other sites More sharing options...
lofaifa Posted February 20, 2012 Author Share Posted February 20, 2012 <?php require_once('config.php'); require_once('functions.php'); class mysqldb { public $result=array(); public $sql_answer; private $connection; private $magic_quotes_active; private $real_escape_string_active; function __construct(){ $this->open_connection(); mysql_query("set names utf8"); $this->magic_quotes_active=get_magic_quotes_gpc(); $this->real_escape_string_active=function_exists("mysql_real_escape_string"); } public function open_connection(){ $this->connection=mysql_connect(DB_SERVER,DB_USER,DB_PASS); if(!$this->connection){ die("Database connection failed: " . mysql_error()); } else { $db_select=mysql_select_db(DB_NAME,$this->connection); if(!$db_select){ die("Database selection failed: " . mysql_error()); } } } public function close_connection(){ if(isset($this->connection)){ mysql_close($this->connection); unset($this->connection); } } public function clean($value){ if($this->real_escape_string_active){ if($this->magic_quotes_active){$value=stripslashes($value);} $value=mysql_real_escape_string($value); }else { if(!$this->magic_quotes_active){$value=addslashes($value);} } return $value; } public function apply($sql){ $sql_answer=mysql_query($sql); if(!$sql_answer){ die("your sql statement is wrong at the syntax side : " . $sql ); }else{ $this->sql_answer=$sql_answer; } } public function get_results(){ if($this->sql_answer){ $this->result=mysql_fetch_array($this->sql_answer,MYSQL_ASSOC); } else{ die("u didnt select anything at the first time"); } } public function num_rows(){ $this->num_rows=mysql_num_rows($this->sql_answer); return mysql_num_rows($this->sql_answer); } public function clean_XSS($value){ //should read about this functions soon; $value=htmlentities($value,ENT_COMPAT,'UTF-8'); return $value; } } $d_b=new mysqldb(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/#findComment-1319358 Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2012 Share Posted February 20, 2012 $this->sql_answer=$sql_answer; ^^^ The above line of code means that one instance of your database class cannot perform a second query until you are done using the data from a previous query. You would either need to make a second instance of your database class or rewrite your code so that the result resource is returned to the calling code, which assigns it to a program variable and that calling code then simply uses that program variable in the mysql_xxxxxx statements that expect a result resource. Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/#findComment-1319363 Share on other sites More sharing options...
lofaifa Posted February 20, 2012 Author Share Posted February 20, 2012 i understand the problem now .. thank u Quote Link to comment https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/#findComment-1319366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.