I'm trying to reset the pointer to the first record in CodeIgniter. Consider the following greatly simplified code:
$query_milestones = "SELECT * FROM milestones";
$milestones = $this->db->query($query_milestones);
foreach($milestones->result() as $milestoneRow){
// do something
}
$milestones->data_seek(0); // <--- This gives me Fatal error.
foreach($milestones->result() as $milestoneRow){
// do something else
}
This gives me:
Fatal error: Call to undefined method CI_DB_mysql_result::data_seek()
How can I do a mysql_data_seek with CodeIgniter?
UPDATE: It seems VERY odd to me but resetting the pointer apparently is not necessary. The following does what I want but not what I expect:
$query_milestones = "SELECT * FROM milestones";
$milestones = $this->db->query($query_milestones);
foreach($milestones->result() as $milestoneRow){
// do something
}
// As soon as the foreach is reached, the first record is retrieved again.
foreach($milestones->result() as $milestoneRow){
// do something else
}
try #$this->db->data_seek(0); instead of $milestones->data_seek(0);
You can try $milestones->_data_seek(0); but I have not used CodeIgniter in a while and supposedly it is a private method, at least it used to be.
Related
I am subscribing to data from a MQTT broker with phpMQTT. I have successfully set up a pub / sub routine based on their basic implementation. I can echo the information just fine inside the procmsg() function.
However, I need to take the data I receive and use it for running a few database operations and such. I can't seem to get access to the topic or msg received outside of the procmsg() function. Using return as below seems to yield nothing.
<?php
function procmsg($topic, $msg){
$value = $msg * 10;
return $value;
}
echo procmsg($topic, $msg);
echo $value;
?>
Obviously I am doing something wrong - but how do I get at the values so I can use them outside the procmsg()? Thanks a lot.
I dont know about that lib, but in that code
https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php ,
its possible see how works.
in :
$topics['edafdff398fb22847a2f98a15ca3186e/#'] = array("qos"=>0, "function"=>"procmsg");
you are telling it that topic "edafdff398fb22847a2f98a15ca3186e/#" will have Quality of Service (qos) = 0, and an "event" called 'procmsg'.
That's why you later wrote this
function procmsg($topic,$msg){ ... }
so in the while($mqtt->proc()) this function will check everytime if has a new message (line 332 calls a message function and then that make a call to procmsg of Source Code)
thats are the reason why you cannot call in your code to procmsg
in other words maybe inside the procmsg you can call the functions to process message ej :
function procmsg($topic,$msg){
$value = $msg * 10;
doStuffWithDataAndDatabase($value);
}
Note that you can change the name of the function simply ej :
$topics['edafdff398fb22847a2f98a15ca3186e/#'] = array("qos"=>0, "function"=>"onMessage");
and then :
function onMessage($topic,$msg){
$value = $msg * 10;
doStuffWithDataAndDatabase($value);
}
Sorry for my english, hope this help !
i'm not good enough at english and PHP. So I have a question: How to get result by 'GET' request?
i have an url like this:
laporanpemesanancetak.php?TANGGAL_PERIODE1=2016-05-01&TANGGAL_PERIODE2=2016-05-23
and my GET method like:
if (isset($_GET["$TANGGAL_PERIODE1"])) {
$TANGGAL_PERIODE1 = $_GET["TANGGAL_PERIODE1"];
$TANGGAL_PERIODE2 = $_GET["TANGGAL_PERIODE2"];
$query = mysql_query("select p.PURCHASE_ORDER_PEMESANAN,p.TANGGAL_PEMESANAN,s.NAMA_SUPPLIER,i.NAMA_BAHAN_BAKU,p.JUMLAH_PEMESANAN,i.SATUAN,k.NAMA_KARYAWAN
from pemesanan p,supplier s,inventori i,karyawan k
where p.NPWP_SUPPLIER = s.NPWP_SUPPLIER and p.ID_BAHAN_BAKU = i.ID_BAHAN_BAKU and p.NIP_KARYAWAN = k.NIP_KARYAWAN and p.TANGGAL_PEMESANAN between '$TANGGAL_PERIODE1' and '$TANGGAL_PERIODE2' order by p.PURCHASE_ORDER_PEMESANAN");
while ($row = mysql_fetch_array($query)) {
$pdf->Cell(27,8,$row["PURCHASE_ORDER_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["TANGGAL_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_SUPPLIER"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_BAHAN_BAKU"],1,0,"C");
$pdf->Cell(27,8,$row["JUMLAH_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["SATUAN"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_KARYAWAN"],1,0,"C");
$pdf->Ln();
}
}
and the result is show nothing, i've tried using
if (isset($_GET["$TANGGAL_PERIODE1"])&&$_GET["$TANGGAL_PERIODE2"])
but the result is same, i've tried my sql query into mysql and it works.
Can someone help me please? it would be great :)
$_GET["$TANGGAL_PERIODE1"]
Note the $ in the key. This will try to evaluate the variable $TANGAL_PERIODE1 rather than look a key of the $_GET array named TANGAL_PERIODE1
Be sure to not ignore E_NOTICE and E_WARNING messages when developing. These are important clues, this would generate a notice of an undefined variable.
I am occasionally getting the PHP Fatal error: Call to undefined method stdClass::transition() in agent.php on line 25 (I marked line 25 in the code). This code is called often, so struggling to see why it is happening.
Here is the snippet of agent.php that calls the
function agent_exam_complete($exam){
$ce = $exam->educational();
$ce->exam_id = $exam->exam_id;
$ce->exam_grade = $exam->score;
$ce->exams_remaining -= 1;
$ce->exam_received_date = sql_now();
if($exam->status()=='passed'){
$ce->transition('passed');
}elseif($ce->exams_remaining <= 0){
$ce->transition('failed');
}
$ce->save();
if($ce->is_certification_completed($ce->certification_id, $ce->client_no)){
agent_certification_complete($ce->certification_id, $ce->client_no);
}
}
function agent_certification_complete($certification_id, $client_no){
$ce = ClientPurchase::find('first', array('conditions' => "certification_id = '$certification_id' and is_certification = 1 and client_no='$client_no'"));
$ce->certification_date = date('Y-m-d');
$ce->transition('passed'); **//Line 25**
$ce->save();
}
transition() is defined in another file and is called often. I've included a little bit of it's code just for flavor.
function transition($event_tag){
$old_status = $this->status;
$next_status = $this->next_status_for_transition($event_tag);
if($next_status==''){
return; }
$this->status = $next_status;
My question is, why am I only getting this error periodically and not all the time? What can I do to eliminate the error and subsequent blank screen for my clients? I've only noticed that it is happening to those with Firefox or Chrome.
Thanks in advance,
Jim
The object $ce that contains the function is being generated multiple times. I suppose this is so transition is customized for whatever object is called.
Why not create another object for re-useable functions? Consider expanding the function so that it is compatible with all objects that would use it.
$my = new functionClass;
class functionClass
{
function transition()
{
$old_status = $this->status;
$next_status = $this->next_status_for_transition($event_tag);
if($next_status==''){
return; }
$this->status = $next_status;
}
}
$my->transition( 'passed' );
Something like that would cut down on unpredictability and I believe may solve your problem.
Try this little snippet of code to see whats going on:
$ce = false;
$ce->certification_date = date('Y-m-d');
var_dump($ce);
In this case $ce get cast to an object of stdClass when you try to set a property (certification_date).
Now your code:
function agent_certification_complete($certification_id, $client_no){
$ce = ClientPurchase::find('first', array('conditions' => "certification_id = '$certification_id' and is_certification = 1 and client_no='$client_no'"));
//$ce is probably false or null
//it gets cast to a stdClass object
$ce->certification_date = date('Y-m-d');
//stdClass does not have a transition method; ERROR
$ce->transition('passed'); **//Line 25**
$ce->save();
}
So in your code, if find() is returning null or false, or maybe some other choice values, $ce gets cast to a stdClass object on the next line. Then that stdClass object does not have a transition() method so you get an error.
To fix this, either adjust your find method or check its return value and handle accordingly.
As to it happening only in certain browser, I think thats a false conclusion. If find() is calling a query, it probably only happens at certain times depending on the result of that query.
I have a bit of PHP code that works fine on my production server but not on my test server. Here is the code:
function callProcedure0(&$connection, $procname, $dofunction)
{
mysqli_multi_query($connection, "CALL " . $procname . "();");
$first_result = 1;
do
{
$query_result = mysqli_store_result($connection);
if ($first_result)
{
$dofunction($query_result);
$first_result = 0;
}
if ($query_result)
{
mysqli_free_result($query_result);
}
$query_result = NULL;
} while(mysqli_next_result($connection));
}
...
function doGenres($in_result)
{
global $genre_array, $game_array, $genre_order_array;
$genre_count = 1;
// foreach is necessary when retrieving values since gaps may appear in the primary key
while ($genre_row = mysqli_fetch_row($in_result)) // line 81 is here!
{
$genre_array[] = $genre_row[0];
$genre_order_array[$genre_row[1] - 1] = $genre_count;
$game_array[] = [[],[]];
$genre_count += 1;
}
}
...
callProcedure0($con, "get_genres_front", doGenres); // line 138 is here!
The "get_genres_front" bit refers to a stored procedure on my database. Here are the errors:
Notice: Use of undefined constant doGenres - assumed 'doGenres' in /opt/lampp/htdocs/keyboard/keyboard.php on line 138
Again, there are no problems on the production server which is using Apache 2.2.23, MySQL 5.1.73-cll, PHP 5.4.26. The test server where things are broken is running Apache 2.4.10, MySQL 5.6.21, PHP 5.5.19.
Did something change in recent software versions? Thanks.
[edit]
This is not a duplicate question. I'm worried about the first error. I already know what to do about the second error which I have deleted.
The code you have posted is wrong, you must pass function name as string and then use call_user_func to invoke this function.
In your callProcedure0 function change
$dofunction($query_result);
to
call_user_func($dofunction, $query_result);
And then call it with the function name as string like this
callProcedure0($con, "get_genres_front", "doGenres");
The above code could work also with invoking the function with
$dofunction($query_result);
on some php versions, but the line where you pass the function name it should be string, otherwise PHP assumes it is a constant.
I have some code that runs fine until it hits this:
if(array_key_exists($snIdx, $fields));
$surnameField = trim($fields[$snIdx]);
or other version I tried:
if(isset($fields[$snIdx));
$surnameField = trim($fields[$snIdx]);
The $snIdx = -1.
It gives me Undefined offset error at second line ($surname = trim...).
I think I don't need to paste rest of code as the exception says there is sth wrong with those functions. My PHP version is 5.4.16.
Remove the semicolon from the end of the if line. Otherwise it's equivalent to:
if( something) {
// no-op
}
$surnameField = trim($fields[$snIdx]); // undefined offset error.