Jump to content

Myqli doesn't return anything?


crf1121359

Recommended Posts

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:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "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 amount
if ($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 by crf1121359
Link to comment
Share on other sites

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 <?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?> anyway..

 

cheers anyway.

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D 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.

Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.