• 0

[PHP] mysql not returning anything


Question

hi.

i'm going nuts with a function that returns some values from a table, the code works, no mysql or php errors, but the array is not populated.

it's always returning a null value.

every attempt charles webproxy shows me that the result is null.

here is my code:

/**
	 * Returns the item corresponding to the value specified for the primary key.
	 *
	 * Add authorization or any logical checks for secure access to your data 
	 *
	 * @param string $itemID
	 * @return stdClass[]
	 */
	public function getCursoInfoByID($itemID) {


		$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where IDCurso=?");	
		$this->throwExceptionOnError();


		 mysqli_stmt_bind_param($stmt, 's', $itemID);
		 $this->throwExceptionOnError();

		mysqli_stmt_execute($stmt);
		 $this->throwExceptionOnError();

		 $rows = array();

		 mysqli_stmt_bind_result($stmt, $row->IDCurso, $row->IDModulo, $row->NomeModulo, $row->ModuloAnterior, $row->ModuloSeguinte, $row->IDUnidade, $row->NomeUnidade, $row>UnidadeAnterior, $row->UnidadeSeguinte, $row->IDTopico, $row->NomeTopico, $row->TopicoAnterior, $row->TopicoSeguinte, $row->IDEcra, $row->NomeEcra, $row->EcraAnterior, $row->EcraSeguinte,$row->NumCreditos,$row->PathEcra);
		 array_push($rows,$row);

	     while (mysqli_stmt_fetch($stmt)) {
	   $newstdclass= new stdClass();
			mysqli_stmt_bind_result($stmt, $row->IDCurso, $row->IDModulo, $row->NomeModulo, $row->ModuloAnterior, $row->ModuloSeguinte, $row->IDUnidade, $row->NomeUnidade, $row->UnidadeAnterior, $row->UnidadeSeguinte, $row->IDTopico, $row->NomeTopico, $row->TopicoAnterior, $row->TopicoSeguinte, $row->IDEcra, $row->NomeEcra, $row->EcraAnterior, $row->EcraSeguinte,$row->NumCreditos,$row->PathEcra);
		  $newstdclass= $row;
		  array_push($rows,$newstdclass);
	     }

		 mysqli_stmt_free_result($stmt);
	     mysqli_close($this->connection);

	     return $rows;
		}

am i doing something wrong?

i can't seem to get my head around this....

forgot to add something, i can get results on my local machine, but not on a remote server, i've checked permissions, adjusted the connection to the server but still it does not work

help would be apreciated

thanks in advance

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I've never seen SQL data pulled like this (usually use the fetch_array methods). It looks generally ok to me though, apart from what appears to be a slightly pointless creation of $newstdclass which is then overwritten with $row. Other than that, I'm clueless.

With apparently complicated issues like this it is usually something simple that breaks it, so are you sure that the SQL query is actually returning data (check in phpMyAdmin or whatever the MySQLi equivalent is)? If you are sure it is, then you can try dumping variables throughout the method to find at what point the data disappears. Not elegant, but it usually narrows down an issue quite quickly.

Link to comment
Share on other sites

  • 0

this code is generated by flashbuilder, it creates the service to get the table contents

i've checked with multiple mysqli's and the data exists.

i've tweaked it around a bit and now it looks like this:

$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where IDCurso=?");	
		$this->throwExceptionOnError();


		 mysqli_stmt_bind_param($stmt, 's', $itemID);
		 $this->throwExceptionOnError();

			mysqli_stmt_execute($stmt);
		 $this->throwExceptionOnError();

		 $rows = array();

		mysqli_stmt_bind_result($stmt, $row->IDCurso, $row->IDModulo, $row->NomeModulo, $row->ModuloAnterior, $row->ModuloSeguinte, $row->IDUnidade, $row->NomeUnidade, $row->UnidadeAnterior, $row->UnidadeSeguinte, $row->IDTopico, $row->NomeTopico, $row->TopicoAnterior, $row->TopicoSeguinte, $row->IDEcra, $row->NomeEcra, $row->EcraAnterior, $row->EcraSeguinte);

	    while (mysqli_stmt_fetch($stmt)) {
	      $rows[] = $row;
	      $row = new stdClass();
	      mysqli_stmt_bind_result($stmt, $row->IDCurso, $row->IDModulo, $row->NomeModulo, $row->ModuloAnterior, $row->ModuloSeguinte, $row->IDUnidade, $row->NomeUnidade, $row->UnidadeAnterior, $row->UnidadeSeguinte, $row->IDTopico, $row->NomeTopico, $row->TopicoAnterior, $row->TopicoSeguinte, $row->IDEcra, $row->NomeEcra, $row->EcraAnterior, $row->EcraSeguinte);
	    }

		mysqli_stmt_free_result($stmt);
	    mysqli_close($this->connection);

	    return $rows;

i'm going to break it and try to find out where the code breaks

Link to comment
Share on other sites

  • 0

one other thing, is it possible that this happens because the data is being pulled from a view instead of a normal table?

Link to comment
Share on other sites

  • 0

Not sure what you mean, but if you think it is possible then test it and find out. :p

I'd try doing a var_dump (or use a debugging method's dump if you have one) on $row before the while loop next.

I don't think the usage of the bind method is correct though for getting the result as an array. As I said, I'm not familiar with these methods, but reading the documentation it doesn't look like they are being used quite right. All it seems to be doing is temporarily (and pointlessly) offloading the data to the $row class var, before pulling it back for usage in an array. Might as well just put it straight into the array and not waste the resources. It looks like one of the (many) cases where OOP is being used for the sake of it, rather than for any benefit.

Try this method and see how/if it works with the class.

Link to comment
Share on other sites

  • 0

you tell me....this is the standard way that adobe flex uses zend framework or amfphp, as you create a class to get some data from a db.

thanks for the help, i followed the link, tweaked the class a bit to my needs and it's working.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.