I have two php files. One PHP file is my details to connect to a mysql database
The second PHP file performs a SELECT function on the database
My problem is that the second PHP file does not seem to be actioning the first PHP file.
I have just moved to using PDO commands instead of MYSQL commands. When I used a Require('connect_db'); function everything worked. Since I've moved to PDO commands it does not work
I am getting the error
Call to a member function execute () on a non object in................
My code is as below:-
The connect_db file is (I have not shown the strings for password, user , database:-
try
{
$mysql_link= new PDO("mysql:host=$servername;dbname=dbname;charset=utf8",$username,$password);
$mysql_link->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
$mysql_link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
echo "Connected Success";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
My SELECT file is as follows:-
Require('connect_db.php');
$stmt=$mysql_link->prepare("SELECT cif_train_uid,cif_stp_indicator FROM schedule WHERE cif_train_uid=:cif_train_uid");
$stmt->execute(array(':cif_train_uid'=>$cif_train_uid));
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
echo $row['cif_train_uid'];
echo $row['cif_stp_indicator'];
}
$mysql_link=null;
If I merge all the code into one file it works fine. (Both scripts are in the same folder). So its just the Require () function that does not seem to be working but I cannot see why. Any help gratefully received
The only reason I can see that a included / required file results in a problem that does not occur when you put the code in the including file, is when there are no opening php tags in the included file:
<?php
^^^^^ this...
try
{
$mysql_link= new PDO("mysql:host=$servername;dbname=dbname;charset=utf8",$username,$password);
...
Related
I have a bunch of php pages that are running on a schedule that pull data from different SFTP sources, to try and minimise this as a temporary fix I am turning them into functions and having one page that calls each of them
However, when converting these to functions the pages are giving me error 500, through process of elimination I have found that it is when it using the opendir function via sftp
I have called the function page by itself with a reference to call itself as a test and it connects fine, but when called from another page it errors out.
The variables being used to open the directory via sftp are generated on the 'required' page so it's not losing anything via session variables
If I point it to an incorrect directory it is being caught by the error handling in place, but when it 'successfully' connects I get the error 500
Page1.php
<?php
require 'Page2.php';
sftpFunc();
?>
Page2.php
<?php
Function sftpFunc()
{
/* variable declarations and value assignments go here*/
if (!$FTP_CONN = ssh2_connect($FTP_HOST, $FTP_PORT))
die('Unable to connect');
if (!ssh2_auth_password($FTP_CONN, $FTP_USER, $FTP_PASS))
die('Unable to authenticate.');
if (!$FTP_STRE = ssh2_sftp($FTP_CONN))
die('Unable to create a stream.');
if (!$FTP_OPEN = opendir("ssh2.sftp://{$FTP_STRE}{$FTP_DIRI}"))
die('Could not open the directory');
}
?>
Updated the php version on the site from 7.2.34 to 7.3.11 and it seems to have done the trick, hadn't seen any version specific issues with opendir() before and it seemed to only happen when using it through a function on a different page.
Not sure on the why, but at least this worked.
I use phpseclib to SSH into my remote server from a web browser and execute a php file. Below is the code I use:
$ssh->exec('cd myfolder/; php main.php ' . $file, 'packet_handler');
function packet_handler(){
echo "Completed";
header("Location: exec_completed.php");
}
The main.php file gets executed without any issue. The problem is with returning the data after execution. I have the following questions:
I do a lot of processing in the main.php file and i need to show real time progress of what the script does. When i execute the file through exec, only the first echo in the main.php is printed and the execution stops. Is there any way to get real time data from the executing script.
I follow this example from phpseclib for callbacks although my callback function packet_handler doesn't run after exec is executed. I want to redirect to another page once the main.php I execute through SSH has completed its execution. Now if i redirect to that page i get only partial results as the main.php file has not completed its execution. I tried to use sleep(10) but my main.php may take longer times to execute at times so it didn't work. please suggest any ideas
Callback example from phpseclib:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
function packet_handler($str)
{
echo $str;
}
$ssh->exec('ping 127.0.0.1', 'packet_handler');
?>
For #1... doing $ssh->exec without the callback should work. If it doesn't I'd need to see the logs, which you can get by doing define('NET_SSH2_LOGGGING', 2) at the top and then echo $ssh->getLog() after. Posting the log at pastebin.com and then posting a link would be good. But that said, that won't get you real time output either.
For #2... the callback function is mainly intended for real-time updates and odds are very likely that what you'll get with each call of the callback function will be an incomplete output. So for your callback to output "Completed" and redirect the user to another location is, in all likelihood, incorrect.
Another approach that may work for you: use the interactive shell. Example:
http://phpseclib.sourceforge.net/ssh/examples.html#sudo,
I don't know what your output is like. Maybe you could read() until you got to certain parts of the output that are guaranteed to be output. Or maybe you could use $ssh->setTimeout(5) and get updated output every five seconds or something..
I'm trying to connect database.
Here's the details of my work:
$con=mysql_connect("localhost", "root","");
mysql_select_db("employees",$con);
if(!$con)
{
die("cant establish database connection".mysql_error());
}
else
{
echo "connection Created";
where in xamp my database,which i created in phpmyadmin in localhost xamp, is on the following directory:
C:\xampp\mysql\data
Here a folder named Employees is created then it has some files in it.
when i run php file to create connection it shows nothing on the screen according to my code it must show :
connection created
now can anyone tell me where im wrong? why its not showing any output?
If that's your whole script, you're missing a closing brace at the end }.
Turn on error display, it will be useful in the future: How do I get PHP errors to display?
Also have a read of this: Why shouldn't I use mysql_* functions in PHP?
I'm trying to use PHP to connect to SQLite. I created a database by importing a CSV file into the tables for three tables. However, I'm unable to connect using the following code:
$dbhandle = sqlite_open('db/pokedex.db', 0666, $error);
if(!$dbhandle) die ($error);
This returns the following error:
Warning: sqlite_open() [function.sqlite-open]: file is encrypted or is not a database in /pokedex/configpokedexdb-sqlite.php on line 12
file is encrypted or is not a database
Googling told me I might have a version mismatch. Despite finding some SQLite3 mentions in my phpinfo(), I decided it might still be a problem so I tried the following suggested code:
try
{
//connect to SQLite database
$dbhandle = new PDO("sqlite:db/pokedex.db"); //sqlite:VPN0.sqlite
// echo "Handle has been created ...... <br><br>";
}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br><br>Database -- NOT -- loaded successfully .. ";
die( "<br><br>Query Closed !!! $error");
}
After which I received the following error:
Warning: sqlite_exec() expects parameter 1 to be resource, object given in /home/rawdco81/public_html/pokedex/index-sqlite.php on line 53
Before this, I tried running new PDO("sqlite:VPN0.sqlite"); which was what the site provided, but that was obviously wrong because it didn't point to my .db file at all. You'll see this piece of code in the comments beside the function call.
I'm having a hard time just connecting to the database...What is the proper way to do this?
Also, I'm running PHP Version 5.2.13.
EDITED: I pasted the wrong error message in the wrong place.
Now that I look more closely.., I think you are connecting successfully in the second code segment! You shouldn't be using sqlite_exec in tandem w/ PDO though; those are two different PHP interfaces into SQLite.
The reason the first bit of code bombed is because the legacy library doesn't support PDO v3. The second bit of code is bombing once you try to run sqlite_exec against the PDO object I presume.
I bet if you put a var_dump($dbhandle); after the try/catch you'll see you've got an initialized PDO object.
Just read up on using PDO to run queries and you should be golden!
I am trying to retrieve data from MySQL for a flash application via PHP, but I am having trouble connecting to the server. I created the following .php to test whether it is functioning correctly:
<html>
<body>
<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
if (mysql_query("CREATE DATABASE testphp",$link))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
?>
</body>
</html>
When I try and access this from my browser I don't get anything back, it just remains blank? (when I just do localhost/xxx.php is just displays the code, don't know if that's helpful? -aside from entering file path) I tried some other .php files, but I either just get an error back or white web browser screen, as above? How can I test to see whether PHP is setup correctly? I know for sure it is working when I login using the mysql shell and do "show databases;" and testphp is listed, it is currently not. Thanks!
SOLUTION:
Turns out that I had been using the installer while I think there were a lot of assumptions that I was using binaries (or vice versa). Anyhow I have it up and running...expletive, expletive! :) Thanks everyone!
put the following script somewhere within your document root. Then go to that address it should print out a lot of useful information.
<?php
phpinfo();
?>
Your problem is obviously (from the comments) that of a web server setting: you php pages are not being processed.
Try instead this
<html><body>
Hi
<br>
<?php
echo "This line comes from php";
?>
<br>
Bye
</body></html>
When you see three lines in your browser, then you know that you PHP pages are being processed. After then, try the phpinfo() and only after then try to connect to the database. Step by step...