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(); ?> 
2
  • 1
    How come error message will output the PHP codes ? The die() does not behave like this. Weird Commented Jan 16, 2015 at 3: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 Commented Jan 16, 2015 at 4:59

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OP. your code is failing on the "Check connection" I agree with @PressingOnAlways answer
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.

Comments

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

Comments

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)

Comments

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();
?> 

Comments

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.