0

i am new to PHP, and i am attempting to create a simple connection from html to mySQL using php. I met with some problems when running my codes.

this is my code:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT username FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> id: ". $row["userid"]. ;
     }
} else {
     echo "0 results";
}

$conn->close();
?> 

after running on a browser, this is displayed:

connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT username FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "
id: ". $row["userid"]. ; } } else { echo "0 results"; } $conn->close(); ?> 
student15
  • 1
  • 1
  • 1
  • 1
    How come error message will output the PHP codes ? The `die()` does not behave like this. Weird – Raptor Jan 16 '15 at 03:43
  • This isn't an answer, but it's just a good practice. You should use constants for your connection variables, as you won't be changing them. It's a perfect place to use them – Daniel Jan 16 '15 at 04:59

5 Answers5

3

Do you have mysql running on your localhost machine? You must verify that it is working first before you can connect via php. Also, make sure you have TCP/IP sockets open in mysql and to make sure it isn't just listening via unix sockets.

PressingOnAlways
  • 11,948
  • 6
  • 32
  • 59
1
  echo "<br> id: ". $row["userid"]. ;

this is a syntax error, no need to end it with a full stop.also the correct syntax to connect to sql server is

mysqli_connect("localhost","my_user","my_password","my_db") or die("");

Debug the code before posting it here.

abhishek
  • 56
  • 8
0

maybe try testing it with a try catch statement. Its what I've done. this way you can display your error messages a little more nicely. As for the cause, PressingOnAlways is probably right

Termonator145
  • 109
  • 1
  • 14
0

If you are using wampserver or any other server you need to put your php files into c:\wamp\www folder and run them in browser by typing localhost/nameofyourfile.php (change c:\wapm\www for your server installation path or type)

0

you have the syntax error in blow line

echo "<br> id: ". $row["userid"]. ;

. (dot) should not be there.

second you fetch usernamestrong text by following query

$sql = "SELECT username FROM users";

but you try to get userid

echo "<br> id: ". $row["userid"]. ;

try below code.

<?php
    $servername = "localhost";
    $username = "root";
    $password = "password";
    $dbname = "databasename";

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

        // Check connection
        if ($conn->connect_error) {
             die("Connection failed: " . $conn->connect_error);
        } 

        $sql = "SELECT usersname FROM users";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                 echo "<br> id: ". $row["usersname"] ;
            }
        } else {
         echo "0 results";
        }

    $conn->close();
?> 
always-a-learner
  • 3,671
  • 10
  • 41
  • 81