crf1121359 Posted March 8, 2013 Share Posted March 8, 2013 (edited) Hi,I have been trying to SELECT from mysql databse using mysqli query with no success!I am quite new to mysqli so bear with me.I have looked on the PHP site as well but they don't realy make sense if i'm honest!This is what I have done so far:<?phperror_reporting(E_ALL);ini_set('display_errors', '1');?><?php// Run a select query to get my letest 6 items// Connect to the MySQL databaseinclude "config/connect.php";$dynamicList = "";$sql = "SELECT * FROM users order by id";$query = mysqli_query($db_conx, $sql);$productCount = @mysqli_num_rows($sql); // count the output amountif ($productCount > 0) {while($row = @mysqli_fetch_array($sql)){$id = $row["id"];$username = $row["username"];$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));$question = $row["question"];$dynamicList .= '<div style="width:550px;"> <hr style="color:#E9E9E9; border:dashed thin;"/><a href="questions.php?">' . $question . '</a><hr style="color:#E9E9E9; border:dashed thin;" /><br ></div>';}} else {$dynamicList = "NOTHING TO SHOW";}?>i'm not getting any erros in my php page but it doesn't show the mysql database details either!!i also have <?php echo $dynamicList; ?> where i want the data to be shown on my page! where i have placed the <?php echo $dynamicList; ?> will show the NOTHING TO SHOW message!any help would be greatly appreciated.Thanks Edited March 8, 2013 by crf1121359 Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 8, 2013 Share Posted March 8, 2013 You're suppressing errors and not catching others. Read the debugging SQL link in my signature. Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 8, 2013 Author Share Posted March 8, 2013 Thanks Jessica, I did place the "or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);" after my mysqli query and still didn't come back with any errors. also, at the top of my php code I already had <?phperror_reporting(E_ALL);ini_set('display_errors', '1');?> anyway.. cheers anyway. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 8, 2013 Share Posted March 8, 2013 You need to remove all the @ What do you get when you run the query in mysql? Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 8, 2013 Author Share Posted March 8, 2013 I have started with a code without them @'s but i was keep getting Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given in line 12 which is $productCount = mysqli_num_rows($sql); But when i place the @ signs in the code, that error goes away. as i said, i am not getting any error, i even get a custom message taht will let me know that the script is connected to the database successfuly but I don't understand why it doesn't show the mysql data as it should! I bloody, truely HATE mysqli and PHP documentation. there is no reason for them to make it as hard as possible to learn!! Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 8, 2013 Share Posted March 8, 2013 You're not FIXING the error, you're HIDING it. You ARE getting errors, you just IGNORED them. That error is VERY VERY useful. It's telling you it expects a specific object. You're giving it a string. Look at your code - what argument are you giving mysqli_num_rows? It's not "as hard as possible", it's actually very clear English. Slow down and think about what each line does. Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 9, 2013 Author Share Posted March 9, 2013 lol, Jessica, I have to see the "error" to be able to fix it or not? I have tried your error reporting on your debugging SQL link and no errors were showing!! I don't know how else I can see the errors that are not showing on the page!!! and by the way, i don't usually go to the shops and say can I have mysqli_num_rows of oranges so i don't really think that is plane english Just kidding. anyway, as I mentioned above, I am very very new to mysqli and what have you... and to be honest everywhere i've looked on Google, someone's complaining about PHP documentations and how hard they are for someone with untrained eyes. Thanks for your help Jessica. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 9, 2013 Share Posted March 9, 2013 *smh* I just told you what the problem was. Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 9, 2013 Author Share Posted March 9, 2013 (edited) can you say it in a terms that my slow brain understand it as well please? I'm so lost here!!!! some sort of example!! Edited March 9, 2013 by crf1121359 Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 9, 2013 Share Posted March 9, 2013 Remove all the @ and read the line the message tells you is wrong. Quote Link to comment Share on other sites More sharing options...
kicken Posted March 9, 2013 Share Posted March 9, 2013 (edited) lol, Jessica, I have to see the "error" to be able to fix it or not?You have seen the error:I have started with a code without them @'s but i was keep getting Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given in line 12 which is $productCount = mysqli_num_rows($sql); But when i place the @ signs in the code, that error goes away.It "goes away" because you told PHP to ignore it, not because you fixed it. You still have the error, you just told PHP you don't care. That's the exact same logic as if you had a broken window in your house and you just hung a painting over it to "fix it", then in the winter you complain because the inside of your house is freezing cold when you obviously "fixed" the broken window. Edited March 9, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 9, 2013 Author Share Posted March 9, 2013 right I have removed the @s and also given the mysqli_num_rows an object "$db_conx" instead of an string but i get this error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in line 12. line 12 is the same line as i mentioned above!! Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 9, 2013 Share Posted March 9, 2013 That's not the right object. It expects a result set. What in your code creates a result object? Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 9, 2013 Author Share Posted March 9, 2013 i think you guys are having a right laugh lol Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 9, 2013 Author Share Posted March 9, 2013 all i needed was this: $query, MYSQLI_ASSOC and i was pointed in the wrong direction! Thanks anyway Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 9, 2013 Share Posted March 9, 2013 Uhm, yes. $query is what you called your result set. What we were telling you to change. Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 9, 2013 Author Share Posted March 9, 2013 but i didn't change that. I just added $query, MYSQLI_ASSOC as well as the rest of the code and its working now. am i doing somethign wrong? i don't want to run this and find out i've done something wrong in the future! Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 9, 2013 Share Posted March 9, 2013 You added it *where*? Post your new code. Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 9, 2013 Author Share Posted March 9, 2013 here : while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 9, 2013 Share Posted March 9, 2013 Yes. You were using $sql which was a string. Now you're using the CORRECT result object. If you don't understand why the code works you need to read about it and learn. You still need to remove all your @. That's bad practice. We told you the problem and how to fix it. Quote Link to comment Share on other sites More sharing options...
crf1121359 Posted March 9, 2013 Author Share Posted March 9, 2013 righty, Cheers Jessica, appreciate it. 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.