When i try to execute this one function, it returns an error saying "Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM on line 103".
In line 103, it looks like (This is the beginning part of the code)
function get_list($option)
{
//$db = new db();
//$db->getConnection();
$rs = $this->db->MongoCursor::doQuery($this->sql, $this->sql_params); //this is the line 103
$this->resultList = $rs;
What am I doing wrong?
You can not mix static and instance syntax. It would either be:
$rs = $this->db->MongoCursor->doQuery($this->sql, $this->sql_params);
or
$rs = MongoCursor::doQuery($this->sql, $this->sql_params);
I don't know what framework if any that you are using to tell you exactly what it should be but what I showed you will fix the syntax error.
$this->db looks like it might be Codeigniter? But MongoCursor::doQuery() is vanilla PHP: http://php.net/manual/en/mongocursor.doquery.php
Related
I try to access to database from PostgreSQL by using PHP.
I run this code, but I got an error:
VM6722:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Object.success ((index):58)
at c (jquery-3.4.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.4.1.min.js:2)
at l (jquery-3.4.1.min.js:2)
at XMLHttpRequest.<anonymous> (jquery-3.4.1.min.js:2)
I'm trying to figure out but I'm very new to php. SO I would appreciate any inputs to fix this issue.
<?php
$db = new PDO('pgsql:host=localhost;dbname=webmap103;', 'postgres', 'postgres');
$sql = $db->query("SELECT id, name, image, web, category,ST_AsGeoJSON(geom, 5) as geom FROM cdmx_attractions ORDER BY name");
$features = [];
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
$feature = ['type'=>'Feature'];
$feature['geometry'] = json_decode($row['geom']);
unset($row['geom']);
$feature['properties'] = $row;
array_push($features, $feature);
}
$featureCollection = ['type'=>'FeatureCollection', 'features'=>$features];
echo json_encode($featureCollection);
?>
I expected to show the data on my web application.
I'm using html and ajax to call php to get an access to my database on postgresql.
If your sql statement fails $db->query(...) returns false. Check to see if your sql statement is valid and executes properly. If it is false you will get the error "Fatal error: Uncaught Error: Call to a member function fetch() on bool"
As Engin said, $db->query() return false when it fails. See https://www.php.net/manual/en/pdo.query.php
PDO::query() returns a PDOStatement object, or FALSE on failure.
Don't call your PHP using Ajax, at least to debug. It will be way easier to see the error. Just call your PHP route directly in a browser. json is a mess to debug in a browser console.
Try to catch errors and display it using PHP :
<?php
try{
$db = new PDO('pgsql:host=localhost;dbname=webmap103;', 'postgres', 'postgres');
$sql = $db->query("SELECT id, name, image, web, category,ST_AsGeoJSON(geom, 5) as geom FROM cdmx_attractions ORDER BY name");
$features=[];
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
$feature=['type'=>'Feature'];
$feature['geometry']=json_decode($row['geom']);
unset($row['geom']);
$feature['properties']=$row;
array_push($features, $feature);
}
$featureCollection=['type'=>'FeatureCollection', 'features'=>$features];
echo json_encode($featureCollection);
} catch (Exception $e) {
var_dump($e->getMessage());
var_dump($e->getCode());
}
}
?>
Check your PHP logs (I don't know what is your OS or version of PHP but here is a quick exemple on debian, using PHP7.3-FPM :
tail -f /var/log/php7.3-fpm.log
Call your page and watch your shell.
is there something wrong with my code, it look exactly like the example on the php page but it give me this error Fatal error: Call to a member function bindParam() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/videosharing/index.php on line 68
$hi = 'hi';
$limit = 4;
$isi = 1;
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views`,`tags` FROM `videolist` WHERE `tags` = :atagz ";
$stmt = $connection->prepare($query);
$stmt->bindParam(':atagz',$hi);
Your connection is likely fine (otherwise, you'd have a different error, sooner).
If the error is "Fatal error: Call to a member function bindParam() on a non-object", then $stmt isn't an object. In other words, your prepare() call is failing. Per the documentation for prepare(), that occurs when the database can't prepare the statement.
Reporting these errors is one of the areas where I think PDO falls short. You can get more information on the error with the following:
var_dump($connection->errorInfo());
The most likely cause is a misspelling in an attribute or table name.
I set up my global like this:
require('../scripts/mysql_db.php');
$DB = new mysql_db();
$connectid = $DB->sql_connect($mysql_host, $mysql_user , $mysql_password, $mysql_database);
I then use $DB throughout like this:
$query1 = $DB->query('SELECT ....');
However I wrote a function to use $DB and its not accessible for some reason:
function deletePendingRow($aOkReason, $aFailReason) {
$query99 = $DB->query('DELETE .....');
}
I think this is a basic php thing, can someone help me understand why.
The mysql_db is here: https://github.com/Noitidart/MailtoWebmails-Backend/blob/master/scripts/mysql_db.php
Thanks
edit:
i tried this:
function deletePendingRow($aOkReason, $aFailReason) use ($DB, $rowPending) {
It doesnt seem to work it tells me Parse error: syntax error, unexpected T_USE, expecting '{'
edit 2:
i also tried this:
$deletePendingRow = function($aOkReason, $aFailReason) use ($DB, $rowPending) {
};
but this throws Parse error: syntax error, unexpected T_FUNCTION in /home/a1304271/public_html/ajax/approve_pending.php
define it like to access global variables in the functions
global $DB;
Although if you are getting the database variable via parameters, then there should be no problem accessing it.
Hello everybody~ I am getting this error (Parse error: syntax error, unexpected T_RETURN in C:\wamp\www\Nu-Bio\view_topic.php on line 77) when trying to run this little bit of code:
$idd = $rows['id'];
$thisql = "SELECT `locked` FROM `forum_question` WHERE `id` = '$idd'";
$mythisql = mysql_query($thisql);
$res1 = return($mythisql);
It's standalone, not in a function or anything. I'd give you more information, but I'm not sure what to give. I'm calling it with if ($res1 == 0) {. Thanks for any help I get!
(PS: I know I should be moving to mysqli. I WILL be doing that soon, please don't tell me to. I just want to make sure it works before changing it, as I'm almost done with my project)
What should this "return" do? Anyway it's wrong here and is your error at line 77.
$res1 = return($mythisql);
Solution:
$res1 = $mythisql;
or
$res1 = mysql_query($thisql);
I receive the following error...
Parse error: syntax error, unexpected T_AS in ....\index.php on line 98
for the following script...
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'pw');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
$stmt = $db->prepare("SELECT * FROM tablename");
$stmt->execute();
while($db->fetch(PDO_FETCH_ASSOC) as $row) {
$id= $row['id'];
$name= $row['name'];
}
$db->commit();
}
catch (PDOException $e)
{
$db->rollback();
echo "There was a system error.<br>".$e->getMessage();
}
?>
Any idea what is throwing the error? I have checked for missing semicolons, commas, and the works but got nothing!
Parse error: syntax error, unexpected T_AS in ....\index.php on line 98
T_AS is the token for as in the PHP interpreter. It was unexpected when trying to parse your code's syntax.
as is only valid in a foreach loop, and you are using a while.
Change your while loop to a foreach loop.
Update
Fatal error: Call to undefined method PDO::fetch() in index.php on line 113
This is a run time error - the PDO object has no method called fetch(). Are you calling fetch() on the right object?
Check out the documentation.
As Wrikken states in the comments, it will be a method of your $stmt object.
Because you are using the 'as' keyword in a 'while' loop, which is not valid. Change the 'while' to 'foreach' and you are good to go.