Page 1 of 1

[Solved] mysql_fetch_row???

Posted: Fri Mar 11, 2005 1:03 pm
by joncampbell
I was wondering if someone could help me with a way of working around mysql_fetch_row. I wrote a program that is suppose to display data from a mysql database, the database has 3 columns, (company,testimonial, and date). I am trying to display this database in a html format. I can get the program to display 1 entry from the database, but not display the whole database. I was wondering if I should just write a function for it or try this method.

Code: Select all

/* I like to decalare my variables early */
$company = mysql_query("select company from testimonials");
$testimonial = mysql_query("select testimonial from testimonials");
$date = mysql_query("select date from testimonials");
/*
$companyrow = mysql_fetch_row($company);
$testimonialrow = mysql_fetch_row($testimonial);
$daterow = mysql_fetch_row($date);
*/
//End declares

?>
<table border="0" width=100%>
<tr>
<?
/* Just in case I need it later.
while ($field=mysql_fetch_field($company)) {
        echo "<th align=left width=30%>";
        echo "$field->name";
        echo "</th>";
}
echo "</tr>";
*/

while ($row = mysql_fetch_row($company)) {
        for ($i=0; $i<mysql_num_fields($company); $i++) {
                echo "<tr>";
                echo "<td width=30%>";
                echo "<b>$row[$i]</b>";
/*              echo "<br>";
                echo "$testimonialrow[$i]";
                echo "<br>";
                echo "<b>$daterow[$i]</b>";
*/
                echo "</td>";
        }
}
echo "</tr>";
echo "</table>";
?>
Thanks for the input in advance.


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Mar 11, 2005 1:07 pm
by infolock
yeah. what you need to use in this case is mysql_fetch_assoc()

http://www.php.net/mysql_fetch_assoc

Posted: Fri Mar 11, 2005 1:13 pm
by feyd
have you tried switching the mysql_num_fields call to a count($row) :?: You may want to move the <tr> to before the while loop starts, if you want the </tr> to stay where it is.

Posted: Fri Mar 11, 2005 1:41 pm
by joncampbell
Here is the new attempt, this is giving this error "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/niteandd/public_html/company/testimonials_test.php on line 36
".

Code: Select all

$result = mysql_query("SELECT id as company, testimonial, date  FROM testimonials");

?>
<table border="0" width=100%>
<tr>
<?
/* Just in case I need it later.
while ($field=mysql_fetch_field($company)) {
        echo "<th align=left width=30%>";
        echo "$field->name";
        echo "</th>";
}
echo "</tr>";
*/
while ($row = mysql_fetch_assoc($result)) {
                echo "<tr>";
                echo "<td width=30%>";
                echo "<b>";
                echo $row["company"];
                echo "</b>";
                echo "<br>";
                echo $row["testimonial"];
                echo "<br>";
                echo "<b>";
                echo $row["date"];
                echo "</b>";
                echo "</td>";
}

Posted: Fri Mar 11, 2005 1:46 pm
by feyd
you have an error in your query syntax. 'date' is a keyword. You need backticks.

Posted: Fri Mar 11, 2005 1:55 pm
by joncampbell
I tried the backticks, and I just decided to change the column name, this is giving the same error.

Code: Select all

<?
$result = mysql_query("SELECT id as company, testimonial, testimonial_date FROM testimonials");

?>
<table border="0" width=100%>
<tr>
<?
while ($row = mysql_fetch_assoc($result)) {
                echo "<tr>";
                echo "<td width=30%>";
                echo "<b>";
                echo $row["company"];
                echo "</b>";
                echo "<br>";
                echo $row["testimonial"];
                echo "<br>";
                echo "<b>";
                echo $row["testimonial_date"];
                echo "</b>";
                echo "</td>";
}

Posted: Fri Mar 11, 2005 1:57 pm
by feyd
okay.. ask it to display mysql_error()

Posted: Fri Mar 11, 2005 2:02 pm
by infolock
try this....

Code: Select all

<?php
$company = array();
$testimonial = array();
$date = array();

$sql = mysql_query("Select company, testimonial, date from testimonials") or die(MySQL_Error());

while($row=mysql_fetch_assoc($sql)
{
      $company[] = $row['company'];
      $testimonial[] = $row['testimonial'];
      $date[] = $row['date'];
}

echo '<table border="0" width=100%><tr><td>Company Name</td><td>Testimonial</td><td>Date</td></tr><tr>';

for($i=0; $i<count($company); $i++)
{
     echo '<td>'.$company[$i].'</td><td>'.$testimonial[$i].'</td><td>'.$date[$i].'</td>';
}

echo '</tr></table>';
?>

Then, if it says that Date is not a recognized field, go into your console and manually connect to mysql... Then, run a DESCRIBE TESTIMONIAL on your table and see what the actual field name it is you gave for the date field... you may just not be entering it correctly.

Last, if you are, just post the error message and we'll see what we can see ;)

Posted: Fri Mar 11, 2005 2:06 pm
by joncampbell
Thank you, that did the trick :&) I didn't know could get a REAL error message from it.

I had to create a key field, and set it primary and define a valid column.

Posted: Fri Mar 11, 2005 2:08 pm
by joncampbell
Wow, thats nice looking. Thanks for the code snippet, And thanks again for all the help. I am coming back from a long vacation from programming. :&)

Posted: Fri Mar 11, 2005 2:12 pm
by infolock
welcome back to the world of php 8) good luck with your project