1

I creating a php file that pulls a view from an SQL database. Can someone let me know why this isn't working? It seems to be timing out. I am not getting a connection error, either. Thank you in advance.

<?php       
require ('mysqli_connect.php');         

    $sql = "SELECT * FROM testview ;";
    $result = mysqli_query($dbc,$sql);

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

     $result=mysqli_query($sql);

     if ($result->num_rows > 0) {
         echo "<table><tr><th>userID</th><th>first_name</th></tr>";
         // output data of each row
         while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["userID"]."</td><td>".$row["first_name"]."</td></tr>";
}
  echo "</table>"; } else {
  echo "0 results"; }

} 
$dbc->close(); 

?>

Here is the connection file

<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Test');

// Make the connection:
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' );
?>
17
  • For starters your mixing mysql and mysqli which doesn't work. Switch everything to mysqli. And the connection seems to be both $conn and $dbc? Commented Apr 4, 2015 at 20:53
  • shoot, that is just a typo. I cannot get mysqli_connect to work on my computer, so I am switching it to mysql_connect. This is just a test. I will correct that typo. Commented Apr 4, 2015 at 20:54
  • Show your connection code if you are getting connection error Commented Apr 4, 2015 at 20:56
  • I am not getting a connection error. @anantkumarsingh. I am just not getting any output Commented Apr 4, 2015 at 20:56
  • try to remove second mysql_query code and put mysqli_* every where? Commented Apr 4, 2015 at 20:58

2 Answers 2

1

give this a go. you were using mysqli and mysql in the same document. this somtime causes issues.

require_once ('mysqli_connect.php');         

$q = "SELECT * FROM testview";
$r = mysqli_query($dbc, $q);

//there was no real need to check the connection, you should be doing this in your connection script.

//you where using 'mysqli' above and 'mysql' below. 
$row = mysqli_fetch_array($r);

if ($r) {

   echo "<table><tr><th>userID</th><th>first_name</th></tr>";

     while ($row = mysqli_fetch_array($r)){

         echo "<tr><td>" . $row["userID"] . "</td><td>" . $row["first_name"] . " " . $row["last_name"] . "</td></tr>";
     }
         echo "</table>"; 
} else {
         echo "0 results"; 
}

close($conn);
Sign up to request clarification or add additional context in comments.

3 Comments

Not sometimes - mysqli and mysql are like oil and water. They just don't mix.
Thank you @ballbern. After made your suggested changes, it worked.
@balibern just remove the first $row to include all returned data
0

You do not have error messages turned on. Therefore when you are getting syntax errors and etc. they are not showing up and you are assuming a time-out. OR perhaps since you have a non-existent function in your die() call maybe it gets confused trying to die but not able to die.

Before you euthanize your code, turn error messages on. Your code will thank you.

Oh, and change

die("Connection failed: " . $dbc->connect_error);

to

die("Connection failed: " . mysql_error($dbc));

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.