I know this isn't good practice to have the queries within a controller, but for some reason they do not work within this controller class. I'm not too experienced with codeigniter, so any help is appreciated, as I have no idea on how else to set this up to work properly. I have an ajax script that gets the information.
Here is my code:
<?php
class Phonecall extends CI_Controller {
public function index() {
/*$con = mysqli_connect('localhost','root','root','MYDB');
if (!$con) {
die ('Could not connect: ' . mysqli_error($con));
}*/
//$operatorId = $_SESSION['Oper']['OperatorID']; //I also cannot figure out how to get $_SESSION information either
//$sql = "SELECT phone_number FROM incoming_calls WHERE OperatorID='${operatorId}'";
//$result = mysqli_query($con,$sql);
$query = $this->db->query("SELECT phone_number FROM MYDB.incoming_calls");// WHERE OperatorID='${operatorId}'");
while ($row = $query->row_array()) {
$number = $row['phone_number'];
}
/*$sql = "SELECT Username, UserID, Name
FROM tblUsers
WHERE PhoneHome='999-999-9999' OR PhoneCell='999-999-9999' OR PhoneWork='999-999-9999'";
*/
$query = $this->db->query("SELECT Username, UserID, Name
FROM MYDB.tblUsers
WHERE PhoneHome='999-999-9999' OR PhoneCell='999-999-9999' OR PhoneWork='999-999-9999'");
while ($row = $query->row_array()) {
$userArray[] = array("name" => $row['Name'], "username" => $row['Username'], "user_id" => $row['UserID']);
}
if (!empty($userArray)) {
echo json_encode($userArray);
}
if (isset($_POST["drop"])) {
$query = $this->db->query("DELETE FROM MYDB.incoming_calls
WHERE phone_number = $number
LIMIT 1");
if (!$result) {
die ('Could not drop row: ');
}
}
$this->db->close();
}
}
?>
Here is my JS:
var user = new Array();
var user_id = new Array();
var name = new Array();
$.get(baseURL + 'phonecall/index', function(data) {//where baseURL is defined
var loginInfo = jQuery.parseJSON(data);
for (var i = 0; i < loginInfo.length; ++i) {
name[i] = loginInfo[i].name;
user[i] = loginInfo[i].username;
user_id[i] = loginInfo[i].user_id;
}
}
I do not know much about frameworks and I'm trying to learn, I also do not know how to make this file access the $_SESSION data. Any help is greatly appreciated.
Always use model for DB queries.
For first query use the following code:
$this->db->select("phone_number");
$this->db->from("incoming_calls");
$query = $this->db->get();
return $query->result_array();
For the secound query use this:
$this->db->select("Username, UserID, Name");
$this->db->from("tblUsers");
$this->db->where("PhoneHome,'999-999-9999'");
$this->db->where("PhoneCell,'999-999-9999'");
$this->db->where("PhoneWork,'999-999-9999'");
$query = $this->db->get();
return $query->result_array();
Be sure to make 2 different functions for the queries in model.
Then at your controller side use the following code:
$data['first_record'] = $this->your_modal_name->your_first_function_name();
$data['secound_record'] = $this->your_modal_name->your_second_function_name();
Then run this data in foreach, and it will give you the record, which you want.
Related
I'm not really much into PHP or mySQL so I hope you can help me.
I got a php script with a function that returns a json with all the entries in a database table:
public function select($table, $wheres = null)
{
$connect = $this->connect();
if ($wheres == null)
{
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'`');
} else {
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'` WHERE '.$this->wheres($wheres));
}
$i = 0;
$ret = array();
while ($row = mysqli_fetch_assoc($query)) {
foreach ($row as $key => $value) {
$ret[$i][$key] = $value;
}
$i++;
}
return ($ret);
}
Edit:
I have my application which calls this function. Everything worked as expected but then I added 2 more fields to one of the tables ( 1 text, 1 varchar ), and now when I call this function it returns nothing ; literally an empty string.
I've also noticed that if I just delete those new fields it works, which is kinda annoying plus I need those fields and I can't figure out where the problem root is.
Also, the application code is not the problem: when this function is called it only passes 2 parameters (the method name, and the table name), it can't be wrong.
By the way, here is the table structure if it may help you help me:
pic related
This function helps you to get data form database in json from , this return json
<?php
function getData($tbl_name=null,$id=null)
{
$con = mysqli_connect("localhost","root","","mydb"); //my db is my database name
if(!$con){ die( "could't connect with database" ); }
$sql = mysqli_query($con,'SELECT * FROM $tbl_name WHERE id = {"$id"}');
while($row = mysqli_fetch_assoc($sql))
{
#echo"<pre>";
#print_r($row); //this will return data form data base in array from
return json_encode($row);
}
}
?>
I am working on Basketball Referee project. I am trying to update table checking input value.
I am sending the value from my control.php like this:
$check_user = $this->input->post("headreferee");
$check = $this->sql_model->check1($check_user);
Here is my sql_model.php:
function check1($referee_name)
{
$sql = "SELECT * FROM duties WHERE username ='{$referee_name}' ";
$query = $this->db->query($sql);
if($query->num_rows()>0)
{
$this->db->set('count','count'+1);
$this->db->insert('duties');
}
else
{
return 0;
}
}
Actually it is increasing count and adding new row but without any referee_name. It has to find the correct referee_name and increase that row's count.
If you want to update so please modify your check1() function by below script.
function check1($referee_name)
{
$sql = "SELECT * FROM duties WHERE username ='{$referee_name}' ";
$query = $this->db->query($sql);
$result_referee = $query->row();
$countNew = $result_referee->count + 1;
if($query->num_rows()>0)
{
$countArray = array('count'=>$countNew);
$this->db->where('username',$referee_name);
$this->db->update('duties',$countArray);
}
else
{
return 0;
}
}
I hope that will be helpful for you as your solution.
I'm getting a 500 server error within the console when the ajax script is executed, and I'm not sure as to why. If I change the code up a bit to implement mysqli functions, and run these on my local machine everything works just fine, but as soon as I try to get this to work with codeigniter, I get the 500 server error.
Here is my code:
<?php
class Phonecall extends CI_Controller {
public function index() {
/*$con = mysqli_connect('localhost','root','root','MYDB');
if (!$con) {
die ('Could not connect: ' . mysqli_error($con));
}*/
//$operatorId = $_SESSION['Oper']['OperatorID'];
//$sql = "SELECT phone_number FROM incoming_calls WHERE OperatorID='${operatorId}'";
//$result = mysqli_query($con,$sql);
$query = $this->db->query("SELECT phone_number FROM MYDB.incoming_calls");// WHERE OperatorID='${operatorId}'");
while ($row = $query->row_array()) {
$number = $row['phone_number'];
}
/*$sql = "SELECT Username, UserID, Name
FROM tblUsers
WHERE PhoneHome='999-999-9999' OR PhoneCell='999-999-9999' OR PhoneWork='999-999-9999'";
*/
$query = $this->db->query("SELECT Username, UserID, Name
FROM MYDB.tblUsers
WHERE PhoneHome='999-999-9999' OR PhoneCell='999-999-9999' OR PhoneWork='999-999-9999'");
while ($row = $query->row_array()) {
$userArray[] = array("name" => $row['Name'], "username" => $row['Username'], "user_id" => $row['UserID']);
}
if (!empty($userArray)) {
echo json_encode($userArray);
}
if (isset($_POST["drop"])) {
$query = $this->db->query("DELETE FROM MYDB.incoming_calls
WHERE phone_number = $number
LIMIT 1");
if (!$result) {
die ('Could not drop row: ');
}
}
$this->db->close();
}
}
?>
Here is the ajax call that retrieves the information:
var user = new Array();
var user_id = new Array();
var name = new Array();
$.get(baseURL + 'phonecall/index', function(data) {//where baseURL is defined
var loginInfo = jQuery.parseJSON(data);
for (var i = 0; i < loginInfo.length; ++i) {
name[i] = loginInfo[i].name;
user[i] = loginInfo[i].username;
user_id[i] = loginInfo[i].user_id;
}
}
Does anyone know as to why this would happen?
Sorry about the last post I had. Here's my revision, please help me.
<?php
//connect database
$sql = "SELECT * FROM user where user_id = 8320 AND password = 'admin' ";
$query = pg_query($sql);
var_dump($row = pg_fetch_array($query)); //dumps correctly.
?>
BUT THE PROBLEM IS THIS..when I try to make it as a function LIKE:
function check($user_id, $password)
{
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($sql);
$row = pg_fetch_array($query);
return $row;
}
AND CALL IT HERE:
var_dump($data = check(8320, 'admin')); DUMPS NULL;
How come it ended up like this?
Its returning NULL because there is an error with your SQL query, and no results are being returned. You should do some error checking in your function, try this version:
function check($user_id, $password)
{
$dbconn = pg_connect("host=localhost dbname=test");
$sql = "SELECT * FROM user where user_id = $1 AND password = $2 ";
$result = pg_query_params($dbconn, $sql, array($user_id,$password));
$row = pg_fetch_array($result);
if (!$row) {
echo pg_last_error($dbconn);
} else {
return $row;
}
}
Try the code below. It should work fine for you.
$data = check(8320, 'admin');
var_dump($data);
Seems like your PostgreSQL resource is missing inside the function. You have two options.
Declare the connection resource inside the function using global.
Establish the connection inside the function.
This is the first option:
$conn = pg_connect('host','user','pass','db');
function check($user_id, $password)
{
global $conn;
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($conn, $sql);
$row = pg_fetch_array($query);
return $row;
}
And this is the second option:
function check($user_id, $password)
{
$conn = pg_connect('host','user','pass','db');
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($conn, $sql);
$row = pg_fetch_array($query);
return $row;
}
According to the PHP manual, You may omit connection resource, but it is not recommended, since it can be the cause of hard to find bugs in scripts.
I've got a table in the database called "favorites" with 3 columns (user_id, bookmarked_song_id, bookmark_tag) and I want to get all the Bookmarked_song_id for the current user.
$username = $this->session->userdata('username');
$uidq = mysql_query('SELECT user_id FROM users WHERE username="' . $username . '"');
$rq = mysql_fetch_assoc($uidq);
$user_id = $rq['user_id'];
$getfavq = mysql_query("SELECT * FROM favorites WHERE user_id=$user_id");
$favsr = mysql_fetch_array($getfavq); //contains all the information from the favorites database where user_id is the user_of the currently logged-in user
And I don't know what to use next... I want to have something like:
foreach($favsr['bookmarked_song_id'] as $song_id) {
$getsongq = mysql_query("SELECT * FROM songs WHERE song_id=$song_id");
$getsongr = mysql_fetch_assoc($getsongq);
$singer = $getsongr['singer'];
$song_name = $getsongr['song_name'];}
Obviously the method is wrong because I get: "Invalid argument supplied for foreach()". Can anyone help me with getting the songs? Thanks in advance.
It should be this:
$favsr = mysql_fetch_array($getfavq, MYSQL_ASSOC);
foreach($favsr as $row) {
$songid = $row['bookmarked_song_id'];
...
}
mysql_fetch_array only loads one row,
it should be like that
$getfavq = mysql_query("SELECT * FROM favorites WHERE user_id=$user_id");
while $favsr = mysql_fetch_array($getfavq);
{$songid=$favsr['bookmarked_song_id'];
$getsongq = mysql_query("SELECT * FROM songs WHERE song_id=$song_id");
$getsongr = mysql_fetch_array($getsongq);
$singer = $getsongr['singer'];
$song_name = $getsongr['song_name'];}
You have this tagged with codeigniter. If you've building a CodeIgniter application, you should probably use CI's database library:
$username = $this->session->userdata('username');
//Select your user
$this->db->select('user_id');
$this->db->where('username', $username);
$this->db->limit(1);
$user_query = $this->db->get('users');
if($user_query->num_rows() > 0)
{
// We found a user
$user = $user_query->row(); // select a single row
// Grab this user's favorites
$this->db->where('user_id', $user->id);
$favorites_query = $this->db->get('favorites');
$songs = $favorites_query->result();
if($songs)
{
foreach($songs as $song)
{
$song_id = $song->bookmarked_song_id;
$tag = $song->bookmark_tag;
// Do stuff with data.
}
}
else
{
// No songs/favorites found, catch error
}
}
else
{
// No such user found, catch error
}
Of course, the best practice is to have your user data and your favorites data in separate models, but this should work for now.