Lewisdow Posted September 9, 2015 Share Posted September 9, 2015 hi I have a basic script that connects to the mysql database server successfully.There are no connection errors however when I query the table the script then reports that 0 rows were returned. I know that there is data available as I can query the table in the mysql server tool.Please could someone point out what may be the problem.Below is the script I am using.thanks, <?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "databasename"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, column FROM databasetable"; $result = mysqli_query($conn, $sql); echo "id: " . $row["id"]. "<br>"; if (mysqli_num_rows($result) > 0) { echo $row; // output data of each row while($row = mysqli_fetch_assoc($result)) { echo $sql; echo "id: " . $row["id"]."<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 9, 2015 Share Posted September 9, 2015 Apply error checking to your query $result = mysqli_query($conn, $sql); // error check query. mysqli_query returned false on error if($result) { // process query results } else { // get the error from the query trigger_error('Query returned an error: ' . mysqli_error($conn)); } Quote Link to comment Share on other sites More sharing options...
Lewisdow Posted September 9, 2015 Author Share Posted September 9, 2015 Hi, thank youNothing was returned from try error, unless I have a setting or something that is hiding the error message?Script below... <?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "databasename"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, column FROM databasetable"; $result = mysqli_query($conn, $sql); $result = mysqli_query($conn, $sql); // error check query. mysqli_query returned false on error if($result) { // process query results if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo $sql; echo "id: " . $row["id"]."<br>"; } } else { echo "0 results"; } } else { // get the error from the query trigger_error('Query returned an error: ' . mysqli_error($conn)); } mysqli_close($conn); ?> Quote Link to comment Share on other sites More sharing options...
Lewisdow Posted September 9, 2015 Author Share Posted September 9, 2015 If I echo in the // get the error from the query I do get a echo back but no error message so perhaps it is being supressed... Can you please tell me how I can show the error?I can not locate the .ini file, perhaps you can provide one?many thanks Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 9, 2015 Share Posted September 9, 2015 Yes, make sure in your php.ini you have error_reporting set to E_ALL and display_errors is set to On. Alternatively add these two line at the top of your script ini_set('display_errors', 1); error_reporting(E_ALL); PHP errors are also logged in your servers error logs. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 9, 2015 Share Posted September 9, 2015 You're not doing a fetch call. query check fetch process fetched row repeat fetch Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 9, 2015 Share Posted September 9, 2015 Upon further review: YOu are doing two query calls. You are trying to grab data before you do the fetch, which I finally noticed. - do the query call (just once!) - check if it ran - check if there are rows If all this works: - loop thru the rows and grab the items from the $row and echo them Quote Link to comment Share on other sites More sharing options...
Lewisdow Posted September 9, 2015 Author Share Posted September 9, 2015 Sorry to be pain, but as a beginner would you mind please providing what that code would look like?thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted September 9, 2015 Share Posted September 9, 2015 $sql = "SELECT id, column FROM databasetable"; "column" is a reserved word - avoid using for column or table names. If you do use it then it needs to be in backticks $sql = "SELECT id, `column` FROM databasetable"; Quote Link to comment Share on other sites More sharing options...
Lewisdow Posted September 9, 2015 Author Share Posted September 9, 2015 Thank you soooo much Sen, that sorted the issue and I am now getting database row results back!! Quote Link to comment Share on other sites More sharing options...
Barand Posted September 9, 2015 Share Posted September 9, 2015 You're welcome Newbie. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.