php importing csv into database [closed] - php

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a csv file with 65000 records which i need to import into my database.
I have tried following code but real slow.
Is they anyway I can do this faster.
#set_time_limit(1200);
$file = './csvFiles/aw_Products.csv';
$handle = fopen( $file , 'r');
while (($row = fgetcsv($handle)) !== false) {
if( is_integer($row[0]) || $row[0] != 0 )
{
$product = new Product();
$product->merchant_id = $row[0];
$product->merc_product_id = $row[1];
$product->product_id = $row[2];
$product->product_name = $row[3];
$product->product_desc = htmlentities($row[4]);
//$product->keywords = htmlentities($row[6]);
$product->category_id = $row[5];
$product->link_url = $row[6];
$product->image_url = $row[7];
$product->price = $row[8];
$product->delivery_cost = $row[9];
//$product->deliveryAvailable = $row[12];
//$product->deliveryDetails = $row[13];
//$product->valid_to = $row[14];
//$product->valid_from = ($row[3] == 'yes') ? 1 : 0;
if( Product::find_by_id( $row[0] ) )
$product->updateRecord();
else
$product->createRecord();
}
sleep (1);
}
fclose($handle);

sleep() is probably the culprit. But I am also wondering about this: Product::find_by_id() because it might be doing a SELECT query for every INSERT. You might think about making a single SELECT query to get all of the existing DB keys into a PHP array, then you can use *in_array()* to check whether to UPDATE or INSERT. Probably goes without saying, but you will want to add to the PHP array if you INSERT.

Give up sleep() and use prepared statements for mysql insert/updates.

Related

