PHP ignores mysql_connect requests

I am new to PHP and have installed on Linux for download (also newbie). Anyway, PHP works ...

<?
$myVar = "test";
echo($myVar);
?>

      

... works great.

But...

<?

$dbhost = "localhost";
$dbuser = "myuser";
$dbpass = "mypass";
$dbname = "mydb";

echo($dbhost . "-" . $dbuser . "-" . $dbpass . "-" . $dbname);

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to MySQL");
print $conn;

mysql_close($conn);

phpInfo();
?>

      

... doing nothing. No mistakes, no nothing. Its like the code isn't even there.

Any help?

+1


source to share


6 answers


Try the following:



  • First make sure display_errors is enabled in your php config file. Also set the error_reporting level to show all errors, including severe ones (error_reporting = E_ALL | E_STRICT). After making the changes, restart the web server.
  • Run phpinfo () and make sure the mysql extension is installed and working. If it doesn't make sure you uncomment it in the php config file (again, remember to restart apache after every change to the config file).
  • At this point, MySQL should be up and running, and you should be able to tell by mistake (if it persists) what the problem is.
  • Try dumping the contents of the connection ($ conn) to see what it contains.
  • In general, I would recommend using long php tags (<? Php and not <?) As it is more portable (short tags are disabled by default in PHP 5 installations).
+4


source


If it doesn't do anything, doesn't that mean it's connected ok? What result do you expect from this statement?

You may try

error_reporting(E_ALL);
$conn = mysql_connect("localhost", "myusername", "mypassword");
if(!$conn) {
    echo 'Unable to connect';
} else {
    echo 'Connected to database';
}
var_dump($conn);

      



edit: Referring to the comment saying that you have a mysql query setup, if you don't see "success" it means there is something wrong with your query. Add to the above

$sth = mysql_query("SELECT * FROM tablename");
if(!$sth) {
    echo 'unable to query: ' . mysql_error();
} else {
    echo 'success';
}

      

+1


source


Try adding this to the top of your code:

error_reporting(E_ALL);

      

+1


source


Is there more code than you are showing us? The block you just made a connection. You will not see anything, if it succeeds, you have to use $conn

to do something.

To confirm, try changing your password to a known incorrect value, and then see if you have an error. If you do this, the code works fine.

0


source


Connecting to a database using

$conn = mysql_connect("localhost", "myusername", "mypassword") or die("Unable to connect");

      

will not (visible (results if the connection was successful). However, after running this statement, you can use other mysql functions to create queries against the database.

The database connection tells your program "hey, I want to talk to this database".

0


source


This code should create a db connection, nothing else. What do you expect to see?

try it

<?php
$conn = mysql_connect("localhost", "myusername", "mypassword") 
or die("Unable to connect");
print("code sample");

print $conn;

?>

      

It should print you something like "resource # 1" ...

And then you can use this connection to communicate with the db server

0


source







All Articles