Codeigniter command line can't inset to database - php

I have this function on 'myproject/controllers/sample.php'
public function callTest()
{
$this->load->database();
$this->db->query("INSERT INTO table_name (client_id, subscription_id, platform, device) VALUES (1, 1, 1, 1)");
$this->load->view('sample');
}
Whenever I run it on the browser (localhost/index.php/sample/callTest), it successfully runs and inserts the data on the table, but when I use command line (php index.php sample callTest), the SQL won't run.
I removed the database part and it shows the code for the view, which I think is correct. But if I put a database logic to it, it won't run and the output is blank. No error logs also.
Am I doing something wrong? Am I missing an argument?
My codeigniter version is 2.2.6.
This is my reference https://www.codeigniter.com/userguide2/general/cli.html
Edited: Aril 17,2017
I just figured out that in some of environment, the command line above works. I suspect that the cause of this was me converting PHP 5.5 to 5.6 in Ubuntu 14, some of the dependencies might not be configured correctly.

You need to specify controller and function name as well,
Example
php index.php/sample/callTest
Also you need to set one config variable as below
$config['uri_protocol'] = 'AUTO'
Read the document answer is hidden there.
Now normally you would visit the your site using a URL similar to
this:
example.com/index.php/tools/message/to

Related

Laravel DB Query Not Working in server but in local host is running well

DB Query was running well but i think there is a content of a file changed during uploading files to update
I use simple query to get Data from data base but didn't work
it Not running after DB query
$symptoms = DB::table('symptoms')->select('MainSymptoms','Symptom')->get();
return json_encode($symptoms);
probably your database config is wrong.
first you need to see the error so go to config/App.php and change "debug" to true like that:
'debug' => 'true'
and then you will be able to see where is the problem and you can solve it

PHP mysqli statement execute() not working

I havn't found something equally on stackoverflow.com, so i post my question in the hope someone have an idea what's wrong with my code or something i'm using.
Before i begin: On my productive webspace the same code works, the problem is located at the xampp-installation for my local development-server... sigh
I have a little webapplication for my family and me to document the work at home (e.g. to empty the bin), so we can distribute money to our kids, depending on how much they done by themself (a little bonus to the pocket money they get without homework)
The code to insert a line into my db-table is:
function AddGarbEntry($DBConnection, $DBPrefix, $UserID, $GarbID)
{
$stmt = $DBConnection->prepare("INSERT INTO t_".$DBPrefix."_entries (idUsers,idGarbType) VALUES (?,?)");
$stmt->bind_param("ii", $UserID, $GarbID);
$stmt->execute();
$stmt->close();
}
The part of the script, where i call this function is:
$con = new mysqli($_SESSION["DBHost"], $_SESSION["DBUsername"], $_SESSION["DBPassword"], $_SESSION["Database"]);
AddGarbEntry($con, $DBPrefix, $_POST["user"], $_POST["garbagetype"]);
$con->close();
I've already checked the Session-Variables (They are all filled with the correct values), and the POST-Values (They are filled correct either).
In my MySQL-Log a can find the following lines:
170423 11:54:38 66 Connect rldmla_1#localhost as anonymous on rldmla_db1
66 Prepare INSERT INTO t_garb_entries (idUsers,idGarbType) VALUES (? , ?)
66 Close stmt
66 Quit
So, i can see, that the prepare is working, but there is no sign, that my testserver runs the execute()-Command. The error-logs of php, apache and mysql have no entry about that.
Curiously, if i'm using a specific function of my website to update already existing lines (also prepared-statement, but an update-statement), my insert code works properly for about two times, after that it stop working until i update my table again.
I've tried the actual version of xampp (7.1.x) and the older version (7.0.15), but the problem is the same. Xampp is running as x86-version on a x64-running Windows Server 2012-Machine. I've only installed the modules Apache, PHP and MySQL.
I don't have any clue, what i'm doing wrong. Any help is appreciated.
Greetings, Ronny
Urks.... Now I've got the clue:
I changed my Xampp-Installation to a linux based virtual machine, where i've installed Apache, PHP, MySQL and phpMyAdmin. After that the missing execute-line was in the MySql-Logfile.
So, nothing to do here anymore.
Thx #all for supporting me :)
Greetings, Ronny

Get windows title using php doesn't work on browser call

My problem is I need to fetch FOOBAR2000's title because that including information of playing file, so I create a execute file via Win32 API(GetWindowText(), EnumWindows()) and it's working good.
TCHAR SearchText[MAX_LOADSTRING] = _T("foobar2000");
BOOL CALLBACK WorkerProc(HWND hwnd, LPARAM lParam)
{
TCHAR buffer[MAX_TITLESTRING];
GetWindowText(hwnd, buffer, MAX_TITLESTRING);
if(_tcsstr(buffer, SearchText))
{
// find it output something
}
return TRUE;
}
EnumWindows(WorkerProc, NULL);
Output would look like "album artis title .... [foobar2000 v1.1.5]"
I created a php file like test.php, and use exec() to execute it.
exec("foobar.exe");
then in console(cmd) I use command to execute it
php test.php
It's working good too, same output like before.
Now I use browser(firefox) to call this php file(test.php), strange things happened.
The output only foobar2000 v1.1.5, others information gone ...
I think maybe is exec() problem? priority or some limitation, so I use C# to create a COM Object and register it, and rewrite php code
$mydll = new COM("FOOBAR_COMObject.FOOBAR_Class");
echo $mydll->GetFooBarTitle();
still same result, command line OK, but browser Fail.
My question is
Why have 2 different output between command line and browser. I can't figure it out.
How can I get correct output via browser.
or there is a easy way to fetch FOOBAR2000's title?
Does anyone have experience on this problem?
== 2012/11/28 edited ==
follow Enno's opinion, I modify http_control plug-in to add filename info, original json info is "track title".
modify as following
state.cpp line 380 add 1 line
+pb_helper1 = pfc::string_filename(pb_item_ptr->get_path());
pb_helper1x = xml_friendly_string(pb_helper1);
# 1: when firefox opens the php and it gets executed, it the context depends on the user which runs the php-container (apache), this is quite different from the commandline call which gets executed in your context
# 2 and 3: there seems to be more than one way for getting the title: use the foobar-sdk and create a module which simply reads the current title per api, then write your result in an static-html-document inside your http-root-folder OR use the http-client inside the sdk, with it, you do not need a wabserver, even better use a already implemented module: for instance foo_upnp or foo-httpcontrol
Good luck!
If your webserver runs as a service, in windows you need to enable "allow desktop interaction" for the service. Your php script runs as a child of the webserver process when requested via browser.

