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.
Related
I'm trying to use php session variables in a SELECT Statement to fetch the data stored in a multiple array database.
However, I am facing some challenges with my code below:
<?php
if(!empty($_SESSION['array'])) {
if($_SESSION['array'] == 'arrayname') {
$query = "SELECT '{$_SESSION['option']}' FROM '{$_SESSION['array']}' WHERE '{$_SESSION['key']}'='".mysqli_real_escape_string( $link, '{$_SESSION['value']}')."'";
if ($result=mysqli_query( $link, $query)) {
while ($row = mysql_fetch_array($result)) {
echo $row['{$_SESSION['value']}'];
}
}
mysqli_close($link); // Closing Connection with Server
}
} ?>
Any attempt to fetch the data with the above code rather displays an error messages, like;
Parse error: syntax error, unexpected 'value' (T_STRING) in C:\xampp\htdocs\content\fetchData.php on line 5
I will be much grateful for a way out to deal with this challenge.
Thanks in advance!
For your query, get your variable first:
$value = mysqli_real_escape_string( $link, $_SESSION['value']);
Then use $value rather than try and embed like you have above.
You don't need the single quotes or the curly braces around $_SESSION when you are echoing the value. Just do this:
echo $row[$_SESSION['value']];
That should fix your current error, however you still have more issues with your code. Namely you are mixing mysql and mysqli functions, which won't work. Move your code over to mysqli completely.
I'm trying to get a list from my MySQL database, which normally works fine. But today I'm getting this error Fatal error: Call to a member function setFetchMode() on a non-object in "Way too long path to file here".
This is my PHP code:
$conn = new PDO('mysql:host=localhost;port=3306;name=erty', 'erty', 'Ops, that my password ...');
$result = $conn->query("SELECT name FROM mod_devs");
$result->setFetchMode();
foreach ($result as $row) {
echo '<tr><td>'.$row['name'].'</td></tr>';
}
Now, you probably understand my goal :)
Generally Call to a member function setFetchMode() on a non-object means that $result is not available. Probably because of MySQL error - either in connection or query. Check if($conn) or if($result).
You still need to fetch the result...
So instead of foreach....do while...
while ($row = $result->fetch()) {
//your code here
}
you maybe need prepare not query
$result = $conn->prepare("SELECT name FROM mod_devs");
or this
$result->setFetchMode(PDO::FETCH_OBJ);
I just found the error. In the connection I wrote name=erty not dbname=erty!
Sorry for taking up your time :(
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
OK, I see some similar questions to mine, but their examples all use PHP classes...mine does not. Maybe that's the problem? I shouldn't need classes because my site is exceedingly simple at this point in time.
Anyway, I'm trying to use PDO to connect to a MySQL db. I connect to the db fine in a file called config.php, and include this file in index.php with require_once().
I can successfully query the db from another file called process.php, but the problem is within a function within that file; it seems my DBO object is out of scope within that function.
Here are the relevant code snippets:
index.php
require_once('./lib/config.php');
config.php
// tested and connects fine
$pdo = new PDO('mysql:host=' . $hostname . ';dbname=' . $dbname, $username, $password, array(
PDO::ATTR_PERSISTENT => true
));
process.php
<?php
...
// can call $pdo fine in this file outside of functions
...
function authenticate($u, $p) {
// can't call $pdo in here, error says $pdo is non-object
$que = $pdo->query('select user_id, user_pass from users where user_name = \'' . $u . '\' limit 1');
...
}
?>
By the way, I'm using PDO because I was having similar trouble with mysqli, and am trying to get away from mysql, which is apparently depreciated and discouraged.
EDIT: I should have clarified first based on the number of responses I got on this matter: I did try to pass $pdo in as a param to the function, with no luck or change in the error message.
SOLUTION: OK, apparently the problem was that I needed to add require_once('config.php') in my process.php file as well. Not sure why (wouldn't it already be included when index.php was run first?). Then I was able to successfully pass $pdo in as a param to my function, and voila.
That's pretty basic PHP stuff. Variables inside functions are local variables unless you use the global keyword to load them. I suppose you want this:
function authenticate(PDO $pdo, $u, $p) {
$que = $pdo->query('select user_id, user_pass from users where user_name = \'' . $u . '\' limit 1');
//...
}
Edit: If PHP claims that $pdo is not an object, it's not an object, so it doesn't really matter how it's passed to the function. Inspect the variable right before you call authenticate():
var_dump($pdo);
Without the relevant code there's no way to say why. (Assuming it's true that new PDO succeeds.)
You need to pass the PDO object as a parameter to the authenticate() function:
function authenticate(PDO $pdo, $u, $p) {
// ..as in the question..
}
Oh and you should be using a place holder for that username in the query, not string concatenation which is prone to SQL injection attacks.
because $pdo has been declared outside of the function authenticate it isn't available inside it. You need to either pass $pdo in
function authenticate($u, $p, $pdo) {
$que = $pdo->query('...');
}
or declare it as global inside the function to be able to access it
function authenticate($u, $p) {
global $pdo;
$que = $pdo->query('...');
}
I am trying to make a function to pull a page's content from a MySQL table using a PDO Prepare statement. My code works just fine outside of the function I defined, but no matter what I do it will not work within the function - I receive the following error:
Fatal error: Call to a member function prepare() on a non-object in /home/tappess1/public_html/pages/stations.php on line 6
Here is my PHP:
function getPageContent($page) {
$st = $db->prepare("SELECT * FROM content WHERE title LIKE ?");
$st->execute(array($page));
$pageContent = $st->fetch();
$text = wordwrap($pageContent['content'], 100, "\n");
$tabs = 4;
$text = str_repeat(chr(9), $tabs) . str_replace(chr(10), chr(10) . str_repeat(chr(9), $tabs), $text);
echo $text;
}
and then
<?php getPageContent(Main);?>
I have even tried using a query instead of prepare statement, simply calling getPageContent() and I receive the same error.
Thanks!
You are trying to access the variable $db which is outside your function's scope.
Either re-initialize your database within the function $db = new PDO...., or - probably better and easier in your case - import the global variable:
function getPageContent($page) {
global $db;
Where and how to best store the global database object is subject of a lot of discussion. If you want to get into it, here is one place to start (there are many others on SO, too). But if you're just getting into PHP, I'd say using the global variable is fine.
The variable $db is not known within your function.