0

I just learnt how to connect to a DB. However I intentionally put the wrong user to return an error. But it doesn't, it keeps showing me that I am connected.

<?php


$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';

@mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die('Sorry');

echo 'Connected';
?>

In this case it shows connected because there is a user named root in my DB, but if I replace with anything else, it still shows Connected and not Sorry.

I am not allowed to answer my own question because of less reputation apparently and I was asked to wait for some 8 hours or edit my question and fix it. (I hope you guys give me some ticks so that I can answer too :P Just kidding)

So here is the fix:

When I went to users overview tab under localhost link, it showed "User: Any", "Host: localhost", "Privileges: Usage". I just deleted that thing. And kept the pma, linux and root thing alone. Removed that extra user (don't know how it came) and that fixed the problem.

So basically for any user, it was connecting and giving access.

I don't know if I am allowed to answer my own question but since I figured it out I am posting it so that it helps someone when they are in need and are on a similar boat.

Sorry and thanks to everyone :)

Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
  • Go try this on your localhost and phpmyadmin! There is no reason as to why I would do such a thing. All my questions asked before prove that I am a learner and my doubts have been very basic at times. And I have every right to ask about it. And please don't forget end of the day we are all humans. We make mistakes, sometimes very silly, but mistakes are mistakes! – Ankur Sinha Nov 30 '12 at 13:26

2 Answers2

1

Firstly as other said, you should used mysql library anymore

But as far your question goes:

mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die('Error'.mysql_error());

Also, "or die" statement shouldn't be use really in anything it kills the script and uslaly you want to "die" gracefully

Elijan
  • 1,406
  • 1
  • 8
  • 11
  • I was just learning bro. That's what the video showed and I followed his footsteps. I know die kills the page but there was nothing after it either ways. And don't worry, I will keep your tip in mind :) – Ankur Sinha Nov 30 '12 at 13:28
  • @WebMeister all good dude. we all had to learn and we are all stil learning as we go, for learning purposes you still can use mysql library (as there is abundance of tutorials as one you have follow) but when try to find tutorials with PDO extension. – Elijan Nov 30 '12 at 14:02
0

Error messages are being suppressed by the @ sign. Remove it and you will see message in die()

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

Community
  • 1
  • 1
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
  • The purpose of @ was to hide the error from the server and DB end but it should print the sorry message right? Never mind, I remove the @ and still got connected. – Ankur Sinha Nov 30 '12 at 13:08
  • If your still connecting then you may not have set a password for `root` – ajtrichards Nov 30 '12 at 13:09
  • No, I don't but how is it that that tutorials I followed didn't have the password set but got the error like it happened and then added the "@" too and still got it. – Ankur Sinha Nov 30 '12 at 13:12
  • Are you running Windows / Linux on your MySQL host? – ajtrichards Nov 30 '12 at 13:14