Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/257412-while-do-not-apply-sql-statements/
Share on other sites

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.

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?

<?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();
?>

$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.

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.