I am running the following piece of code to dump a csv into a database table and I have followed the instructions here to make sure that I am doing it the right way but I keep on getting the following error even though my file exists
File 'http:/localhost/dashboardapp/uploads/admin.csv' not found (Errcode: 2)
$this->db->truncate($this->table_name);
$query_name = "LOAD DATA LOCAL INFILE '"
. $file_path .
"' INTO TABLE `"
. $this->table_name .
"` FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\n'
IGNORE 1 LINES
(#mytimestamp,curr_property,curr_property_cost,day_property,day_property_cost,curr_solar_generating,curr_solar_export,day_solar_generated,day_solar_export,curr_chan1,curr_chan2,curr_chan3,day_chan1,day_chan2,day_chan3)
SET time_stamp=STR_TO_DATE(#mytimestamp, '%d/%m/%Y %h:%i')";
$link = mysqli_init();
if (!$link) {
die('mysqli_init failed');
}
if (!mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true)) {
die('Setting MYSQLI_OPT_LOCAL_INFILE failed');
}
if (!mysqli_real_connect($link, 'localhost', 'root', 'rolemodel', 'dashboard')) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
$result = $link->query($query_name);
if (!$result) {
printf("%s\n", mysqli_error($link));
}
I am not really sure on how to proceed as i have looked for resources but havent found any yet
Thanks for the comments guys, the problem was with the path as mentioned, I used codeigniter so I used FCPATH variable and it worked.
Related
I am trying to create an php script which can read and array list from android and read it's element and store it in data base, now I have been able to store the data in an text file but unable to proceed further, could some body help me out in this matter?
this is the php script which I have used:-
<?php
$i=0;
$password="";
$user="root";
$database="shadowpets";
$host="localhost";
$response=array();
$con=mysqli_connect($host,$user,$password,$database)or die("Unable to connect");
if($_SERVER["REQUEST_METHOD"]=="POST"){
if(isset($_POST['OrderSummary'])){
$data=$_POST['OrderSummary'];
$file='text.txt';
$result=file_put_contents($file,$data);
$response["success"]=1;
$response["message"]="done";
}else{
$response["success"]=0;
$response["message"]="parameters not correctly formatted";
}
echo json_encode($response);
}
?>
what changes I can do here, so I can read the data store it in the database?
First, you need to set up a database and a table in a database server (MySQL).
Then you can create an insert statement like below, bind its parameters and execute it.
if (!($statement = $con->prepare('INSERT INTO `your_table` (`OrderSummary`) VALUES (?)'))) {
die('Could not prepare statement: (' . $con->errno . ') ' . $con->error);
}
if (!$statement->bind_param('s', $_POST['OrderSummary'])) {
die('Could not bind parameter: (' . $statement->errno . ') ' . $statement->error);
}
if (!$statement->execute()) {
die('Could not execute statement: (' . $statement->errno . ') ' . $statement->error);
}
I am brand new in PHP / MySQL.
I got this code from
http://www.johnmorrisonline.com/how-to-insert-form-data-into-a-mysql-database-using-php/
(great tutorial by the way) and used it. The problem is that it runs smoothly (Got no errors) but it just won't add the new row to the database. I tried to simplify the code to figure it out. If i run the query on PHPMyAdmin it works ok (it adds a new row) but from browser (php script) won't.
I cant tell what's wrong.
I am using MAMP on my MBP computer.
Thanks so much
Rafa
Here's the code:
<?php
$mysqli = mysqli_init();
if (!$mysqli) {
die('mysqli_init failed');
}
if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!$mysqli->real_connect('localhost', 'root', 'root', 'procedimientos')) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . $mysqli->host_info . "\n";
// Insert our data
$sql = "INSERT INTO PROCEDIMIENTOS (`RUT`) VALUES ('1587');";
$insert = $mysqli->query($sql);
// Print response from MySQL
if ( $insert ) {
echo "Success!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
?>
You have to add $mysqli->commit(); before $mysqli->close(); to persist your changes to the DB.
You have this on top of your code:
if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
In other words, you're wrapping everything you do against MySQL server inside a transaction.
Your options are:
Do not start transactions automatically by default (and do it manually whenever you actually need them)
Commit the transaction on success
I am using function based database connection and select query also plz help me.
FIRST CODE:
function connectDb(){
//my postgresql db connection
$link=pg_pconnect('host=' . __DB_HOST__ . ' port=' . __DB_PORT__ . ' dbname=' . __DB_NAME__ . ' user=' . __DB_USER__ . ' password=' . __DB_PWD__) or die('connection failed');
AllDelete();// Audit Logs Auto Deletion
}
SECOND CODE:
function AllDelete(){
$MysqlForLogs=pg_query("select audit.*,users.username from audit,users where audit.user_id=users.id and DATE_PART('day',now()-audit.date) > 7") or die(pg_last_error());
$CountRows=pg_num_rows($MysqlForLogs);
}
I am executing the browser it will show on error like 'No database selected' but really i am giving the database name.
What is the problem?
I have been banging my head against a wall for hours now, reading several answers on SO, searching the 'net, trying to find a solution. While I realize similar questions have been asked and answered here, my code is different so I can't extrapolate from previous answers what's wrong with mine.
I'm new to PHP/MySQL. I have a poll script that I patched together using some code I found online and my own knowledge; the MySQL is mine, standard stuff. Each time a person votes for one of two things, its corresponding value in the database in increased by one. I'm trying to retrieve and assign each value to its own variable and perform a calculation. I can retrieve the first one just fine, but I get the dreaded
Trying to get property of non-object in..." error and
"Fatal error: Call to a member function bind_param() on a non-object in...
for the second one. It's exactly the same as the first prepare, so I'm not sure what's wrong? This may not be the most efficient way to do this, but like I said, I'm new to this. I DO understand what the error is trying to convey; I just don't understand why I'm getting the error when the prepare statement is identical to the one before it except for the id in the WHERE clause. var_dump($query2) returns bool(false), so I can clearly see nothing is there. But there IS something in the database.
<?php
// Turn on error reporting
ini_set('display_errors', 'On');
// Connect to database
$mysqli = mysqli_connect('localhost', 'dbuser', 'dbpwd', 'dbname');
// Check connection
if (!$mysqli || $mysqli->connect_errno)
{
echo "Connection error: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
exit();
} else if (isset($action)) // This is set through a page redirect
{
// DISPLAY ITEM - THIS WORKS FINE
if (!($query = $mysqli->prepare("SELECT votes FROM influential WHERE id = 1")))
{
echo "Prepare failed: " . $query->errno . " " . $query->error;
}
if (!$query->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if (!$query->bind_result($infl1))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if ($query->fetch())
{
echo $infl1;
}
// DISPLAY ITEM - THIS DOESN'T WORK
if (!($query2 = $mysqli->prepare("SELECT votes FROM influential WHERE id = 2")))
{
echo "Prepare failed: " . $query2->errno . " " . $query2->error;
} else echo "Prepare succeeded.";
$query2->bind_param("i", $votes);
if (!$query2->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
} else echo "Execute succeeded.";
if (!$query2->bind_result($infl2))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
} else echo "Bind succeeded.";
if ($query2->fetch())
{
echo $infl2;
}
exit();
$mysqli->close();
}
?>
In mysqli, when you issue the command:
.bind_param()
you are binding parameters to placeholders in the original query, not in the result. Your select query should read:
SELECT `votes` FROM `influential` WHERE `id`=?;
Then, you use bind_param to bind an integer to the placeholder, thusly:
.bind_param('i', $id);
After calling .execute(), the result is bound using references by calling .bind_result(). Whatever variable you pass into .bind_result() will contain the data afterwards.
Cheers
I just referenced this answer and what I preferred was very first solution, now the issue is he has given an information for mysql_() but am using mysqli_(), so using 4th parameter as true, I select the database when user logs in, the moment he logs in he gets redirected to respective page but it is showing that connection was actively refused. any Idea how I can use 2 database, 1 is my default engine database which I need to keep it on for running my framework and second database to run respective scripts according to the user logged in...
What am trying is this
<?php
$database_connect = mysqli_connect('localhost', 'root', '', 'engine');
if(!$database_connect) {
die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
if(isset($_SESSION['system_id'])) {
$system_database = mysqli_connect('localhost', 'root', '', $_SESSION['system_name'], true);
if(!$system_database) {
die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
}
?>
P.S I want a procedural way
You don't need any extra parameters, simply do it like this
<?php
$database_connect = mysqli_connect('localhost', 'root', '', 'engine');
if(!$database_connect) {
die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
if(isset($_SESSION['system_id'])) {
$system_database = mysqli_connect('localhost', 'root', '', $_SESSION['system_name']);
if(!$system_database) {
die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
}
//Now whatever query you write, just do it like this
$access = mysqli_query($database_connection_var, "/*Query Goes Here*/"); //Will get database name from $database_connection_var
$access2 = mysqli_query($database_connection_var2, "/*Query Goes Here*/"); //Will get database name from $database_connection_var2
?>