3

I already have my database named als and I still got the error.

<?php

$mysql_host='localhost';
$mysql_user='root';
$mysql_password='';
$mysql_db='als';
$con = @mysql_connect($mysql_host,$mysql_user,$mysql_password) or             die(mysql_error());
@mysql_select_db($mysql_db) or die(mysql_error());

?>
7
  • 3
    The mysql_* extension is deprecated. Better pick another API to connect to the MySQL server. See docs.php.net/mysqlinfo.api.choosing Commented Jul 14, 2015 at 0:19
  • 2
    Remove @ and post your errors. Commented Jul 14, 2015 at 0:21
  • Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\Website\connect.php on line 9 @DaveChen Commented Jul 14, 2015 at 0:27
  • 2
    @MelvinJayBelenario Yep, you need to use PDO or MySQLi. Commented Jul 14, 2015 at 0:32
  • 2
    Didn't say that it switching to mysqli or PDO will fix that particular problem. Still mysql_* is deprecated - even your version of php says so. And since it looks like you don't have a ton of legacy code but are beginning your journey: now is the time to switch and use an API that is under active development and support. Commented Jul 14, 2015 at 0:34

2 Answers 2

2

Not exactly an answer to your question but too long for a comment:
After establishing the database connection you could just query the existing databases via SHOW DATABASES

<?php
$mysqli = new mysqli('localhost', 'root', '');
if ($mysqli->connect_errno) {
    trigger_error('query failed: '.$mysqli->connect_error, E_USER_ERROR);
}

$result = $mysqli->query('SHOW databases')
    or trigger_error('connect failed: '.join(',', $mysqli->error_list), E_USER_ERROR);

foreach( $result as $row ) {
    echo join(', ', $row), "<br />\r\n";
}

Does your database als show up?
Since you're using the default root account (with an empty password; you might want to look into that as well) there shouldn't be any permission related problems. So, if the database doesn't show up, it's just not there...

(almost) same script using PDO (my weapon of choice) instead of mysqli:

<?php
$pdo = new PDO('mysql:host=localhost;charset=utf8', 'root', '', array(
    PDO::MYSQL_ATTR_DIRECT_QUERY => false,
    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));

foreach( $pdo->query('SHOW DATABASES', PDO::FETCH_NUM) as $row ) {
    echo $row[0], "<br />\r\n";
}
Sign up to request clarification or add additional context in comments.

3 Comments

My database als did not appear
Then So, if the database doesn't show up, it's just not there... applies and that means something went wrong with I already have my database named als ;-)
ill try to reinstall/config mysql. Thanks by the way sir. :)
0

There you go. The mysql_ family has been deprecated for some time. Please change to the mysqli_ library. Another machine may work because it's using an older version of PHP in which it hasn't been deprecated OR where deprecated warnings have been globally supressed.

MySQLI Connect

In the wild

$mysql_host='localhost';
$mysql_user='root';
$mysql_password='';
$mysql_db='als';
$con= mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_db) or die("Error " . mysqli_error($con));

There's no need to arbitrarily select the database anymore. Now you can use $con as an argument to the mysqli_ family of procedural functions.

Last, but not least, never debug with the @ symbol. This suppresses the error warnings from the function it precedes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.