I have installed php and mysql on windows xp with iis 5.1 as my web server. But when i run the following code it just shows me a blank page. Can anybody let me know where i am wrong. Thanks in advance.
<?php
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
Well, it doesn't generate any output if the connection succeeds so that's probably what's happening. What would you expect?
<?php
$con = mysql_connect('localhost', 'root', 'xxxx') or die(mysql_error());
?>
what does this print ?
* make sure you have mysql running
* make sure you have php running on IIS
* try to check in php.ini:
error_reporting = E_ALL
display_errors = On
Use the following code
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} else {
die('Connection ok!');
}
Related
I currently have a db file I include to establish a DB connection:
db.php:
$dbs = array(
"127.0.0.1",
"127.0.0.3",
"127.0.0.2",
);
if(!$con){
$con = mysql_connect($dbs[rand(0,2)],"user_login","password") or die('Could not connect: ' . mysql_error());
}
However when I try to include it sometimes and do a query, it returns a blank result:
include("db.php");
mysql_select_db("database_table", $con);
$result = mysql_query("SELECT * FROM users where email='admin#test.com'");
while($row = mysql_fetch_array($result)){$id = $row['email'];break;}
The weird thing is, if I modify the initial db.php to connect like this, it works (non randomizing it):
db.php:
if(!$con){
$con = mysql_connect("127.0.0.1","user_login","password") or die('Could not connect: ' . mysql_error());
}
Is there any explanation in this? Is there a difference in how they are connecting?
Thanks
First I would turn on error checking.
You can do that by adding the following to the very top of your php file:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
or the long way by changing it globally: display_errors = On in your php.ini
This will output what's wrong. If you can't figure it out be then try the following:
Then check if you could connect to all databases like you did with
if(!$con){
$con = mysql_connect("127.0.0.1","user_login","password") or die('Could not connect: ' . mysql_error());
}
Then I would check if they all had the same tables.
If that's correct I would check the output of your:
if(!$con){
$con = mysql_connect($dbs[rand(0,2)],"user_login","password") or die('Could not connect: ' . mysql_error());
}
Does it really work the way it's intended?
I am testing a website offline with XAMPP. My PHP code connects and works with my local MySQL database (i.e. 'localhost' or '127.0.0.1'). I wish to conduct tests using an online MySQL database.
I am trying to use PHPMyAdmin Demo Server but I get an error when trying to connect to the server. I changed the 'DB_HOST' to '192.168.30.23' (which seems to be the demo server ip address). Am I missing something?
Here is a snippet of my code:
<?php
define('DB_NAME', 'ears');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', '192.168.30.23');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
?>
Thank you in advance.
The phpMyAdmin MySQL demo server is not available to test your script. Google for "public access MySQL server" to maybe find one.
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 the following PHP code that creates a mysql connection:
$link = mysqli_connect("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
In the event the connection is not made, how can I write the error to a text file on my server?
Yes it is possible with error_log function in php
$con = mysqli_connect("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db");
if (!$con)
{
error_log(mysqli_error($con) . "\n", 3, "/var/tmp/my-errors.log");
}
Ok, Ive been playing around with PHP and databases. Note, Im doing all my work on a webserver hosted by bluehost.
So I went into the cpanel and set up a database called gagalugc_stocks.
I also setup a username (test) and a password (password) and added them to be able to control the databse. Below is my script in an attempt to connect to it, not it is also on the server.
<?php
$connect = mysql_connect('localhost','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
-Thanks
NOTE: The problem is that it can never reach the database.
<?php
$connection = mysql_connect ('localhost', 'test', 'password')
or die ('<b>Could not connect: </b>' . mysql_error());
$result = mysql_select_db("gagalugc_stocks")
or die ('<b>Could not connect: </b>' . mysql_error());
?>
Didn't check for port when adding database make sure to specify host and port
(localhost:2083)
How about
<?php
$connect = mysql_connect('localhost:2083','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
For local system:-
$con = mysql_connect("localhost","root","");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
For website or remote server:-
$con = mysql_connect("localhost","username","password");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
for more please visit : Algosoftwares