Import csv from Yahoo Finance into MySQL using PHP - php

Using Johnboy's tutorial on importing .csv files to MySQL, I tried to make a script that would take exchange rate data from Yahoo finance, and write it to the MySQL database.
<?php
//connect to the database
$host = "****"
$user = "****"
$password = "****"
$database = "****"
$connect = mysql_connect($host,$user,$password);
mysql_select_db($database,$connect);
//select the table
if ($_FILES[csv][size] > 0) {
//get the csv file
$symbol = "ZARINR"
$tag = "l1"
$file = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=$tag&s='$symbol'=x";
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO ZAR_to_INR(exchange_rate) VALUES
(
'".addslashes($data[0])."',
)
");
echo "done";
}
} while ($data = fgetcsv($handle,1000,",","'"));
//redirect
header('Location: import.php?success=1'); die;
}
else{
echo "nope";
}
?>
I added the echos in the hope that they'd tell me whether or not the script worked. It doesn't work at all. There are no error messages or anything. When I run the script by opening it in my webhost, it simply does not run.
I'd appreciate any advice on how to make this script work (or even an alternate way of solving the problem).

try using mysql debugs :
mysql_select_db($database) or die('Cant connect to database');
$result = mysql_query($query) or die('query fail : ' . mysql_error());
$connect = mysql_connect($host,$user,$password)
or die('Cant connect to server: ' . mysql_error());
to find this outputs you need to check your php error_log : where-does-php-store-the-error-log

Related

My Sql connect to database not showing anything in browser

