From my studies I understand that one should free the result set data, after manipulating/using the Query data. I am trying to use the mysqli_result::free pdo in the following code context:
if ($result)
{ $change = $result[0][9];
$change = $change+1;
$sql='UPDATE users SET visits = :visit WHERE username = :username';
$q=$dbh->prepare($sql);
$q->bindValue(':username',$username,PDO::PARAM_STR);
$q->bindValue(':visit',$change,PDO::PARAM_INT);
$q->execute();
// initiate SESSION and Set USER SESSION Variables through the --session_handler.php
require ('./includes/session_handler.php');
//Free the Query Result set to free memory on the database handler
$result->free();
// direct registered users to MEMBER_ZONE.php for registered users interface
header('Location: member_zone.php');
exit();
}
I receive the message :
Fatal error: Call to undefined method PDOStatement::free() in...
I have researched at http://php.net/manual/en/mysqli-result.free.php as well as on Google and found no example code to further elucidate its application.
On this site the examples : mysqli_result::free increase php memory usage
Here it is confusing: shouldn't in this code the $row be freed?
And here Are mysqli_result::free and mysqli_stmt::free_result the same?
Here two forms of free_result() and free() are used together.
I cannot seem to find my error or a clear usage example. I would appreciate any input.
It seems I was using the $q->free() on a result, which never came, because I was updating the database, not requesting a result. The syntax seemed to be correctly used. It was my order of use which I think was wrong.
Related
I am trying to get a reply text message from Clickatell using their Rest API, when I call the parseReplyCallback function when their system posts to my page - it seems to be null or I am not sure how to get the variables it is returning. What I would like to do is have all of the variables returned insert into a SQL database so I can use it elsewhere.
I have tried quite a few things, using various styles of getting the variables such as $_POST, $results['text'], $results->text, and so forth each time I can't seem to get any information out of it. I can't just var_dump or anything because I can't see any backend or console so I am pretty much in the blind, hoping someone else is using this system and has it working fine.
require __DIR__.'/clickatell/src/Rest.php';
use clickatell\ClickatellException;
use clickatell\Rest;
$Rest = new Rest("j8VKw3sJTZuVfQGVC7jdhA");
// Incoming traffic callbacks (MO/Two Way callbacks)
$Rest->parseReplyCallback(function ($result) {
//mysqli_query($con,"INSERT INTO `SMSCHAT` (`text`) VALUES ('$result')");
$mesageId = mysqli_real_escape_string($con,$result['messageId']);
$text = mysqli_real_escape_string($con,$result['text']);
$replyMessageId = mysqli_real_escape_string($con,$result['replyMessageId']);
$to = mysqli_real_escape_string($con,$result['toNumber']);
$from = mysqli_real_escape_string($con,$result['fromNumber']);
$charset = mysqli_real_escape_string($con,$result['charset']);
$udh = mysqli_real_escape_string($con,$result['udh']);
$network = mysqli_real_escape_string($con,$result['network']);
$keyword = mysqli_real_escape_string($con,$result['keyword']);
$timestamp = mysqli_real_escape_string($con,$result['timestamp']);
//do mysqli_query
});
I'd like for it to break the result into individual variables (because I plan on doing other things such as an auto-reply, etc) and upload it to the SQL database scrubbed.
Either doesn't create the table entry or gives me a blank one altogether in that first test where I put the result in the text field.
From a Clickatell point of view, although we understand what you're asking - it's unfortunately outside the scope of support that we offer on our products.
If you would like more information on our REST API functionality, please feel free to find it here: https://www.clickatell.com/developers/api-documentation/rest-api-reply-callback/
If you don't succeed in setting up the callbacks, please feel free to log a support ticket here: https://www.clickatell.com/contact/contact-support/ and one of our team members will reach out and try to assist where possible.
There is useful method getStats in Db-component Yii
$sql_stats = YII::app()->db->getStats();
echo $sql_stats[0] //the number of SQL statements executed
echo $sql_stats[1] //total time spent
Official documentation link
Is there method in Yii2 to get this information?
Here is equivalent for Yii 2:
$profiling = Yii::getLogger()->getDbProfiling();
$profiling[0] contains total count of DB queries, $profiling[1] - total execution time.
Note that if you want to get information about all queries at the end of request you should execute this code in right place, for example in afterAction():
public function afterAction($action, $result)
{
$result = parent::afterAction($action, $result);
$profiling = Yii::getLogger()->getDbProfiling();
...
return $result;
}
Otherwise you will get the information according to the moment of execution this command.
Official documentation:
getDbProfiling()
afterAction()
If you enable the debugging toolbar you get a lot more info, it is much much better than this. Also you can enable logging that should also get you much more info.
More info on the debugging toolbar here http://www.yiiframework.com/doc-2.0/ext-debug-index.html and more info about the logging here http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
My following piece of code works fine on chrome, however when I try to load it on firefox is says: 'Notice: Trying to get property of non-object in */loginTrueFunctions.php on line 23'
these are the "problem codes":
$firstName = $sqlObjectFirstName->firstName;
And:
$lastName = $sqlObjectLastName->lastName;
PHP code:
function getFullName($id, $mysqli_connect){
if(isset($_SESSION['login'])){
$queryFirstName = mysqli_query($mysqli_connect,"
SELECT firstName
FROM users
WHERE id = '$id'
LIMIT 1
");
$sqlObjectFirstName = mysqli_fetch_object($queryFirstName);
$firstName = $sqlObjectFirstName->firstName;
printf( ucfirst($firstName));
printf(' ');
$queryLastName = mysqli_query($mysqli_connect,"
SELECT lastName
FROM users
WHERE id = '$id'
LIMIT 1
");
$sqlObjectLastName = mysqli_fetch_object($queryLastName);
$lastName = $sqlObjectLastName->lastName;
printf( ucfirst($lastName));
}
}
Thanks in advance!
EDIT: if I call the result as an array, firefox gives no error nor a result, but then chrome gives this error: Cannot use object of type stdClass as array.
Then I change mysqli_fetch_object to mysqli_fetch_array, result: Chrome works fine, firefox doesn't give an error nor a result.
Probably your session (or cookie) contents are different between your browsers(for example, you are logged in with one, but not with the other). Do a
var_dump($_SESSION); var_dump($_COOKIE);
and see
Check the return value of mysqli_fetch_object($queryFirstName); before trying to access it. It's likely your query is not returning anything.
Another thing to mention is DO NOT EVER PASS USER DATA DIRECTLY INTO A QUERY as you are vulnerable to a sql injection attack. Just because you are using mysqli, doesn't make you immune. You have to use the features of mysqli such as prepared statements to avoid sql injection.
More likely then not the error appears on both browsers and there is just broken markup and they are fixing it in two different ways, one leaving your markup visible the other not. Without seeing code there is no way to know for sure.
PROTIP
You can select multiple fields from a database in a single call.
$res = mysqli_query($mysqli_connect,"
SELECT firstName, lastName
FROM users
WHERE id = '".(int)$id."'
LIMIT 1");
if (!$res) die("Error running query at ".__FILE__." : ".__LINE__);
$person = mysqli_fetch_object($res);
echo $person->firstName;
echo $person->lastName;
Post the code that is posting this data from the browser to the php script, it very well could be that the data you are submitting to the server is different because of the browsers.
also, where is the code that shows how your data is being called on the php side of things?
I'm experiencing a strange problem. I'm caching the output of a query using memcache functions in a file named count.php. This file is called by an ajax every second when a user is viewing a particular page. The output is cached for 5 seconds, so within this time if there will be 5 hits to this file i expect the cached result to be returned 3-4 times atleast. However this is not happening, instead everytime a query is going to db as evidenced from a echo statement, but if the file is called from the browser directly by typing the url (like http://example.com/help/count.php) repeatedly many times within 5 seconds data is returned from cache (again evidenced from the echo statement). Below is the relevant code of count.php
mysql_connect(c_dbhost, c_dbuname, c_dbpsw) or die(mysql_error());
mysql_select_db(c_dbname) or die("Coud Not Find Database");
$product_id=$_POST['product_id'];
echo func_total_bids_count($product_id);
function func_total_bids_count($product_id)
{
$qry="select count(*) as bid_count from tbl_userbid where userbid_auction_id=".$product_id;
$row_count=func_row_count_only($qry);
return $row_count["bid_count"];
}
function func_row_count_only($qry)
{
if($_SERVER["HTTP_HOST"]!="localhost")
{
$o_cache = new Memcache;
$o_cache->connect('localhost', 11211) or die ("Could not connect to memcache");
//$key="total_bids" . md5($product_id);
$key = "KEY" . md5($qry);
$result = $o_cache->get($key);
if (!$result)
{
$qry_result = mysql_query($qry);
while($row=mysql_fetch_array($qry_result))
{
$row_count = $row;
$result = $row;
$o_cache->set($key, $result, 0, 5);
}
echo "From DB <br/>";
}
else
{
echo "From Cache <br/>";
}
$o_cache->close();
return $row_count;
}
}
I'm confused as to why when an ajax calls this file, DB is hit every second, but when the URL is typed in the browser cached data is returned. To try the URL method i just replaced $product_id with a valid number (Eg: $product_id=426 in my case). I'm not understanding whats wrong here as i expect data to be returned from cache within 5 seconds after the 1st hit. I want the data to be returned from cache. Can some one please help me understand whats happening ?
If you're using the address bar, you're doing a GET, but your code is looking for $_POST['...'], so you will end up with an invalid query. So for a start, the results using the address bar won't be what you're expecting. Is your Ajax call actually doing a POST?
Please also note that you've got a SQL injection vulnerability there. Make sure $product_id is an integer.
There are many problems with your code, first of all you always connect to the database and select a table, even if you don't need it. Second, you should check $result with !empty($result) which is more reliable as just !$result, because it's also covers empty objects.
As above noted, if the 'product_id' is not in the $_POST array, you could use $_REQUEST to also cover $_GET (but you shouldn't, if you are certain it's coming via $_POST).
i need to implement a simple project using PHP and MySql in which i need to push data to the user's UI when some one else is updating the database, and i need to periodically do this too, so when some one else accessing the same table and modify it, another person who uses the UI can see the updates, sorry if i'm being silly but up to now i'm only aware of saving data to a database and retrieving and showing it to the user(simplest form of data base connection). how can i achieve this in php, please some one help me on this matter, if the answer explains the things in detail it is good, because i'm very novice to this.thanks in advance.
rangana.
If the web page has no data to return, then get the page to wait for a period while polling the database. You must use the sleep statement to avoid maxing out your server.
Warning: Some servers wont let the user open another page while one is in progress, which may cause you problems in some situations. So either dont hold the page open for too long, or maybe try to get the ajax page to use a different session.
// do this when you have put new data in to database
setappdata("lastupdate",microtime(true));
// use this loop to poll for new data
$loop = 0;
$lastupdate=$_SESSION["lastupdate"];
while ($lastupdate==$last=getappdata("lastupdate") and $loop<10) {
$loop++;
usleep(500000); //0.5sec
}
// use a table called appdata to store application data
function getappdata($var) {
$query = "SELECT data FROM appdata WHERE var='$var'";
$res1=mysql_query($query);
if (mysql_numrows($res1)<1) return false;
return mysql_result($res1,0,"data");
}
function setappdata($var,$data) {
$query = "SELECT data FROM appdata WHERE var='$var'";
$res1=mysql_query($query);
if (mysql_numrows($res1)>0) $query = "UPDATE appdata SET data='$data' WHERE var='$var'";
else $query = "INSERT INTO appdata SET var='$var',data='$data'";
return mysql_query($query);
}
These topics may get you started:
http://en.wikipedia.org/wiki/Push_technology
http://en.wikipedia.org/wiki/Reverse_Ajax