I'm trying to access to my mssql server by php here is the code:
<?
mssql_connect($core['localhost'],$core['sa'],$core['mypass']);
mssql_select_db($core['dbone']);
?>
This is at the top of my page before <!DOCTYPE> Basically when I open my index.php no content is shown - it's like it can't connect to the mssql? Is there something i have to enable before I can access mssql_connect()?
edit: extension=php_mssql.dll is enabled if this is the right extension?
It's not "mssql" on both functions change them to "mysql" and try again.
For debugging the problem, put this in your index.php and see what gets printed out
<?php
$con = mssql_connect($core['localhost'],$core['sa'],$core['mypass']);
if(! $con) { echo 'Could not connect'; }
else { echo 'Connection successful'; }
echo '<br />';
$db = mssql_select_db($core['dbone']);
if (! $db) { echo 'Could not select database'; }
else { echo 'Database selected'; }
?>
<!DOCTYPE html>
.
.
.
Something should get printed out now.
Related
Please help me connect to a DB - I need to get this to work:
<?php
require_once 'general.php';
try {
$db = new PDO('mysql:host=account.ipowermysql.com;dbname=zipcodes', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (PDOException $e) {
print 'Oh SNAP ... Error!: ' . $e->getMessage() . '<br/>';
die();
}
If i run it as is, i get a 500 error page.
If I add ?> at the end, I get a blank page.
If I run "DB-connect" code generated by IPOWER it works.
IPOWER generated code:
<?php
$link = mysql_connect('account.ipowermysql.com', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(zipcodes);
?>
I guess the server is configured not to show PHP errors, but a 500 error page instead...
PS - here is general.php code:
<?php
error_reporting(0);
#ob_start();
session_start();
function echo_session($arg) {
if(isset($_SESSION[$arg])) {
return $_SESSION[$arg];
} else {
return '';
}
}
PS2 - original code (at the top) works on GoDaddy and MediaTemple ... it's something with IPOWER ...
I'm a noob and I need this to work, but don't know how to.
Please help
Even if I have used LAMPP many times, this time something goes wrong. When I visit the browser(chrome) nothing echos. Here is my code:
index.php
<?php
error_reporting(E_ALL); /*after edit*/
$link = mysqli_connect('localhost', 'root', 'root', 'db');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Do I miss something? The output is nothing. By the way i write my files in
var/www/html/my_pages
and i call it this way: localhost/my_pages. Simple echos are working and php in general is fine. Something goes wrong with my db connection.
Use this code
<?php
$link = mysqli_connect('localhost', 'root', 'root', 'db');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
else
{
echo 'Connected successfully';
}
?>
Yes probably, because mysqli_connect() method return the object, not Boolean value.
You can verify the connection with the following code:
if($link->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return false;
}
else{
echo "Connection Successful";
return $link;
}
Why not using PDO.
$dsn = "mysql:host=localhost;dbname=db";
try {
$pdo = new PDO($dsn, "root", "");
} catch (PDOException $ex) {
$ex->getMessage();
}
with PDO you can change anytime you need the Db vendor:
http://php.net/manual/en/book.pdo.php
<?php
echo phpinfo();
?>
Run this file and get all PHP and apache details. Search for mysqli support in it. If it is supported, you should have something like below.
Also check for your root directory
Thanks everyone for the response! I found the problem. Something went wrong and mysqli wasn't enabled in php. That's why i had this error Fatal Error:Call to undefined function mysqli_connect() in /var/www/html/diamond/index.php on line 8 I reinstalled php and problem solved :)
I have a problem with my database! Here is my code:
<?php
$host = "/homes/49/jc192699/public_html/dbase";
$database = "EduPro.db";
$dbhandle = new PDO("sqlite:".$host.$database);
if (!$dbhandle){
echo "Error connecting to database.\n";
}
else{
echo "<br>";
echo "<br>";
echo "Database connection successful!";
}
mysql_select_db($database);
?>
The problem is that it's saying "Database connection successful!" No matter what I do, if I type the address in wrong, it still says successful, when I renamed the database to a database that doesn't exist, it still says successful. I can't see what the problem is here?
If anybody could help me out it would be GREATLY appreciated!
Thank you!
For starters, the PDO constructor throws an exception if there is an error. It does not return false. Check for errors using
try {
$dbhandle = new PDO("sqlite:".$host.$database);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Secondly, as you are using SQLite, provided your dbase directory is writeable by the script, your connection attempt will create an empty database.
Try this:
<?php
try {
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:/path/to/database.sdb");
}
catch(PDOException $e)
{
/*** real error message prints here ***/
echo $e->getMessage();
}
?>
This is directly taken from here:
http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#4.2
I have an error.php file which can be grossly simplified to:
<?
if (!isset($error))
$error = "Unspecified Error";
echo "Error: $error";
?>
It is not "normal usage" to just navigate to error.php. Rather, I would do something like:
$dbh = mysql_connect($host, $user, $pass);
if (!$dbh)
{
$error = "Can't connect to MySQL: " . mysql_error();
include('error.php');
exit();
}
That said, if the user does navigate to error.php then they will just get "Error: Unspecified Error" as expected.
All my code is working, and the error page shows up and works exactly as expected, however Zend is complaining that $error is undefined on the line: if (!isset($error)).
I realise my design pattern is awful, but I'm just throwing together something quick-and-dirty in this case.
Better idea, create a function instead:
function output_error( $error = NULL )
{
if( !$error ) $error = "Unspecified Error";
echo "Error: $error";
}
It has the benefit of both removing the Zend issue, and you have a MUCH better design. Then:
if (!$dbh)
{
include('error.php');
output_error( "Can't connect to MySQL: " . mysql_error() );
exit();
}
$dbh = mysql_connect($host, $user, $pass);
if (!$dbh){
$error = "Can't connect to MySQL: " . mysql_error();
if(!include('error.php'){
echo $error;
exit();
}
}
Try this:
if (!isset(#$error)).
I have this code:
$link = mysql_connect("localhost", "ctmanager", "pswsafgcsadfgG");
if ( ! $link )
die("I cannot connect to MySQL.<br>\n");
else
print "Connection is established.<br>\n";
print "a";
if ( mysql_create_db("ct", $link) )
print "AAA";
else
print "BBB";
print "2";
die();
And this is the output:
Connection is established.
a
So, I cannot understand how it's possible that neither "AAA" no "BBB" is outputted. Is it because program dies at mysql_create_db?
You probably guessed right. Try adding:
error_reporting(-1);
ini_set('display_errors', 'On');
at the very top of your script. You will get more details I'm sure. Post your findings and I'll update my answer if needed to.
Also, if you use PHP 5, you can try:
try
{
if (mysql_create_db("ct", $link))
echo 'AAA';
else
echo 'BBB';
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
maybe this will catch something...
Also, as František Žiačik pointed out with his link:
The function mysql_create_db() is
deprecated. It is preferable to use
mysql_query() to issue an sql CREATE
DATABASE statement instead.
mysql_create_db is deprecated in PHP5, maybe there even does not exist anymore.
See http://php.net/manual/en/function.mysql-create-db.php for an example how to do it.
I think you're right it is failing at mysql_create_db but i would guess it's the SQL that you're passing into it. are you trying to create a DB called "ct"? If so i'd try "CREATE DATABASE ct"
What PHP version are you running? According to the documentation, mysql_create_db is depracated, and a CREATE DATABASE query should be issued instead.
if (mysql_query("CREATE DATABASE ct", $link))
print "AAA";
else
print "BBB";
I think this would be the alternate way hopefully.
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
?>