So, I am trying to get the IMDb id for the from the table called videos. I tried it with these these are giving me error.
Code 1:
$imdb_id = $this->db->get_where('videos', array('imdbid'))->result_array();
Code 2:
$imdb_id = $this->db->get('videos','imdbid');
code 3:
$query = $this->db->query("SELECT * FROM videos;");
$row = $query->row(0, 'videos');
$imdb_id = $row['imdbid'];
Here is a screenshot of database table. Thank you for your contribution in advance.
Please explain the error and output expectations.
you want only one return data or multiple data?
This Codeigniter 3 right?
the current code you are using is incorrect :
Code 1 :
// will get only one data with where condition
// videos_id = 1
$video_id = 1;
$data = $this->db->get_where('videos', array('videos_id'=>$video_id))->result_array();
$imdb_id = $data[0]['imdbid'];
Code 2 :
// will get all data without where condition
// must use loop foreach
$data = $this->db->get('videos')->result_array();
Related
In my MySQL table (userdata) there's a column named "options". In that column user responses are stored after they complete a quiz.
Sample data pertaining to a user is given below:
{"correctness":{"question_id_209":false,"question_id_208":true,"question_id_207":true,"question_id_206":false,"question_id_205":true},"user_answered":{"question_id_209":"830","question_id_208":"826","question_id_207":"822","question_id_206":"818","question_id_205":"815"},"passed_time":"13 seconds","user_points":3,"max_points":5,"attributes_information":[],"calc_method":"by_correctness"}
Now I want to search this array for the word "true" and want to count the word.
I started with this, but don't know what to do next:
$query="SELECT options FROM userdata WHERE user_id=1";
$result= mysqli_query($conn,$query);
If you want to only count how many correct answers the user had for display purposes, here is what you should do following your example:
$row = mysqli_fetch_assoc($result);
$data = json_decode($row['options'], true);
$correctAnswers = array_filter($data['correctness'], function($question) {
return $answer === true;
});
$total = count($correctAnswers);
But if you want to do a SQL query, then it would be more complicated. For that you might want to take a look at this page:
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html
I hope it helps.
My code just for save the data session.
This is my code:
$idfb = 12121983918 // just sample id.
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
$datalogin = $sql->row();
$loginsession= array(
'jenis_user' =>$datalogin->jenis_user, // the problem is here, i got eror in here, the errors is Trying to get property of non-object .
'photo'=>$datalogin->photo,
'facebookid'=>$datalogin->facebookid,
'id'=>$datalogin->id,
'email'=>$datalogin->email,
'username'=>$datalogin->username,
'nama'=>$datalogin->nama,
// 'jenis_user'=>$datalogin->jenis_user,
'alamat'=>$datalogin->alamat,
'no_telpon'=>$datalogin->no_telpon,
);
$this->session->set_userdata('loginsession',$loginsession);
redirect($this->agent->referrer());
I hope someone can solved this problem. Thanks! :)
It seems you are going right
Please double check your sql, make sure that you are using right table and column names.
and try replacing
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
with
<?php
$this->db->where('facebookid', $idfb);
$this->db->limit(1);
$sql = $this->db->get_where('user');
?>
if same error, try echo $sql->num_rows() to see if you are getting row or not.
Since database structure is not given, I can assume the database query is returning 0 rows. Always try to handle exception.
$idfb = 12121983918; // just sample id.
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
if($sql->num_rows()>0){
$datalogin = $sql->row();
$loginsession= array(
'jenis_user' =>$datalogin->jenis_user, // the problem is here, i got eror in here, the errors is Trying to get property of non-object .
'photo'=>$datalogin->photo,
'facebookid'=>$datalogin->facebookid,
'id'=>$datalogin->id,
'email'=>$datalogin->email,
'username'=>$datalogin->username,
'nama'=>$datalogin->nama,
// 'jenis_user'=>$datalogin->jenis_user,
'alamat'=>$datalogin->alamat,
'no_telpon'=>$datalogin->no_telpon,
);
$this->session->set_userdata('loginsession',$loginsession);
redirect($this->agent->referrer());
}
else
{
Your Code
}
I generate chart using jpgraph. I succesfully get data for the plot from database and i want the title also take from database. i already use this script
$sql = $this->db->select("title from table")->get()->first_row();
$title = $sql->title;
$graph->title->Set($title);
But that not work. can anyone solve this issue? thank you
This will help to get first_row along with field:
$this->db->select('title'); // your column
$this->db->from('table'); // your table
$result = $this->db->get()->result(); // get result
$title = $result->first_row()->title; // get ist row using first_row with your field name
$graph->title->Set($title); // as you are using for graph
Also note that, in CI function row() also return the ist row of your query in object form.
From the manual:
You can walk forward/backwards/first/last through your results using
these variations:
$row = $query->first_row()
$row = $query->last_row()
$row = $query->next_row()
$row = $query->previous_row()
Or if you want to print result in array instead of object than you can use a word 'array' as param, like:
$query->first_row(‘array’)
You can also follow the CI manual for better understanding: http://www.codeigniter.com/userguide3/database/results.html
I want to get the current ID while inserting the value in database.Which i will use to save custom Data. How Do I do it?
I have written a query already.
$currentId = Student::orderBy('id', 'desc')->get(['id']) + 1;
But it's showing the following Error :
Object of class Illuminate\Database\Eloquent\Collection could not be
converted to int
$currentId = Student::orderBy('id', 'desc')->first()->id + 1;
But on the heavy traffic website this potentially can cause problems with data integrity, so I'd user this approach:
$student = new Student();
....
$student->save();
$currentId = $student->id;
try this
$data = Student::orderBy('id', 'desc')->get(['id']);
$data->getId();
You have to do something like this :-
$insertData['name'] = 'your name';
$insertData['password'] = 'password';
..
etc..
// $insertData this will be your array which you want to insert in your db. name and password used in insertData array will be same as your table column name..
$lastId = Student::insertGetId($insertData);
It will give you last inserted id in laravel, it will help you.
I currently have a db table filled with organisation numbers [1,002 million rows].
Now, what im trying to do is fetch the phone number of the organisation from a remote website API. I've got this working, but just after 30-50 or so requests to the API, i dont see any new changes to the table im inserting the phone numbers into. I've still got 1 million++ rows to fetch the phone number from, but i cant seem to get further than a small amount of rows.
Thanks in advance for the help.
I dont know if this is gonna help, but here is the code im using to do this.
// Remove timeout limit
// This is going to take alot of time!
set_time_limit(0);
// Initialize...
include $_SERVER['DOCUMENT_ROOT'] . '/core/Init.php';
// Profiles
$url = 'http://finnrett.no/API/business/get/quickresults?q=';
$profile_url = 'http://finnrett.no/API/business/get/profile?id=';
// Select names
$sql = 'SELECT organisasjonsnummer FROM brreg ORDER BY id LIMIT 10000';
$result = $Dbh -> query($sql, []);
// Each name
foreach ($result as $orgnr) {
// Pre for output explananation
echo'<pre>';
// Grab json from quick results url
$bedrift = json_decode(file_get_contents($url.$orgnr['organisasjonsnummer']));
$ID = get_object_vars($bedrift[0])['ID'];
$profile = json_decode(file_get_contents($profile_url.$ID));
$CONTACT = get_object_vars($profile);
$number = $CONTACT['contact'] ? $CONTACT['contact']: '0';
$sql = 'INSERT INTO profiles (orgnr, telefon) VALUES (:orgnr, :telefon)';
$args = ['orgnr' => $orgnr['organisasjonsnummer'], 'telefon' => $number];
if (!$Dbh -> query($sql, $args)) {
echo 'Sjekk opp org: ' . $orgnr['organisasjonsnummer'] . ' fordi her skjedde det noe galt.';
}
}
Looks like i solved it by choosing curl instead of file_get_contents.
Solved it by switching to curl instead of file_get_contents()