Jump to content

Recommended Posts

Hi,

 

I seem to have a weird issue with the php command mysql_query. I pass 2 arguments the query in a string format (SQL is working properly) and a database ressource which is also good. The problem is that even with good parameters, the mysql_query returns me bogus data

 

exact code that cause problem

public function execute($query){
$result = mysql_query($query, $this -> ressource);
return $result;
} // execute

 

the full database object code

class DBconnexion{

private $host;		// database url/host
private $user;		// database login name
private $pass;		// database login password
private $db;		// database name
private $ressource;	// mysql ressource for database

public function DBconnexion($db_host, $db_user, $db_pass, $db_name){
	$this -> host 	= $db_host;
	$this -> user 	= $db_user;
	$this -> pass 	= $db_pass;
	$this -> db 	= $db_name;

		// connect to database
	$cnDB = mysql_connect($this -> host, $this -> user, $this -> pass);
	mysql_select_db($this -> db, $cnDB);

		// store database ressource in database object
	$this -> ressource = $cnDB;
} // DBconnexion (constructor)

	// getter for database ressource
public function get(){
	return $this -> ressource;
} // get_connexion

public function execute($query){
	    $result = mysql_query($query, $this -> ressource);
		return $result;
} // execute

public function number($sqlRessource){
	    $result = mysql_num_rows($sqlRessource);
		return $result;
} // number

	public function row($sqlRessource){
	    $result = mysql_fetch_row($sqlRessource);
		return $result;
} // row

} // DBconnexion

Here is the query (I'm a french coder so the table cols are named in french)

SELECT projets_id, projets_nom, projets_nomStrip, projets_url, projets_pdf, projets_login, projets_password, projets_miSession, projets_finSession, projets_cote FROM projets ORDER BY projets_nomStrip ASC

 

When i test it in the SQL field of phpmyadmin it works fine and retrun me the results i'm hoping for but not when i execute it from my php page

 

Here are the error messages i'm getting when trying num_rows and fetch_rows on result retruned

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/designWeb2/ressources/model/db_connexion.php on line 55

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/designWeb2/ressources/model/db_connexion.php on line 60

Ok, so what is going on is basically you are passing a bool to those functions and not the resource, if you can post the code where this error occurs (Where you actually call it) that would be helpful, in the meantime this may help you understand:

 

$dbCon = new DBconnexion($db_host, $db_user, $db_pass, $db_name); // note this is just an example so yea.
$query = $dbCon->execute("SELECT projets_id, projets_nom, projets_nomStrip, projets_url, projets_pdf, projets_login, projets_password, projets_miSession, projets_finSession, projets_cote FROM projets ORDER BY projets_nomStrip ASC");

$numRows = $dbCon->number($query);
$row = $dbCon->row($query);

 

Hopefully that will help you figure out what is wrong, if you still cannot get it working post the code plus 5-10 lines above it where you run the actual query.

here is the whole code where these commands are involved. I'm using Active Record coding pattern so that's why there is the dispatch data to tb_projets. This last section is used to make a string that will be explode once in the tb_projets object.

 

Variable $nbProjets is used later on

 

Active Record pattern

http://en.wikipedia.org/wiki/Active_record_pattern

 

$DB_obj = new DBconnexion($config["DB"]["HOST"], $config["DB"]["USER"], $config["DB"]["PASS"], $config["DB"]["NAME"]);

	// query for the list of projects
$queryProjets = "	SELECT 
						projets_id, projets_nom, projets_nomStrip, projets_url, projets_pdf, projets_login, projets_password, projets_miSession, projets_finSession, projets_cote 
				FROM 
					projets 
				ORDER BY 
					projets_nomStrip ASC";

$resultatProjets = $DB_obj -> execute($queryProjets);

	// dispatch data to tb_projets
$projets = array();
$nbProjets = $DB_obj -> number($resultatProjets);
while($row =  $DB_obj -> row($resultatProjets)){
	$fields = "";
	for($i = 1 ; $i < count($row) ; $i++){
		if($i < count($row)-1){
			$fields .= $row[$i] . ", ";
		}else{
			$fields .= $row[$i];
		} // if
	} // for
	array_push($projets, new TBprojets($DB_obj, $row[0],$fields));
	}

The only alternative I can think of is your Query has an error in it and thus returning false. As you said you did test in phpMyAdmin, try adding this just to see if there was an error:

 

$resultatProjets = $DB_obj -> execute($queryProjets);
echo "<br />MySQL Error:   " . mysql_error() . "<br /><Br />";

 

Other than that the code looks fine, so add that and see if there was an error in your query.

Thanks a lot the mysql_error() did the trick. I had problems with my config array so no database was selected. It now works properly.

 

I will work on my error handling class right now instead of later :D

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.