Web Hosting Talk







View Full Version : MySQL/PHP Questions


MeAmRussian
04-20-2006, 03:32 PM
I am currently trying to build a project and learn PHP/MySQL at the same time. I have a question, and I WILL have questions on how to do something.

Here is my current question:
When I SELECT from a database and show the table, how do I sort the rows by ABC order according to one of the columns?

That's the question for now. If I have more, I'll just post them here. Go to the bottom to see if I have any questions (I guess this is my "question" thread?).

Thanks :)

X-TechMedia
04-20-2006, 03:35 PM
SELECT * FROM table_name ORDER BY column_name ASC

MeAmRussian
04-20-2006, 03:57 PM
Thanks X-TechMedia.

MeAmRussian
04-20-2006, 04:35 PM
Alright, for some reason I'm coming out with an error.

Here is my code:
$query = "SELECT suite, title, category, our_url FROM business ORDER BY category ASC";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<tr><td>";
echo htmlspecialchars(stripslashes($row["suite"]));
echo "</td><td>";
echo htmlspecialchars(stripslashes($row["title"]));
echo "</td><td>";
echo htmlspecialchars(stripslashes($row["category"]));
echo "</td><td>";
echo htmlspecialchars(stripslashes($row["our_url"]));
echo "</td></tr>";
}
It gives me this error:
=======
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in a cerain folder on line 51
=======
Why is the supplied argument not valid?

Thanks in advance.

thartdyke
04-20-2006, 05:01 PM
Have you connected to the database anywhere? If not, you'll need to do something like this out of the manual:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

MeAmRussian
04-20-2006, 05:45 PM
Yes, I have connected.

$db = mysql_connect("localhost", "user", "pass");

if (!$db)
{
echo "Error: Could not connect to databsae. Please try again later.";
exit;
}
mysql_select_db("business");

MeAmRussian
04-20-2006, 06:48 PM
Actually, for the code posted in reply #4, if I put that $num_results = 2, I get the same error for mysql_fetch_array(). It says: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

Can someone explain why this isn't working?

Thanks.

deuce868
04-21-2006, 08:00 AM
I'll bet you're getting an error in your query. For the love of whoever you love, please check for errors on an result. You're code assumes it will always work which is just not true. Read up on checking for mysql errors and add in some code and I'll bet you anything you'll find an obvious problem.

juangake
04-21-2006, 08:48 AM
As deuce868 said, you must check for errors on the query before using the results. Something like:


$result = mysql_query($query);
if (! $result) {
die("MYSQL error: ".mysql_error());

}


This way you'll can debug the error that mysql would say on a console.

Regards,

Juan

fozzy
04-21-2006, 08:56 AM
Re post 4:

Another way to do that loop is:

while ($row = mysql_fetch_array($result)) {
// do stuff here
}


That way you don't have to worry about somehow looping too many or too few times.

MeAmRussian
04-21-2006, 04:01 PM
Thanks, I've figured it out :) Turns out there was an error connecting to the db.

Thanks.

Saeven
04-23-2006, 08:00 PM
Do it the way that fozzy suggests. mysql_num_rows should be avoided.