7

My code only shows a blank page with no text. Why could the mysql_connect not connect to the database?

I am using MySQL version is 5.0.77 and PHP version is 5.1.6

$user = "user";
$password = "user";
#$database = "database";

$con = mysql_connect("localhost",$user,$password);

if (!con) {
    echo "could not connect";
} else {
    echo "connected";
}

if (mysql_query("CREATE DATABASE my_db",$con)) {
     echo "databse created";
}

#@mysql_select_db($database) or die( "Unable to select database");
#$query="CREATE TABLE tablename(id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,field1-name varchar(20) NOT NULL,field2-name varchar(20) NOT NULL,field3-name varchar(20) NOT NULL,field4-name varchar(30) NOT NULL,field5-name varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
#mysql_query($query);
mysql_close($con);
Ivar
  • 6,138
  • 12
  • 49
  • 61
user1601000
  • 149
  • 2
  • 2
  • 3
  • are you sure username and password are correct .. since i think you are on local host and havnt change default so try `$user ="root"` and `$password=""` – NullPoiиteя Jan 18 '13 at 05:23
  • I think it's a typo `if(!con)` should be `if(!$con)`. `NOTE:` mysql_* function have been deprecated in PHP. Please update your code to use either mysqli_* of pdo_mysql http://php.net/manual/en/function.mysql-query.php – Rikesh Jan 18 '13 at 05:24
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя Jan 18 '13 at 05:24

4 Answers4

4

Well i wold like to point out one by one

1. you are using !con which is invalid and generate error

Notice: Use of undefined constant con - assumed 'con' in blabla on line bla connected

to use like this you need to declare constant which i think you don't want so use

if(!$con){

2. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
  2. PDO Tutorial for MySQL Developers
  3. Pdo Tutorial For Beginners

3. as i saw in comment you have not turned on error reporting you can do this by in php.ini file

ini_set('display_errors', 1);
error_reporting(E_ALL);

4. do not use # for comment however its same as // comment and cause no problem till 5.5.0alpha3 live result . however its(#) deprecated Comments starting with '#' are now deprecated in .INI files. so its better to use

  1. For single line comment // this is single line comment
  2. For multiline comment /* this is multiline comment */
Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
2

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. Check here for detailed information

Authorize
  • 21
  • 1
1

First try Following code only

<?php
$user="user";
$password="user";
$con=mysqli_connect("localhost",$user,$password);
if(!$con)
{
echo "could not connect" ;
}
else
{
echo "connected";
}
?>

Then see the result and update here

عثمان غني
  • 2,786
  • 4
  • 52
  • 79
0

Use mysqli_connect instead of mysql_connect.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
George
  • 1