Empty database output in CI

I'm building a simple app and trying to test DB result output. But unfortunately all I'm getting is an array of size 0. Here's the controller code excerpt:
$data['query'] = $this->db->query('SELECT role_id, role_privilege FROM role');
$this->load->view('welcome_message', $data);
And a view code excerpt:
<?php
echo count($query->result_array())."<br/>";
foreach ($query->result() as $row){
echo $row->role_id . '<br/>';
echo $row->role_privilege . '<br/>';
}
echo 'Total result '.$query->num_rows();
?>
And what I get is next:
0
Total result
Running query from a command line gives a 2 rowed output. Can someone point out what i'm missing?
EDITED:
This issue is also discussed here .
EDITED:
Maybe some platform specific stuff (I really doubt that)? I'm running LAMP (php 5.3.2, mysql 5.1.37, apache 2.2.15).
EDITED:
This prints out a "Array ( )" string. My DB is 100% filled. I can say that for sure, because I did
INSERT INTO role(role_privilege) VALUES ('ROLE_MODERATOR');
INSERT INTO role(role_privilege) VALUES ('ROLE_USER');
and then checked it through a command line.
EDITED:
After I put this into my controller:
echo $this->db->last_query(); exit;
I get next output:
SELECT role_id, role_privilege FROM role
And that's exact sql query that I needed. Unfortunately results are o sized array.
Well, this here problem is basically not a CI related one, but strange thing is that CI couldn't track this error. Here's what was going on:
While installing php I haven't specified --with-mysql-socket parameter and it looks like php tried to use a /tmp/mysql.sock (default one) which obviously was not specified in my.cnf. So CI tried to bind to a nonexistent socket. Changing mysql params in a php.ini solved the problem.
Well, it sounds like your db access is set up properly because you're not getting an error. Which I think also means that the table exists. I know you said you ran that query from the command line, but are you certain you're accessing the same data set/server/table? No weird versioning or anything? It sounds like there aren't records in the role table. Sorry, just grasping at straws as your code looks right.
How about doing
SELECT * FROM role
and then doing a
print_r($query->result_array());
in your view? That should reveal column names at least so you know you're accessing the right data.
Try that and report back with any detail and hopefully it will supply some hints.

SQL debugging in large PHP app

I am using CodeCharge Studio to finish a large PHP application. This question isn't really CCS related, but a bit more general. I have a web form that is supposed to allow CRUD capabilities with a certain SQL Server table, but the Inserts keep failing without throwing any errors. What would be the best way to debug this?
When I'm having trouble with dynamically generated SQL queries, I typically echo out the query and try running that query on the console for the DB. Or alternatively, you could write a simple PHP function that writes out strings to a file, and that way you don't have to display the query directly on your page, but instead in a log file.
See what the actually query is and then try doing that query directly on the DB. Then you know whether it's a PHP issue or a DB issue.
Then go from there, depending on the outcome.
If the query looks OK, double check that the user running the query has insert rights to the database.
I've been caught out by that before.
You can monitor all sql queries in mysql as shown in this site, once you enable logging, run the query manually and see why its failing..this should be good starting point.
In addition to what's mentioned before, I can add my recent discovery:
trigger_error(print_r($your_var,1),E_USER_ERROR);
So you can output and debug your variable, even if it's a complex script with redirects, where simple echo would not help.
Dmitri.
You should try using FirePHP and log all the SQL to your Firebug:
An Example would be:
$sql = "SELECT * FROM table"
if (!mysql_query($sql)) {
// In un successfull log to FireBug
FB::error($data, "SQL error: ".mysql_error());
}
You can also implement the FB::error call from your own function, so you can later deactivate this behaviour modifying your function:
function log_error($data, $msg){
//dont forget to deactivate in case the application goes live!
FB::error($data, $msg);
}
if (!mysql_query($sql)) {
// In un successfull log to FireBug
log_error($data, "SQL error: ".mysql_error());
}
Most of the database connection classes in CodeCharge have a 'debug' flag which will automatically write all the page's database commands at the top of the page.
For example, in an old PHP project of mine, 'Common Files' the file 'db_mysql.php' (line 27):
public $Debug = 0; ## Set to 1 for debugging messages.
Change to '1' and publish that file. Load the web page. Change back and re-publish when done.
I've used this in CCS for PHP and ASP projects, and is likely in the other languages (not sure if or where to find in .NET projects).

Categories