I'm trying to get data from my data base and load it into an existing csv file i get no errors but each time i check with the file it stills empty.
this is my PHP file :
<?
$server="XXXX";
$user="XXXX";
$password="XXXX";
$db="XXXX";
mysql_connect($server,$user,$password) or die('erreur au serveur');
mysql_select_db($db) or die('erreur db');
if ($file=fopen('XXXX/toERP/exportfactures.csv','w')!=FALSE){
$req='select * from facture';
$sql = mysql_query($req);
$cpt=1;
$response["factures"] = array();
$output=fopen('http://XXXXX/toERP/exportfactures.csv','w');
if (mysql_num_rows($sql) > 0) {
// looping through all results
// factures node
while ($row = mysql_fetch_array($sql)) {
// temp user array
$facture = array();
$facture["id_fac"] = $row["id_fac"];
$facture["id_client"] = $row["id_client"];
$facture["id_agent"] = $row["id_agent"];
$facture["montant"] = $row["montant"];
$facture["type_paiement"] = $row["type_paiement"];
$facture["id_prod"] = $row["id_prod"];
$facture["date_vente"] = $row["date_vente"];
// push single client into final response array
array_push($response["factures"], $facture);
}
foreach ($response["factures"] as $ligne){
var_dump($ligne);
fputcsv($output,$ligne) or die ('erreur');
echo $cpt."inserted";
$cpt++;
}
fclose($file);
} else {
echo "file not found";
}
}
?>
You are unable to write to an http location try changing it to a local directory.
fopen('\XXXX\toERP\factures.csv','w')
or just change the save location
$output = fopen('\XXXX\toERP\factures.csv','w');
fputcsv($output, explode(';',$ligne));
You are trying to open a file from remote server due to url example your have wrote. This is your most possible issue if you see the lines inserted by echo.
Here is the definition for fputcsv : http://php.net/manual/en/function.fputcsv.php
As the second argument, you need to provide an array with fields.
$ligne in your case, is an array with one element: ['factures'] which is another array, so you need to change the foreach to match the form below :
foreach ($response['factures'] as $ligne)
Also, as others mentioned: you need to select a local resource for the filename, and make sure you have the right to write on it.
this code worked for me :
<?
$server="mysql12.000webhost.com";
$user="a2258793_star";
$password="star123";
$db="a2258793_stores";
$con = mysql_connect($server,$user,$password) or die('erreur au serveur');
mysql_select_db($db) or die('erreur db');
mysql_select_db($db);
$file='toERP/factures.csv';
$fp = fopen($file,"w");
$sql = mysql_query("select * from facture") or die (mysql_error);
mysql_data_seek($sql,0);
while ($row = mysql_fetch_assoc($sql)){
$sep = "";
$comma = "";
foreach ($row as $name => $value)
{
$sep.=$comma.''.str_replace('','""',$value);
$comma =";";
}
$sep.="\n";
fputs($fp,$sep);
}
fclose($fp);
?>
Related
I want to send database records with a PHPH file via json to my app I am making with IntelXDK. Because I can't use PHP code with the Intel XDK, I needed to use JSON. I want to show the two records 'quote' and 'author' from my 'quotes' table on my screen. Someone helped me to this code but it just returns [null,null]instead of the two records I need.. I tried debugging but I am new to PHP so I can'get it to work.. Anyone who can help or sees an error in this code? Thanks!
PS: Yes I now there are already multiple questions asked on this subject by other people. I have read them all but none of them solves my question..
<?php
if(isset($_GET["get_rows"]))
{
//checks the format client wants
if($_GET["get_rows"] == "json")
{
$link = mysqli_connect("localhost", "xxxxx", "xxxxx", "xxxx");
/* check connection */
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
header("HTTP/1.0 500 Internal Server Error");
exit();
}
$query = "SELECT quote, author FROM quotes WHERE id = " . date('d');
$jsonData = array();
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
$row = $result->fetch_assoc($result);
// Create a new array and assign the column values to it
// You can either turn an associative array or basic array
$ret= array();
$ret[] = $row['quote'];
$ret[] = $row['author'];
//encode to JSON format
echo json_encode($ret);
}
else {
echo json_encode($ret);
}
/* close connection */
mysqli_close($link);
}
else
{
header("HTTP/1.0 404 Not Found");
}
}
else
{
header("HTTP/1.0 404 Not Found");
}
?>
You have a bug in fetch_assoc() function call - remove $result parameter. If you had error reporting enabling, you should see:
Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given
Just change it to:
$row = $result->fetch_assoc();
In javascript to parse this response, just do this:
var obj = JSON.parse(xmlhttp.responseText);
document.getElementById("quote").innerHTML = obj[0];
document.getElementById("author").innerHTML = obj[1];
I think your problem is with fetch_assoc()
Try to use that :
$row = mysqli_fetch_assoc($result);
instead of
$row = $result->fetch_assoc($result);
It's works for me with your example
change this
$row = $result->fetch_assoc($result);
to
$row = $result->fetch_assoc();
Just change it to:
$row = $result->fetch_assoc();
Updated:
response = JSON.parse(xmlhttp.responseText);
you can now access them independently as:
reponse.quote and response.author
Hey guys I have stored few xml files in database and want to get them for xslt transformation. But I am unable to load it for transformation, they get downloaded to PC. I want to process it instead of download.
here is my code: I am accessing the file with its id.
// Fetch the file information
$query = "
SELECT `pname`,`pdata`
FROM `plist`
WHERE `pid` = {$id}";
$result = $dbLink->query($query);
if ($result) {
// Make sure the result is valid
if ($result->num_rows == 1) {
// Get the row
$row = mysqli_fetch_row($result);
echo "$row[0]";
header("Content-Type:text/xml");
header("Content-Disposition: inline");
echo $row['pdata'];
}
mysqli_free_result($result);
---missing something here to get file data--
until now it gets downloaded but i want to continue the transformation
$xml = new DOMDocument;
$xml->load(what goes here??);
$xsl = new DOMDocument;
$xsl->load('file.xslt');
//etc
any help appreciated.
If you have a string with the XML then you can use $xml->loadXML($row['pdata']) I think to parse the XML.
Working solution that i found later enjoy..:)
<?php
if (isset($_GET['id'])) {
// Get the id
$id = intval($_GET['id']);
// Make sure the name is in fact a valid
if ($id <= 0) {
die('The id is invalid!');
} else {
// Connect to the database
include 'config.php';
}
// Fetch the file information
$query = "select id, name, data from data_table where id ={$id};";
$resultx = $mysqli->query($query) or die($mysqli->error . __LINE__);
$arr = array();
if ($resultx->num_rows > 0) {
while ($row = $resultx->fetch_assoc()) {
$arr[] = $row['tdata'];
}
}
$string = implode(",", $arr);
// Load XML file
$xml = new DOMDocument; // create new document
$xml->loadXML($string); // load string to it
From below PHP web services code I am able to get the link from first to last which is saved on server database.....
Now I want to get the saved link from last to first.....
<?php
$con=mysqli_connect("XX.XX.XX.X:XXXX","root","root","db_");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM images");
$images = array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$images[] = $post;
}
}
header('Content-type: application/json');
echo json_encode(array('images'=>$images));
mysqli_close($con);
?>
The output ob above code is:
{"images":[{"image":"http://demo.in/vk/vehicles/v_1.jpg"},{"image":"http://demo.in/vk/vehicles/v_2.jpg"},{"image":"http://demo.in/vk/vehicles/v_3.jpg"},{"image":"http://demo.in/vk/vehicles/v_4.jpg"}]}
but I want It to be come like this:
{"images":[{"image":"http://demo.in/vk/vehicles/v_4.jpg"},{"image":"http://demo.in/vk/vehicles/v_3.jpg"},{"image":"http://demo.in/vk/vehicles/v_2.jpg"},{"image":"http://demo.in/vk/vehicles/v_1.jpg"}]}
Change your query to this
$result = mysqli_query($con,"SELECT * FROM images ORDER BY DESC");
I've found an existing script on the internet that grabs data from WHOIS servers and extracts the relevent data (Eg. Expiry date, Status). As it was long abandoned I have been modifying it and created my own script.php?id=domain.com which lets me enter any domain and whois data comes up,
What I'm having trouble with is I have my whois.php file which I want to grab a list of domains out of my MySQL database and attempt to extract the Expiry/Status data of each domain using (file_get_contents to my script.php and foreach) then update the database with the relevant information.
I'm fairly certain I have coded everything apart from the "Foreach" & "File_get_contents" part right therefore my script encounters errors.
"Warning: Invalid argument supplied for foreach() in /home/user/public_html/mydomain.com/domains/whois.php on line 39"
is the error I receive.
Snippet of my whois.php:
include("database.php");
// Select one domain from the database that hasn't been checked yet
$sql = "SELECT domainName from domains WHERE 1 ORDER BY lastChecked ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$domain = $row[0];
if(mysql_num_rows($result) == 0){
die("No domains found in the database.");
}
// Grab the WHOIS information for the domain selected
// ---------------------------------------------------------------
$domainExt = substr($domain, -3); // grab the domain extension
//var_dump($whois);
$arr = array($content);
foreach($arr as $id) {
echo $id, '<br>';
$data = file_get_contents('http://codestrike.net/domains/script.php?id='.$domain.'');
echo $data, '<br>';
}
foreach($data as $whoisline){
if(strstr($whoisline,"Expiration")){
$whoisline = str_replace("Expire Date:","",$whoisline);
$whoisline = trim($whoisline);
$expiration = substr($whoisline,0,11);
}
if(strstr($whoisline,"Status")){
$statusline = $whoisline;
}
}
$status = str_replace("Status:","",$statusline);
$status = trim($status);
Script.php?id=domain.com works fine, its just a matter of having the whois.php finding the expiry date/status for each domain out of my MySQL Database.
Cheers.
Change:
$data = file_get_contents('http://mydomain.com/domains/script.php?id='.$domain.'');
to:
$data = file('http://mydomain.com/domains/script.php?id='.$domain);
file_get_contents returns the entire file as a single string. file splits it up into an array, where each element is a line of the file.
You also need to process $data in the first foreach loop. Otherwise, you're just overwriting $data each time through the loop, and the code that uses it just gets the last one.
include("database.php");
// Select one domain from the database that hasn't been checked yet
$sql = "SELECT domainName from domains WHERE 1 ORDER BY lastChecked ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$domain = $row[0];
if(mysql_num_rows($result) == 0){
die("No domains found in the database.");
}
// Grab the WHOIS information for the domain selected
// ---------------------------------------------------------------
$domainExt = substr($domain, -3); // grab the domain extension
//var_dump($whois);
$arr = array($content);
foreach($arr as $id) {
echo $id, '<br>';
$data = file('http://mydomain.com/domains/script.php?id='.$domain);
var_dump($data); echo '<br>';
foreach($data as $whoisline){
if(strstr($whoisline,"Expiration")){
$whoisline = str_replace("Expire Date:","",$whoisline);
$whoisline = trim($whoisline);
$expiration = substr($whoisline,0,11);
}
if(strstr($whoisline,"Status")){
$statusline = $whoisline;
}
}
$status = str_replace("Status:","",$statusline);
$status = trim($status);
}
Trying to: get all users by using the query in the below script into an array
Looking to get output like this:
$existing_users=array('value','value','value', 'value'...)
Php Script:
<?php
require_once('../../../inc/db/dbc.php');
$connect = mysql_connect($h, $u, $p) or die ("Cant Connect to Database.");
mysql_select_db($db);
$q = mysql_query("SELECT uUName FROM User");
$existing_users=array(
while ($row = mysql_fetch_array($q, MYSQL_NUM)) {
echo " '$row'.','";
}
);
// $existing_users=array('joe','warren','tim');
# ^^^^ manual way of doing it
//value got from the get metho
$user_name=$_POST['user_name'];
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
//user name is not availble
echo "no";
}
else
{
//user name is available
echo "yes";
}
?>
How do I accomplish this? I know my while/array formation is a bit off there, how do I fix this?
Thanks
$existing_users = Array();
while(list($username) = mysql_fetch_row($q)) $existing_users[] = $username;
mysql_fetch_row($q) will give you Array(0 => result:uUName)
so list($xxx) = mysql_fetch_row($q) - $xxx is first element of Array(0 => result:uUName) equals your result:uUName.