replace last element in array php [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
here an array
$item = new rssGenerator_item();
$query_rss = mysql_query("SELECT * FROM `trades` WHERE `app` = '1' ORDER BY `id` DESC LIMIT 10 ")OR die(mysql_error());
while($row_rss = mysql_fetch_array($query_rss)){
$item->title = $row_rss['unit'];
$item->description = excerpt($row_rss['message'],30);
$item->link = 'http://injaa.com/'.$row_rss['link'];
$item->guid = $row_rss['site'];
$item->pubDate = RelativeTime($row_rss['tarikh']);
$rss_channel->items[] = $item;
}
$rss_feed = new rssGenerator_rss();
$rss_feed->encoding = 'UTF-8';
$rss_feed->version = '2.0';
header('Content-Type: text/xml');
echo $rss_feed->createFeed($rss_channel);
at this line
$rss_channel->items[] = $item;
all value before this clear and replace last value into array!!
i dont now where is the code is wrong?
Use
array_push($rss_channel->items, $item);
At the top(before while) add
$rss_channel->items = array();
Although #Mr Srinivas answer is better, personally I'd rewrite it like this:
$items = array();
while($row_rss = mysqli_fetch_array($query_rss)){
$items[] = $item;
}
$rss_channel->items = $items;
Admittedly I don't have the knowledge to explain why this is better, this is just how it would make sense to me.

How to hide Partial Data in PHP [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Eperimenting PHP just for fun, But As being newbie, I'm unable to understand curcial parts of PHP....Please help me to sort out this problem which I'm explaining by example :
Suppose
$sql = "SELECT id, text,uid FROM feeds WHERE uid='".$ud."' LIMIT 10";
$items = mysql_query($sql);
echo mysql_error();
if (#mysql_num_rows($items) > 0)
{
while ($item = mysql_fetch_array($items))
{
$feed = $item[1];
$nick = getnick($item[2]);
}
}
So I want to display like this :
3 Records with uid details...
jay,vicky, sumair and 17 others like this.
Please help me to get output of something like this !!
Thanks !!
I can't stretch this enougth,
DO NOT USE MYSQL_* API anymore. [Read this]
It is VULNERABLE, mysqli_* functions are just as similar very little difference.
And You already are doing the things required for that output mysql_num_rows() already gives the number of total result. So:
if (mysql_num_rows($items) > 0)
{
$count = mysql_num_rows($items);
echo $count." Records with uid details..."; //Display the count of records
$threeNameHolder = array; // Hold the first three names on this
while ($item = mysql_fetch_array($items))
{
$feed = $item[1];
$nick = getnick($item[2]);
if(count($threeNameHolder) < 3) {
$threeNameHolder[] = $nick;
} else break; // End the loop here
}
//Now display the name
echo implode(",", $threeNameHolder). " and ".($count - 3)." others like this.";
}
Safer and MYSQLi Version
if (mysqli_num_rows($items) > 0)
{
$count = mysqli_num_rows($items);
echo $count." Records with uid details..."; //Display the count of records
$threeNameHolder = array; // Hold the first three names on this
while ($item = mysqli_fetch_array($items))
{
$feed = $item[1];
$nick = getnick($item[2]);
if(count($threeNameHolder) < 3) {
$threeNameHolder[] = $nick;
} else break; // End the loop here
}
//Now display the name
echo implode(",", $threeNameHolder). " and ".($count - 3)." others like this.";
}
To understand the basics fundaments, I really recommend the official documentation PHP
http://www.php.net/manual/en/ref.mysql.php
A simple sample to execute a query and display the output:
$query = mysql_query("SELECT a, b FROM table_name WHERE c='".$something."' LIMIT 10");
$num_rows = mysql_num_rows($query);
$test = array(); // create a empty array
/* while there is result */
while ($item = mysql_fetch_array($items)){
$columnA = $item[0];// first column (a)
$columnB = $item[1]); // second column (b)
$test[] = $columnB; // push_back a item on array
}
echo $num_rows. " Records with **" . $something . "**...";
echo implode($test, ", ") . "and some text";

appending array in php / CodeIgniter [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am try to append my data array in if conditions. I am using array_push function to do it Its first time I am using this function for appending array. I am using conditions so that if user has added the value in the form it would update the field otherwise It would not effect the field. The problem is its not updating the database and its unable to set the fields as it shows Unknown Unknown column '0' in 'field list'
$fid = 2;
$password = "test_pass";
$title = "new title of folder";
$f_access = 1;
$newName = TRUE;
$data = array(
'name' => $title,
'access_type' => $f_access
);
if($newName)
{
$data2 = "'icon' => $newName";
array_push($data, $data2);
}
if($password)
{
$data3 = "'password' => $password";
array_push($data, $data3);
}
$this->db->where('id', $fid);
$this->db->update('folders', $data);
To insert new data into a key => value array you have to use the form:
$arr['key'] = value;
as
$data['password'] = $password;
$data = array(
'name' => $title,
'access_type' => $f_access
);
if(!empty($newName))
$data['icon'] = $newName;
if(!empty($password))
$data['password'] = $password;
This way you can easily push new items in the array.

php permalink like wordpress permalink [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What i want to do is to create a permalink like system such as the wordpress permalink.
For example:
$value = 'abc';
if($query_result > 0){
$value = 'abc1'; // if exists check abc2, abc3, abc4, abc5, etc. etc.
} else{
return $value
}
The query results is a search for the number of rows $query_result = mysql_num_rows
At this moment i created this:
if(!empty($_POST['ajax'])){
if($_POST['ajax'] == 'pages'){
echo prettyName($_POST['title']);
}
}
function prettyName($string)
{
$echo = strtolower(str_replace(array(' ', ' '), '-', preg_replace('/[^a-zA-Z0-9 s]/', '', trim($string))));
$sql = "SELECT * FROM posts WHERE post_pretty = '".$echo."'";
$res = mysql_query($sql) or die (mysql_error());
$num = mysql_num_rows($res);
if($num){
$echo = $echo.'-'.pretty2($echo, $num);
}
return $echo;
}
function pretty2($echo,$num,$i = 1)
{
$sql = "SELECT * FROM posts WHERE post_pretty = '".$echo.'-'.$i."'";
$res = mysql_query($sql) or die (mysql_error());
$num = mysql_num_rows($res);
if($num){
$i++;
$i = pretty2($echo,$num,$i);
}
return $i;
}
What does it do:
check if $value ($_POST['title']) exists
convert to a prettystring (by example: a b c = a-b-c)
check if prettystring exists else +1 (by example: a-b-c exists, a-b-c-1 - a-b-c-9999)
loop prettystring until free name doesn't exists.
But what goes wrong is:
if i create a new Post for example:
new post 1 name = testpage // results in testpage
new post 2 name = testpage // results in testpage-1
new post 3 name = test-page // results in testpage-2 // should be test-page
if i create a new Post with to much whitespaces in it it also get's wrong what could i use to remove all the white spaces? Tried to user TRIM function but that didn't work. Or should i be using javascript to avoid that?
It will return abc if num rows are 0
$value = 'abc';
$query_result = mysql_query("SELECT * FROM `table`");
$num_result = mysql_num_rows($query_result);
if($num_result){
$value = 'abc1';
}
PS. try to use PDO for your next development. A good Tutorial
for your problem:
$query_result = mysql_query("SELECT `name` FROM `Table1` ORDER BY `name` DESC LIMIT 1");
$num_result = mysql_num_rows($query_result);
if($num_result){
$row = mysql_fetch_assoc($query_result);
$value = (int)str_replace('abc', '', $row['name']);
$value = 'abc'.($value+1);
}

Work out missing row in MySQL [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a table that will eventually get a row missing through deletion of records.
How could I check for that missing row in that table then fill this missing row in PHP.
I guess its using looping however I just cant work out the correct loop to use
Cheers
One way to find a missing item in a sequence is to compare count(*) and max(id).
It should give you an idea of how many are missing, then start including ranges that your checking as in a binary search.
Alternate way is just iterate over the rows ordered by id, and trigger your insert when the sequence jumps.
$lastId = 0;
foreach ($rows as $row)
{
if ($lastId + 1 != $row['id']) {
fillInRows($lastId + 1, $row['id'] -1);
}
$lastId = $row['id'];
}
function fillInRows($min, $max)
{
for ($i = $min; $i <= $max; $i++) {
// exercise left for the reader
}
}

Categories