php
PHP mysql_connect not returning boolean
I have the following PHP code: $con = mysql_connect("localhost","name","pass") or die(mysql_error()); $db = "db"; mysql_select_db($db,$con); Now in my experience, $con should be true or false. When I echo $con I get: Resource id #25 If I do the following code, the echo never fires (as to be expected after the above statement): if($con) { echo "it worked"; } When I run a query against this connection, everything works as expected. Is there a reason why this $con will not be true or false? What am I doing wrong? Thanks
Check mysql_connect Return Values : Returns a MySQL link identifier on success or FALSE on failure. So to check the connection: if($con !== false) { echo "it worked"; } or to quit in case of an error: if (!$con) { die('Could not connect: ' . mysql_error()); } A word of advice though, better to use the MySQLi or PDO_MySQL instead of mysql_connectsince it will soon be deprecated: Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_connect() PDO::__construct()
Its simply b/c of the or or die(mysql_error()); at the end. Give this a shot: <?php $con = mysql_connect("localhost","name","pass"); $db = "db"; if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db,$con); ?>
I think it is because of the or die(mysql_error()); part. I should be the $con = mysql_connect("localhost","name","pass") only. Then you can check like this: if (!$con).
Related Links
How to save uploaded image to Storage in laravel?
php/sqlite3 - query - mixing data columns in search
Laravel flysystem file put error when using queues
What is the best way to configure email in Codeigniter?
Accept Additional Value Using API Query
How to append query string to url in laravel 5.3
AFHTTPSessionManager post request to create Stripe Connect Account
Rewrite URL change
Magento image configuration from parent configurable product to child simple products
Symfony Bundle: how to avoid using __DIR__ to build the path to Doctrine mappings
Get value out of Array
Displaying product custom fields values in the order once processed
Google Drive API | PHP
How to convert string to UTF-8 PHP
Testing API in Laravel 5.3
Can't read spaces in between words from mysql json php android