[Solved] mysql_fetch_row???

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
joncampbell
Forum Newbie
Posts: 24
Joined: Fri Mar 11, 2005 12:57 pm
Location: Irvine, California, USA

[Solved] mysql_fetch_row???

Post 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]
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

yeah. what you need to use in this case is mysql_fetch_assoc()

http://www.php.net/mysql_fetch_assoc
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
joncampbell
Forum Newbie
Posts: 24
Joined: Fri Mar 11, 2005 12:57 pm
Location: Irvine, California, USA

Post 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>";
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have an error in your query syntax. 'date' is a keyword. You need backticks.
joncampbell
Forum Newbie
Posts: 24
Joined: Fri Mar 11, 2005 12:57 pm
Location: Irvine, California, USA

Post 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>";
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay.. ask it to display mysql_error()
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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 ;)
Last edited by infolock on Fri Mar 11, 2005 2:08 pm, edited 1 time in total.
joncampbell
Forum Newbie
Posts: 24
Joined: Fri Mar 11, 2005 12:57 pm
Location: Irvine, California, USA

Post 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.
joncampbell
Forum Newbie
Posts: 24
Joined: Fri Mar 11, 2005 12:57 pm
Location: Irvine, California, USA

Post 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. :&)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

welcome back to the world of php 8) good luck with your project
Post Reply