Jump to content

Mysqli_fetch_array


23.12.2012

Recommended Posts

Well... I'm working on a dynamically-generated kind of table. It's not an HTML table, but a combination between CSS and HTML, in order to make the page look like a table. The HTML/CSS part is fine, but it's the PHP/SQL that kills me. It's like this: the user enters some search terms, the search is run, and the results are displayed in a table-like manner. So in order to display the results, I use while($row = mysqli_fetch_array($db_data)) { echo "<p><a href='#'>" . $row['name'] . "</a></p>"; }It works fine, but 75% is fixed, and 25% is generated using the above code. So the name is displayed correctly, but if I want to display an email address, it doesn't get any data from the database. I figured out I could do it using arrays, storing each type of information (name, address, email, etc) in their respective arrays. But it seemed rather complicated, so I'd have to dig a little bit deeper in order to do it using arrays. My question would be: is using arrays the (only) way, or wouldn't it help at all?Here's the code:

echo "<div id='container'><div class='row'><p>Name</p>";		while($row = mysqli_fetch_array($db_data)) {	echo "<p><a href='#'>" . $row['nume'] . " " . $row['prenume'] . "</a></p>";}		echo "</div><div id='row'><p>Email</p>";		while($row = mysqli_fetch_array($db_data)) {	echo "<p><a href='#'>" . $row['email'] . "</a></p>";}		echo "</div></div>";

And here's what I'd like to have:http://bit.ly/7RhSX

Link to comment
Share on other sites

a MySql Query always returns an Array if data if found that meets the selection criteria.better get used to using them if you want to proceed with dynamic web sites and Databases.

Link to comment
Share on other sites

Your problem isn't arrays, it's while loops. You need to understand what this does:

while($row = mysqli_fetch_array($db_data)) {...}

Using the mysqli_fetch_array function gets the next record from the database result and returns it in an array. Every time you call the function with the same result it will return the next row. If it gets to the end of the result set it returns false instead of an array. So a while loop is used for looping through several rows and doing the same thing with each row. You have 2 while loops in your code. The first loop will loop through the result and print the names for each row, and the while loop will end when the result is at the end and mysqli_fetch_array returns false. Then you have another loop, but the result is already at the end so the while loop never runs and it never prints the email. You could reset the result to the beginning if you want to loop through it again, but if your database query is only returning one row (like if you're looking for a specific user), then there's no reason to use a while loop to keep looping through a result set with 1 result in it. Just get the one record you're looking for in an array, and then keep reading whatever data you want out of the array.

Link to comment
Share on other sites

If you're only expecting one record, you can just use something like:$row = mysqli_fetch_array($whatever);and then each field is available by its name:$first_name = $row["first_name"];You don't need to always loop through query results if you know you're only going to have one row.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...