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?
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 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);
...
I think it is a foolish question but I cannot understand why it is happening... I want to connect to my MySQL database with this simple code:
<html>
<head>
<title>File1</title>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","") or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db("Users",$con) or die("Failed to connect to MySQL: " . mysql_error());
if (mysql_error()) {
echo "Failed to connect to MySQL: " . mysql_error();
}
?>
</body>
</html>
As user, I use root. The problem is that root needs a password. So, with this code I should obtain on my screen "Failed to connect to MySQL", right? However, the screen is white.
Why is white instead of appearing "Failed to connect..."?
Many thanks!
I think errors are not displayed in your server , Inside your php.ini add or change value of this line:
display_errors = on
Then restart your web server.
Probably a simple case of fatal errors being hidden?
Use this to turn errors on:
error_reporting(E_ALL);
ini_set('display_errors', true);
In your page, Your connection got established, there is no success message, there is no other browser output aswell. So that it seems blank screen.
if ($con) {
echo "Connection successful!!";
}
The reason you're not getting any output is because of the or die.
die() stops the execution of the script.
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("Users",$con);
The difference between error display on or off, is the warning you get before the script exits through the die() command.
You will always see the message printed by die() at the end of the output as that is unrelated to any php error handling:
Failed to connect to MySQL: Access denied for user 'root'#'localhost'
(using password: NO)
The only reason I can think of that the message not shows, is that the php is not getting executed; for example if your file is an .html file instead of a .php file. That would lead to a blank page as the opening < of the php section will be seen as the opening of an html tag and the same applies to the closing tag.
Checking the source of the output would confirm that.
Your $db variable is unassigned.
Use this instead, by removing $db=
mysql_select_db("Users",$con) or die("Failed to connect to MySQL: " . mysql_error());
Plus, mysql_ functions are deprecated. For more information on this, visit the PHP.net Website.
http://www.php.net/manual/en/function.mysql-db-query.php
I suggest you start using mysqli_ with prepared statements or PDO instead.
WARNING
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
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...