When I was trying to make website for school project then I make a registration page so I make a html page and a php and a database.
But when I tried to enter anything in form then the result after submitting the information is blank page. When I opened php file in browser then a blank page opened. There should be either connected to the database or failed to connect to database. My coding of php file is given below:
Please help me as less time is remaining for last date of submission.
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'practice');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());
function NewUser()
{
$fullname = $_POST['name'];
$userName = $_POST['user'];
$email = $_POST['email'];
$password = $_POST['pass'];
$query = "INSERT INTO websiteusers(fullname,userName,email,pass) VALUES ('$fullname','$userName','$email','$password')";
$data = mysql_query($query) or die(mysql_error());
if ($data) {
echo "YOUR REGISTRATION IS COMPLETED...";
}
}
function SignUp()
{
if (!empty($_POST['user'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM websiteusers WHERE userName='$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());
if (!$row = mysql_fetch_array($query) or die(mysql_error())) {
newuser();
} else {
echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
}
}
}
if (isset($_POST['submit'])) {
SignUp();
}
?>
MySQL is depreciated. Use MySQli instead
Connect to your database this way
$connect = mysqli_query(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
then check this way
if($connect){
echo "Connected";
}else{
echo "Not connected";
}
and any query you run should be in this format
$query = mysqli_query($connect,$Sql);
I tried many source codes from google but I am unsuccessful everytime. Everytime I clicked on submit button it shows a blank page. Please provide me help I want to create a signup page and in this I want to connect html coding to mysql database I have knowledge about html. So please provide an easy way to interconnect the database and html.
I also came to know about the php so I tried to copy source code from google but everytime I failed as php preview in browser is blank page.

code works perfectly in server one but not in server 2

I am posting certain values from front end (android) to back end (php) and this webservice code works well in server one, but for some unknown reason its not working in server 2.
What I found is mysql_query is not working and data is not inputing to the table.
$result = mysql_query("INSERT INTO tbl_booking(booking_name,booking_address,booking_date,booking_phno,description)
Here is the full code
<?php
$con = $con = mysql_connect("localhost","touchlive","touchlive");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("raora", $con);
$response ="FALSE";
$booking_name = $_POST['name'];
$booking_address =$_POST['address'];
$booking_date = $_POST['date'];
$booking_phno =$_POST['phno'];
$description =$_POST['description'];
$result = mysql_query("INSERT INTO tbl_booking(booking_name,booking_address,booking_date,booking_phno,description)
VALUES ('$booking_name','$booking_address','$booking_date', '$booking_phno', '$description')");
if(result ==0)
$response ="TRUE";
echo $response;
mysql_close($con);
?>
your code has
if(result == 0)
where is ' $ '
if($result == 0) like this
and give ur db connection like this
$host="your host";
$port=1234;
$socket="/tmp/mysql.sock";
$user="root";
$password="123456789";
$dbname="dbname";
$con = mysqli_connect($host, $user, $password, $dbname, $port, $socket);
change all above variables as per your details
socket can be empty .

Unable to Store Base64 Data in MySql Database

I am trying to store an Base64 string to my MySql database using PHP. Here is my code.
<?PHP
//Store Image
$photoData = $_POST['PhotoData'];
//Database connection details
$db_url = "192.168.1.140";
$db_user_name = "easyblogadmin";
$db_password = "admin";
$db_name = "easyblog";
$con = mysqli_connect($db_url, $db_user_name, $db_password);
$sql = "insert into temptable (photodata) values('$photoData')";
if(mysqli_connect_errno()){
echo "Failed to Connect Database";
echo mysqli_connect_errno();
die();
} else {
//Connecting to Application Database
$db_selected = mysqli_select_db($con, $db_name);
if(!$db_selected){
echo "Failed to Connect Application Database";
die();
} else {
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
echo "Successfully Added photo".strlen($photoData);
}
}?>
Here the $photoData is my Base64 encoded string and I am storing this in my DB. When i am trying to retrieve this data from DB some data in my string is missing. I don't understand why this is happening. Can any one help me out.
regards.
chnage the sql query as below mentioned
$sql = "insert into databasename.temptable (photodata) values('$photoData')";
also please make sure if the encoded value has any single quotes,if it is then replace the data as mentioned below .
$photoData =str_replace("'","\'",$_POST['PhotoData']);

SQL Server Database Query with PHP

I need to get some data from a Microsoft SQL Server database at work. When I have the data I need, I need to make an Excel spreadsheet that can be saved locally on my computer.
I found PHPExcel which seems to do the job on the Excel part, but what about getting the data from the Database?
I can't seem to find anything that's recent. Only old tutorials.
Use this way to Fetch the Records :
<?php
$hostname = "192.168.3.50";
$username = "sa";
$password = "123456";
$dbName = "yourdb";
MSSQL_CONNECT($hostname,$username,$password) or DIE("DATABASE FAILED TO RESPOND.");
mssql_select_db($dbName) or DIE("Database unavailable");
$query = "SELECT * FROM dbo.table";
$result = mssql_query( $query );
for ($i = 0; $i < mssql_num_rows( $result ); ++$i)
{
$line = mssql_fetch_row($result);
print( "$line[0] - $line[1]\n");
}
?>
This will fetch each rows from the Data Retrieve and Print on the Page. Use your Required format into that. I mean, Use html Table to show the data in well format.
Use this code to get an data from Database.
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '192.168.3.50';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'sa');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
else{
echo "connected ";
mssql_select_db('Matrix') or die("Wrong DATAbase");
//mssql_query("SELECT Seq_no from dbo.Trans_R WHERE Seq_no = 000001",$link) or die("cannot execute the query");
$query = mssql_query("SELECT Tr_Date,Tr_Time,Tr_Data from Matrix.dbo.Trans_R");
$f = mssql_fetch_array($query);
echo $f['Tr_Date'];
}
?>
Can i know why Negative Vote??
He asked me to :
" but what about getting the data from the Database?"

upload text file each line into mysql row

I have email.txt stored in my server the path of email.txt is
/home/xxxx/public_htnl/email.txt
email.txt consists of several thousand emails of subscriber which gets updated daily.
I want to import these emails to other database daily using cron.
I have tried this but its giving error
Could not load. Access denied for user 'test'#'localhost' (using password: YES)
I think I don't have permission to run LOAD DATA INFILE
My code is:
<?php
$db = mysql_connect('localhost', 'test', 'test')
or die('Failed to connect');
mysql_select_db('test', $db);
$string = file_get_contents("http://www.xyz.com/email.txt", "r");
$myFile = "/home/xxx/public_html/email.txt";
$fh = fopen($myFile, 'w') or die("Could not open: " . mysql_error());
fwrite($fh, $string);
fclose($fh);
$result = mysql_query("LOAD DATA INFILE '$myFile'" .
" INTO TABLE email");
if (!$result) {
die("Could not load. " . mysql_error());
}
?>
Any other good method can be accepted.please don't advice that store data directly in other database while storing emails etc..
As the root MySQL user, try running the query:
GRANT FILE on *.* to 'test'#'localhost';
If you still get the error, make sure your username and password for the test user are correct and that the host you are accessing MySQL from matches the host for the test user and password.
EDIT:
<?php
$db = mysql_connect('localhost', 'test', 'test') or die(mysql_error());
if (!mysql_select_db('test', $db)) die(mysql_error());
$emails = file("http://www.xyz.com/email.txt");
foreach($emails as $email) {
$email = trim($email);
if ($email == '' || strpos($email, '#') === false) continue;
$email = mysql_real_escape_string($email);
$query = "INSERT INTO `email` VALUES('$email')";
$result = mysql_query($query);
if (!$result) {
echo "Failed to insert $email. " . mysql_error() . "<br />\n";
}
}
If there are many, many emails, you can build an array of email addresses and do an extended insert every so often.
INSERT INTO emails VALUES('email1'), ('email2'), ('email3'), ('email4')

Categories