-5

I'm trying to learn this stuff. Please be gentle.

Something is wrong here:

$dbhost = "localhost";
$dbname = "something_dbname"; 
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$select_db = mysql_select_db($dbname . $dbconnected) or die ($dberror1);

where is my mistake? I want $dbconnected to show... I can just as easily use

echo "hello";

it shows that I connect but I'm trying to get familiar with using multiple variables.

would this be better?

$dbhost = "localhost";
$dbname = "something_dbname"; 
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";

if ($mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname))
echo $dbconnected;
else die($dberror1);
baltar bsg
  • 15
  • 1
  • 7
  • 3
    Please, please....switch to PDO/prepared statements. You're just starting out. So it would be much easier to do so at this stage – asprin May 15 '14 at 06:51
  • possible duplicate of [Replacement for deprecated function mysql\_connect](http://stackoverflow.com/questions/16531252/replacement-for-deprecated-function-mysql-connect) – mate64 May 15 '14 at 06:54
  • check your $dbhost. what value in it? – wild May 15 '14 at 06:54
  • $dbhost is fine...localhost. Can you guys PLEASE stop voting me down at this stage. I had an 8 rep now I have a 2 rep. give me a break – baltar bsg May 15 '14 at 06:58
  • only pass db name mysql_select_db($dbname) – Rakesh Sharma May 15 '14 at 06:58
  • that is wrong syntax $select_db = mysql_select_db($dbname . $dbconnected) You can use only database name. according to your code database name is `something_dbname you are connected!` – wild May 15 '14 at 06:59
  • @asprin can you elaborate just a bit? – baltar bsg May 15 '14 at 06:59
  • @wild so would ('$dbname' . '$dbconnected') work? I basically want the php page to say I am connected without me resorting to echo or print – baltar bsg May 15 '14 at 07:01
  • There is no way to display a variable value on a page without using `echo` or `print` or `print_r` – asprin May 15 '14 at 07:09
  • oh ok ...thank you @asprin. when does ( $x . $y ) work? if I may ask? – baltar bsg May 15 '14 at 07:11
  • `$x.$y` will only concatenate (join) the values of `$x` and `$y`. It won't be print anything on the page. For example: `$x="abc"; $y="def";` So `$x.$y` will be equal to `abcdef` – asprin May 15 '14 at 07:13

2 Answers2

1

Right now you are trying to connect to a database called something_dbnameyou are connected. The . concatenates variables into one string.

To fix your immediate problem, try this:

First, define $dbhost - I don't see it in your code.

Then change the last line to this:

$select_db = mysql_select_db($dbname) or die ($dberror1);

Then, just echo $dbconnected;

If you are not connected, the page will have called die, and will never reach the line that echos $dbconnected. If you are connected, the program will proceed to this next line and echo your success message.

Or you can do it more explicitly like this:

if ($select_db = mysql_select_db($dbname))
    echo $dbconnected;
else die($dberror1); 

To fix the bigger problem, DON'T use mysql_*. Read this for more information.

mysqli or pdo are far better options, and you can accomplish the same task easier, for instance, connecting to a db with mysqli is just:

$mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);

Or you can do it procedural style, which is closer to your current code. The following snippet is from the php manual, on the page I linked in the comment below.

$link = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);

if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo 'Success... ' . mysqli_get_host_info($link) . "\n";

mysqli_close($link);
Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • so I can use $mysqli right after I define all my variables. I would avoid the if-else statement and my $conn and $select_db? – baltar bsg May 15 '14 at 07:18
  • @baltarbsg That's right. Then you would check `if ($mysqli->connect_error)` to check for any errors. See Example 1 [**on this page**](http://www.php.net/manual/en/mysqli.construct.php) for more information. – Mark Miller May 15 '14 at 07:22
  • @baltarbsg Take a look at my update. Also, to answer your question regarding the update to your question about using `PDO` instead of `mysql_*` - yes, that is a better method. Either `PDO` or `mysqli` are good choices. – Mark Miller May 15 '14 at 07:35
  • is this adequate? After defining variables, if ($mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname)) echo $dbconnected; else die($dberror1); – baltar bsg May 15 '14 at 07:35
  • hey @Mark M I think I prefer $mysqli method over the procedural... is there any advantage to latter other than being closer to my original? Also has $sqlCommand also been deprecated? – baltar bsg May 15 '14 at 07:44
  • @baltarbsg OOP is more advantageous than procedural. If that is what you like better yes you should use it. I'm not sure what `$sqlCommand` is, I don't think that is any sort of PHP function or built in variable. The links I provided will tell you everything you need to know. It might be confusing at first but just practice and try out different things. You will get the hang of it very soon. To answer your first question, no, I don't think that statement will work. After the `... new mysqli(...` line, try `if ($mysqli->connect_error) die($dberror1); else echo $dbconnected;` – Mark Miller May 15 '14 at 07:49
  • @baltarbsg From looking at your previous question I see what you meant by `$sqlCommand` and I think you are missing the point. It is just a variable name - it could be anything, `$sql` or `$command` or `$whatever`... – Mark Miller May 15 '14 at 08:01
1

I'd strongly recommend using PDO. The connection string is similar and can be done using:

// I do not see $dbhost defined in your code. Make sure you have it defined first
try {
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo $dbconnected; // will print out the connection success message
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

To answer your question about not using mysql_* functions, you can check out this

Community
  • 1
  • 1
asprin
  • 9,579
  • 12
  • 66
  • 119