0

I've installed xampp and it seems to be up and running, I have tested php by executing the phpinfo() function and it works. I can create databases and manipulate them in phpmyadmin, and the localhost server works too

How ever when I attempt to actually connect through php....

<?php
$conn = mysqli_connect('localhost', 'root', '');
mysql_select_db("testskills");
if(!$conn) {
die("Connection Failed: ". mysqli_connect_error());
}

..... I don't get any errors but the code breaks and the browser just shows me the actual code from the file the form action called

I'm stumped

Lori

Lori
  • 1
  • 1
  • First: `phpmyadmin` _does_ an actual connection. Second: if that works, then the php setup on your system works. So the question is why it does _not_ work for that tiny script of yours. Please take a look into your http servers error log file. – arkascha Oct 05 '16 at 14:14
  • One issue certainly is that you mix the old and new mysql extension. You _cannot_ mix `mysqli_...()` and `mysql_...()` functions. The old, outdated and long deprecated `mysql_...()` functions should _not_ be used any more. That is clearly documented. – arkascha Oct 05 '16 at 14:15
  • Note: `mysql_select_db()` is deprecated, so try this: `mysqli_select_db ( $conn , "testskills")` reference: http://php.net/manual/en/mysqli.select-db.php – Felix Feliciant Oct 05 '16 at 14:15

2 Answers2

0

Try this

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29
  • There are so many of these *"try this"* answers (lately), they explain nothing as to why they should "try". What if someone told you to "try this pair of shoes" on. What will you answer them? Think about it for a minute. – Funk Forty Niner Oct 05 '16 at 14:29
  • How does my production app work if I call `die` ? – Drew Oct 05 '16 at 14:53
0
<?php
     $host_name = "localhost";
     $u_name    = "root";
     $u_pass    = "";
   try {
    $dbh = new PDO("mysql:host=$host_name;dbname=personal_db", $u_name, $u_pass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $ex) {
    echo "Connection failed: " . $ex->getMessage();

}
?>

I would suggest that you start learning to use PDO man.

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • so PHP.net should just throw out mysqli_, right? – Funk Forty Niner Oct 05 '16 at 14:30
  • I didn't say that, there are many advantages of using PDO rather than mysqli, mysqli support one database driver, while PDo supports 12, there are times when you have to move your project to different DBMS, with mysqli you may need to start your application(queiries) from stretch while with PDO it won't be a mission. – Masivuye Cokile Oct 05 '16 at 14:39
  • 1
    Nonetheless, using PDO on its own without a prepared statement is useless. Having phrased it as: *"I would suggest that you start learning to use PDO with a prepared statement man."* would have been better. Maybe you know that and I know that, but the OP and others may not ;-) – Funk Forty Niner Oct 05 '16 at 14:42
  • why would you use PDO without prepared statements? Everyone should know that when we talk PDO we talk of prepared statements, there was no need to spell that all out we not kids here. – Masivuye Cokile Oct 05 '16 at 14:44
  • 1
    So you think that everyone knows that just because "you" know that? C'mon.... don't be so presumptious. – Funk Forty Niner Oct 05 '16 at 14:45
  • its their duty to findout more man, we not here to spoon food. – Masivuye Cokile Oct 05 '16 at 14:46
  • 1
    a ton of people just convert to new db layers without actually doing proper prepares. If in doubt, look at the rep 1 questions that flow in – Drew Oct 05 '16 at 14:50
  • 1
    you're NOT serious about that. That isn't the right attitude at all. I for one didn't have the faintest idea for a lot of code years ago as I for one wasn't born with it set in my DNA, which seems to be the case with you I see. *lol!*. Don't bother answering this comment, as I won't be going any further with this; as there shouldn't have been one to begin with. P.s.: You did "spoonfeed" them. – Funk Forty Niner Oct 05 '16 at 14:51