I have this PHP 5.6.29 code snippet:
$QsoId = $SQLiteData["QsoId"];
$SQLiteData["MyAntenna"] = $ODBCAnt;
$query = sprintf("UPDATE Log SET 'MyAntenna' = ':%s' WHERE 'QsoId' = %s", $ODBCAnt, $QsoId);
$qry = $SQLite["connection"]->prepare($query);
$res = $qry->execute();
$tt = $qry->rowCount();
This works as expected and $res is set to TRUE. The problem is apparently nothing changes in the data file. According to what I read, a 'COMMIT' is not required in PDO. $tt is set to 0.
I found my answer. A brain fog had settled in and all that needed to be done was to unquote the field names. Thanks all.
Related
I've been having problems trying to update a value based off a code that is received on the page.
For example:
http://example.com/register.php?code=fa82f82712d1 (not the actual code, it's always a 32-char code actually).
I start a transaction, and do an update in the following way:
$stmt = $stmt->prepare("UPDATE USER SET GOT_CODE = 1 WHERE CODE = :code");
Then do a $stmt->execute(array(':code' => $code)); from the code gotten before.
But it never updates anything, I'm running a rowCount() (gives me '0') and closing the transaction but can't seem to get it updated.
The column type is CHAR(32) which should match with the length of the code received.
Is it possible that it's causing confusions because of the data type?
Maybe bind the parameter before like:
$stmt2 = $stmt->prepare("UPDATE USER SET GOT_CODE = 1 WHERE CODE = :code");
$stmt2->bindParam(":code", $code, PDO::PARAM_STR);
if ($stmt2->execute()){
$stmt->commit();
} else {
$stmt->rollBack();
}
EDIT: after comment of other people. The basic is to not use
$stmt = $stmt->prepare("UPDATE USER SET GOT_CODE = 1 WHERE CODE = :code");
but to give different var name like
$stmt2 = $stmt->prepare("UPDATE USER SET GOT_CODE = 1 WHERE CODE = :code");
because otherwise the system will overwrite $stmt and not work anymore
I integrated a PHP file in my Wordpress installation with a plugin. I have found out, how I am able to send several variables and posting them to a MySQL database, but I am confused, how to manipulate my data like this:
$web = "http://internal.weddingcenter.at/wp-content/themes/twentytwelve/orders.php";
a href="<? echo ''.$web.'?contact='.$daten[id].'' ?>">Rechnung</a>
if ($contact) {
$datum = date('Y-m-d', $date);
$sql_update = "Update wccrm_orders set contacted_date = $datum where id = $contact";
$result = mysql_query($sql_update, $db);
}
I never jump into the if-clause.
How can this be solved?
It has to be :
if($_GET['contact'])
...
OR You cant try :
$contact = $_GET['contact'];
if($_GET['contact'])
...
To add to Kunal Gupta I can see more problems...
if ($_GET['contact']) {
//Forgot to mention SQL injection prevention...
//Try preg_replace or mysqli_real_escape_string()
$datum = preg_replace('[0-9 \/]', '', date('Y-m-d',$date)); //I think that will work
//OR
$test = date('Y-m-d', $date);
$datum = mysqli_real_escape_string($test); //Should also work...
//You must always place PHP variables in inverted commas
$sql_update = "UPDATE wccrm_orders SET contacted_date='$datum' WHERE id='$contact'";
//use MySQLi... It's quicker. Use the variables this way around
$result = mysqli_query($db, $sql_update);
}
There are still quite a few problems with the code but without fully understanding what data is coming from the previous page or what you intend to do with the data I can only help so much.
I have created a script which reads an XML file and adds it to the database. I am using XML Reader for this.
The problem is that my XML contains 500,000 products in it. This causes my page to time out. is there a way for me to achieve this?
My code below:
$z = new XMLReader;
$z->open('files/NAGardnersEBook.xml');
$doc = new DOMDocument;
# move to the first node
while ($z->read() && $z->name !== 'EBook');
# now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'EBook')
{
$node = simplexml_import_dom($doc->importNode($z->expand(), true));
# Get the value of each node
$title = mysql_real_escape_string($node->Title);
$Subtitle = mysql_real_escape_string($node->SubTitle);
$ShortDescription = mysql_real_escape_string($node->ShortDescription);
$Publisher = mysql_real_escape_string($node->Publisher);
$Imprint = mysql_real_escape_string($node->Imprint);
# Get attributes
$isbn = $z->getAttribute('EAN');
$contributor = $node->Contributors;
$author = $contributor[0]->Contributor;
$author = mysql_real_escape_string($author);
$BicSubjects = $node->BicSubjects;
$Bic = $BicSubjects[0]->Bic;
$bicCode = $Bic[0]['Code'];
$formats = $node->Formats;
$type = $formats[0]->Format;
$price = $type[0]['Price'];
$ExclusiveRights = $type[0]['ExclusiveRights'];
$NotForSale = $type[0]['NotForSale'];
$arr[] = "UPDATE onix_d2c_data SET is_gardner='Yes', TitleText = '".$title."', Subtitle = '".$Subtitle."', PersonName='".$author."', ImprintName = '".$Imprint."', PublisherName = '".$Publisher."', Text = '".$ShortDescription."', BICMainSubject = '".$bicCode."', ExcludedTerritory='".$NotForSale."', RightsCountry='".$ExclusiveRights."', PriceAmount='".$price."', custom_category= 'Uncategorised', drm_type='adobe_drm' WHERE id='".$isbn."' ";
# go to next <product />
$z->next('EBook');
$isbns[] = $isbn;
}
foreach($isbns as $isbn){
$sql = "SELECT * FROM onix_d2c_data WHERE id='".$isbn."'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count >0){
} else{
$sql = "INSERT INTO onix_d2c_data (id) VALUES ('".$isbn."')";
$query = mysql_query($sql);
}
}
foreach($arr as $sql){
mysql_query($sql);
}
Thank you,
Julian
You could use the function set_time_limit to extend the allowed script execution time or set max_execution_time in your php.ini.
You need to set these vaiables.Mare sure you have permission to change them
set_time_limit(0);
ini_set('max_execution_time', '6000');
You're executing two queries for each ISBN, just to check whether the ISBN already exists. Instead, set the ISBN column to unique (if it isn't already, it should be) then just go ahead and insert without checking. MySQL will return an error if it detects a duplicate which you can handle. This will reduce the number of queries and improve performance.
You're inserting each title with a separate call to the database. Instead, use the extended INSERT syntax to batch up many inserts in one query - see the MySQL manual for the ful syntax. Batching, say, 250 inserts will save a lot of time.
If you're not happy with batching inserts, use mysqli prepared statements which will reduce parsing time and and transmission time, so should improve your overall performance
You can probably trust Gardners list - consider dropping some of the escaping you're doing. I wouldn't recommend this for user input normally, but this is a special case.
Have you tried adding set_time_limit(0); on top of your PHP file ?
EDIT :
ini_set('memory_limit','16M');
Specify your limit there.
if you don't want to change the max_execution time as proposed by others, then you could also split up your tasks into several smaller tasks and let the server run a cron-job in several intervals.
E.g. 10.000 products each minute
Thank you all for such fast feedback. I managed to get the problem sorted by using array_chunks. Example below:
$thumbListLocal = array_chunk($isbns, 4, preserve_keys);
$thumbListLocalCount = count($thumbListLocal);
while ($i <= $thumbListLocalCount):
foreach($thumbListLocal[$i] as $index => $thumbName):
$sqlConstruct[] = "INSERT IGNORE INTO onix_d2c_data (id) VALUES ('".$thumbName."')";
endforeach;
foreach($sqlConstruct as $processSql){
mysql_query($processSql);
}
unset($thumbListLocal[$i]);
$i++;
endwhile;
I hope this helps someone.
Julian
I have the following code which should result in an UPDATE occuring yet I see no change on the table row. Any clues as to why? Below the code I list what I have tried. I've stared at this code for hours!
if (in_array( $topicid , $allowed )) {
$query = $db->query("SELECT lastpost FROM table_topics WHERE forumid=$forumid AND topicid=$topicid");
$thelpost = $db->fetch_array($query);
$db->free_result($query);
$lastpost = explode("|", $thelpost['lastpost']);
$initialtime = $lastpost[2];
$timerightnow = time();
$db->query("UPDATE table_topics SET lastpost='$timerightnow|$loginname|$initialtime' WHERE topicid=$topicid AND forumid=$forumid");
}
Tried printing the query:
$query = $db->query("UPDATE table_topics SET lastpost='$timerightnow|$loginname|$initialtime' WHERE topicid=$topicid AND forumid=$forumid");
print $query;
which resulted in output of 1 which I figure is true.
Tried echoing out variables and they all have the expected output. $loginname and other variables were set in preceeding code not shown.
Tried running the UPDATE manually through phpmyadmin with preset variables. Worked.
Yes, CSV delimiting is bad. It's a legacy app that needs overhauling in the long term.
This is a past paper question for a database class that I'm stuck with. I'm just preparing for my exam, so it's okay to give away the answer.
Consider the following schema:
Borrow(userid: string, callnum: string, copynum: integer, checkout: date, return: date)
Here is the PHP function that has an error.
function countCheckedOutBookCopies($callnum){
$sql = "SELECT COUNT(*) AS bookcount FROM borrow
WHERE return = null and callnum = '".$callnum."'";
$stid = oci_parse($this->conn, $sql); //assume $this->con is correct
if($row = oci_fetch_object($stid)){
return $row->bookcount;
} else{
return -1;
}
}
There are 3 questions.
1.Find the error and fix it.
2.Another error occurs, fix it.
3.Despite everything being fixed, the function would return -1 all the time. Why is this?
I'm only familiar with procedural PHP using MySQL. But I tried running the code and was resulted with $stid returning boolean all the time because I don't know which part is right and which part is wrong.
Here are the things I've tried
1.Changing '".$callnum."' to just simply '$callnum' (because this is how I've always done it in MySQL)
2.Changing return = null to return = 'null' (but I don't think this is the case)
3.Maybe there is something wrong with the concept of getting COUNT(*) instead of just *
EDIT: Just a thought: I feel like oci8 and MySQL do pretty much the same thing, but is there a reason to prefer one over another? I'm sure MySQL is the more popular one, but my school seems to prefer using oci8 for exam questions
Thanks in advance!
Despite the "Find the error" and "Another error occurs":
1.) "return is null",
2.) this->conn is probably supposed to be this->con,
3.) the code is missing oci_execute($stid);,
4.) oci_free_statement($stid);.
function countCheckedOutBookCopies($callnum){
$sql = "SELECT COUNT(*) AS bookcount FROM borrow
WHERE return is null and callnum = '$callnum'";
$stid = oci_parse($this->con, $sql); //assume $this->con is correct
oci_execute($stid);
$ret = -1;
if($row = oci_fetch_object($stid)){
$ret = $row->bookcount;
}
oci_free_statement($stid);
return $ret;
}