tags:

views:

235

answers:

8

I'm new to PHP and have installed on Linux to boot (also a newbie). Anyway, PHP is working...

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

... works just fine.

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();
?>

... does nothing. Nor errors, nothing. Its as if the code isn't even there.

Any help?

+1  A: 

If it does nothing, doesn't that mean that it connected fine? What output do you expect out of that statement?

You could 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: Addressing the comment saying that you have a mysql query setup, if you are not seeing "success" it means something is 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';
}
Paolo Bergantino
I have if statement under if (mysql_query("select * from table")) { print "success"; }
Gene R
+1  A: 

Is there more code than you're showing us? The block you have just sets up a connection. You won't see anything at all if it succeeds, you have to use $conn to do something.

To confirm, try changing your password to a deliberately wrong value, and then see if you get an error. If you do, the code works just fine.

Adam Bellaire
A: 

Connecting to a database with

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

will have no (visible( results if the connection was made succesfully. However, once you run this statement, you can use the other mysql functions to make make queries to the database.

Connecting to a database tells your program "hey, I want to talk to this database".

Alan Storm
A: 

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

Try this

<?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 may use that connection to communicate with db server

andy.gurin
+4  A: 

Try to do the following:

  1. First make sure display_errors is turned on in your php configuration file. Also set the level of error_reporting to show all errors, including strict (error_reporting = E_ALL|E_STRICT). After you make changes, restart your webserver.
  2. Run phpinfo(), and check that the mysql extension is installed and working. If it isn't make sure that you uncommented it in the php configuration file (again, remember to restart apache after each change to the configuration file).
  3. At this point MySQL should be loaded and working, and you should be able to tell from the error (if it persists) what's the problem.
  4. Try also dumping the contents of the connection result ($conn) to see what it contains.
  5. In general, I'd recommend using long php tags (<?php and not <?) since it is more portable (short tags are off by default in PHP 5 installations).
Eran Galperin
Ok, thats a start. Enabled errors, and nothing. But phpinfo() told me the following. Nowhere in the output is mysql found except in the Configure Command section, I see --without-mysql.I'm assumint that's self explanatory. However, how do I turn mysql on?
Gene R
did you uncomment the mysql extension in the PHP configuration file?Also did you check that the MySQL server is running?
Eran Galperin
I don't see anything in the php.ini file with mysql relations that is commented out. And yes, mysqladmin and coldfusion both return results from mysql. Does apache need any sort of mysql module?
Gene R
I've added my answer with another suggestion for debugging (4,5) also, as others have suggested the problem might be with the sorrounding code. Please post some more of your code.
Eran Galperin
I've edited my original question with ALL of the code I have on this page. If I comment out the $conn line, I get results, errors, or whatever. If I leave the $conn line in, it STOPS dead, doesn't display the phpInfo() or anything.
Gene R
Are you sure you have display_errors to on and that you restarted your webserver after the change? I'd recommend looking over the apache error logs
Eran Galperin
Crap, I had turned display_errors = ON in the wrong place, up in the description. I fixed that, and got... Fatal error: Call to undefined function mysql_connect() in /var/www/html/ratedate/index.php on line 26
Gene R
then your mysql extension is not loaded. Check that it appears in the php configuration file, and that the extension itself exists in the path specified in the file
Eran Galperin
+1  A: 

Try adding this to the top of your code:

error_reporting(E_ALL);
Andrew G. Johnson
A: 

So what does var_dump($conn) give you as an output if you place it after this line?

Is it maybe just doing exactly what you asked for and storing a connection in $conn?

feihtthief