heldenbrau Posted July 30, 2009 Share Posted July 30, 2009 What is wrong with this, I just want it to print what's in the rating field, but nothing happens. <?php $mysqli = new mysqli("localhost", "user", "password", "user"); if ($mysqli === false) { die("ERROR: Could not connect to database. " . mysqli_connect_error()); } $sql = "SELECT rating from users where username = \"Chris\""; if ($result = $mysqli->query($sql)){ echo "$result"; } else { echo "no results"; $result->close(); $mysqli->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/ Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 SQL requires single quotes for text fields (i.e. around 'Chris'), double quotes wont work from my understanding. your bigger problem though is you do not close the bracket for your else statement which is probably causing the page to not show. You need to turn on error reporting to see the errors for stuff like this Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887082 Share on other sites More sharing options...
heldenbrau Posted July 30, 2009 Author Share Posted July 30, 2009 Thanks that has got the page showing, but now it says no results. I have changed the script to <?php $mysqli = new mysqli("localhost", "user", "password", "user"); if ($mysqli === false) { die("ERROR: Could not connect to database. " . mysqli_connect_error()); } $sql = "SELECT rating from users where username = \'Chris\'"; if ($result = $mysqli->query($sql)){ echo "$result"; } else { echo "no results"; } $result->close(); $mysqli->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887087 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 take out the slashes <?php $sql = "SELECT rating from users where username = 'Chris'"; I am not entirely sure if this will work with your class structure, but try it <?php $result = $mysqli->query($sql) or die(mysql_error()); if it doesn't, then you should just take exactly what you have for your $sql statement and throw it into your sql query engine and try to run it manually to see what the errors are Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887093 Share on other sites More sharing options...
J.Daniels Posted July 30, 2009 Share Posted July 30, 2009 If you are using single quotes inside of double quotes, you don't have to escape them. $sql = "SELECT rating from users where username = 'Chris'"; As a side note, MySQL works fine with double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887095 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 maybe its just MSSQL, because when I run this select * from table where action like "%login%" i get "Invalid column name '%login%'" Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887098 Share on other sites More sharing options...
heldenbrau Posted July 30, 2009 Author Share Posted July 30, 2009 It works fine when I type it directly into mysql. I have taken out the slashes and it goes back to nothing happening. <?php $mysqli = new mysqli("localhost", "user", "password", "user"); if ($mysqli === false) { die("ERROR: Could not connect to database. " . mysqli_connect_error()); } $sql = "SELECT rating from users where username = 'Chris'"; if ($result = $mysqli->query($sql)){ echo "$result"; } else { echo "no results"; } $result->close(); $mysqli->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887099 Share on other sites More sharing options...
Philip Posted July 30, 2009 Share Posted July 30, 2009 <?php $mysqli = new mysqli("localhost", "user", "password", "user"); if ($mysqli === false) { die("ERROR: Could not connect to database. " . mysqli_connect_error()); } $sql = "SELECT rating from users where username = 'Chris'"; if ($result = $mysqli->query($sql)){ echo "$result"; } else { echo "no results, but here is the error: ",$mysqli->error; } $result->close(); $mysqli->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887103 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 also, put this at the top of your page to view any other errors <?php error_reporting(E_ALL); ini_set("display_errors", 1); Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887106 Share on other sites More sharing options...
heldenbrau Posted July 30, 2009 Author Share Posted July 30, 2009 I put that at the top and I get this error Catchable fatal error: Object of class mysqli_result could not be converted to string Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887109 Share on other sites More sharing options...
J.Daniels Posted July 30, 2009 Share Posted July 30, 2009 That is because you are trying to echo out the results of the query: $result = $mysql->query($sql); if ($row = $result->fetch_assoc()){ echo $row['rating']; } else { echo "no results, but here is the error: ",$mysqli->error; } Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887112 Share on other sites More sharing options...
heldenbrau Posted July 30, 2009 Author Share Posted July 30, 2009 After replacing the code with the above it now says undefined variable and Fatal error: Call to a member function query() on a non-object. Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887117 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 change $mysql->query() to $mysqli->query() Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887118 Share on other sites More sharing options...
heldenbrau Posted July 30, 2009 Author Share Posted July 30, 2009 Yay, that's done it , wish I understood it though. It'll come to me. Thanks for that. Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887132 Share on other sites More sharing options...
heldenbrau Posted July 30, 2009 Author Share Posted July 30, 2009 Now I need to do the same thing, but with a date field. The fetch_assoc()) doesn't work with the date field. Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887170 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 post the code and the error you are getting. did you change your original SQL query, because there you were only retrieving the one field, "rating" so if you try to grab the date field from that it would throw an error Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887176 Share on other sites More sharing options...
heldenbrau Posted July 30, 2009 Author Share Posted July 30, 2009 don't worry, my fault, was using the wrong field name. It does work with dates. Quote Link to comment https://forums.phpfreaks.com/topic/168194-solved-cant-get-php-to-print-a-result-of-an-sql-query/#findComment-887182 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.