0

Here is my code in php:

    <?php echo "hello world";
    $con = mysql_connect("localhost","root","root123");
    echo "Connected";
    mysql_close($con);
    echo  "hell"; ?>

Its printing hello world ,connected ,hell in terminal but it only prints hello world when i run it in the browser. Can anyone tell me why is this happening?

Thanks.

CodeHacker
  • 31
  • 6
  • This wont answer your question, but you should still give this a look. You should not use the deprecated mysql-extension, instead use the newer mysqli-extension. http://stackoverflow.com/questions/8891443/when-should-i-use-mysqli-instead-of-mysql – Tanuel Mategi Oct 27 '15 at 10:46
  • Then why it is working in terminal properly? – CodeHacker Oct 27 '15 at 10:48
  • 1
    you dont catch errors. put this after `mysql_connect`: `if($con === false) { echo mysql_error();}` [php doc](http://php.net/manual/en/function.mysql-error.php) – Tanuel Mategi Oct 27 '15 at 10:49
  • this code is run perfectly.. are you sure you run this file – Mayank Vadiya Oct 27 '15 at 10:56
  • I think your PHP might have no MySQL drivers installed. Check loaded extensions/modules of PHP with phpinfo(). – Pupil Oct 27 '15 at 11:04

4 Answers4

0

Please dont use mysql_*, use mysqli_* instead.

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "collectively-codeigniter";
echo "hello world";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    echo "Not Connected";
} else {
    echo "Connected";
}
// Close connection
$conn->close();

echo "hell";
?> 
Robin
  • 13
  • 3
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
0

May be there has occurred an error in server side and server is not showing informatin about error. You look at error.log.

nagiyevel
  • 397
  • 3
  • 16
0

I do not see syntax errors and since you said that echo() functions before the connection line work, I guess the problem could be the connection:

  1. Replace mysql_connect with mysqli_connect and mysql_close with mysqli_close because mysql_* is now deprecated.

  2. Add the following code after the connection line:

    if (!$con) {
       die("Connection failed: " . mysqli_connect_error());
    }
    

This will check if the connection succeeded and if it failed it will print the error description.

Other things that could cause the problem are:

  • You do not have MySQL modules installed
  • MySQL service is not running (to check windows services: run -> services.msc)

Let me know if this helped you :)

0

You are obviously running into an error server side before the output has a chance to take place. Possibly it's when you try and connect to the mysql server that everything goes south. That results in an error that you have configured PHP not to show (very good for production, not so good when developing), thus you see "nothing".

Start by enabling error reporting in PHP via the php config file or setting

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

at the top of your script. That should give you a clearer picture of what you issue actually is. A guess would be that you are unable to connect to the mysql server due to privileges on the mysql server, the port used, credentials or something of that nature.

Also, as stated, depending on the version of PHP you are running mysql_* command are deprecated and you should instead use the mysqli_* functions, PDO or something like that.

inquam
  • 12,664
  • 15
  • 61
  • 101