2

I'm expecting the following code to produce an error in PHP, but it runs without any sign of error.

<?php

error_reporting(E_ALL);
ini_set('display_errors','On');

$link = mysql_connect('localhost','JUST_ANY_TEXT EVEN WITH SPACES', '');

mysql_select_db('JUST_ANY_TEXT EVEN WITH SPACES', $link);

echo 'LINK RESULT = '.($link?'OK':'ERROR'); //THIS PRINTS: LINK RESULT = OK

echo mysql_query("SELECT INVALID_TEXT FROM TABLE_THAT_DOES_NOT_EXISTS_ANYWHERE PLUS ANY TEXT 3[t3p36g333 3 4638343´tp4oy0i9u54t-9u´089hwpjoeg~,ç");
1
  • Does it at least show a warning? Commented Feb 18, 2012 at 15:37

2 Answers 2

3

This works as intended. If you check out the official documentation you will see that there is a return value form most of the functions. You should check that return value to make sure everything is fine.

Also these legacy mysql_* functions are quite outdated. I would suggest using the PDO package instead.

To point out the immediate error in your code. You should use it like:

$success = mysql_select_db('JUST_ANY_TEXT EVEN WITH SPACES', $link);
if (!$success)
    die ('Error selecting the database!');
Sign up to request clarification or add additional context in comments.

8 Comments

mysql_connect and mysql_select_db returns TRUE (mysql_connect returns the resource id). only the mysql_query results FALSE.
I agree, try using the mysqli functions
@vbence, $success value is FALSE. This solves my problem! But, is there a way to know when mysql_connect fails? Or I got to try to select a database for this?
@DMF From mysql_connect's documentation: Returns a MySQL link identifier on success or FALSE on failure.
I know, but any username I use, it returns MySQL link identifier.
|
1

For the mysql_query() you have to check the result of mysql_query() and use mysql_error() to get the MySQL error message:

$query  = "invalid query";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);

However, mysql_connect() should raise an error in case of wrong credentials.
Are you sure you can see errors occurred? Whatever PHP errors?

3 Comments

then check your credentials if they are really such "ANY TEXT WITH SPACES". Both mysql_connect and mysql_select_db raises a regular PHP error. However, you still can use mysql_error() if you don't sure.
Now I realized that mysql does not throws error only for the username parameter. The others parameters such as servername and password, throws error when using wrong credentials.
@YourCommonSense, Wow, is colonel back?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.