Jump to content

Archived

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

Unseeeen

Grabbing results from sql db

Recommended Posts

Well, I haven't done anything with php or sql in a while and I just had a simple question...

I'm making a very simple news script and right now all I want to do is insert & display results to the database.

Here's my code so far.
[code]
<?
$frontpagelimit = "5";
$dbconnect = mysql_connect ("localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db");
$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";
mysql_query($query);
$row = mysql_fetch_object($query);
echo $row->newsbody;
?>[/code]

What I'm trying to do at this stage is get the newsbody that has already been inserted.

I get a
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource[/quote]
on line 7 error.

From what I remember...this should be right. What's my problem? And how, once I've grabbed everything, display more than one news insert at a time? I'm thinking with maybe an array, but how would I do that?


Thanks.

Share this post


Link to post
Share on other sites
as to your first question, you are trying to create an object with the query string, not the result. you need to assign the query result to a variable and then create the object from the result variable. example:
[code]
<?
$frontpagelimit = "5";
$dbconnect = mysql_connect ("localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db");
$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";
$result = mysql_query($query);
$row = mysql_fetch_object($result);
echo $row->newsbody;
?>
[/code]
as to your 2nd question, you would just wrap a while statement around it, like so:
[code]
while ($row = mysql_fetch_object($result)) {
   echo $row->newsbody;
}
[/code]

Share this post


Link to post
Share on other sites
Hi

something that I found out as well that may be of use in this problem is that whenever I have used a variable in a PHP/SQL statement that variable has to be wrapped in single quotes

[code]

$query = "SELECT * FROM news ORDER BY id DESC LIMIT '$frontpagelimit'";

[/code]

Share this post


Link to post
Share on other sites
I have another question...

My code, will only display one result from the database, but I'm trying to get it to display 5. Here's my code...once again, I dont see what's wrong with it.

[code]<?
include 'functions.php';
$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";
$result = mysql_query($query);
$row = mysql_fetch_object($result);
?>
<center><table class="news">
<tr><td><b>Title</b>:<i><? echo $row->title; ?></i></td></tr>
<tr><td><? echo $row->newsbody;?></td></tr>
<tr><td><b>Posted by:</b> <i><? echo $row->username; ?></i> <b>on</b> <i><? echo $row->date; ?></i> <b>at</b> <i><? echo $row->time; ?></i></td></tr>
</table>[/code]

Share this post


Link to post
Share on other sites
Try this:
[code]
while ($row = mysql_fetch_assoc($result))
{
// do your operations with $row
}
[/code]

Share this post


Link to post
Share on other sites
Thanks, but I don't think I quite understand. I tried:

[code]$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";
$result = mysql_query($query);
$row = mysql_fetch_object($result);
while ($row = mysql_fetch_assoc($result))
{
?>
<center><table class="news">
<tr><td><b>Title</b>:<i><? echo $row->title; ?></i></td></tr>
<tr><td><? echo $row->newsbody;?></td></tr>
<tr><td><b>Posted by:</b> <i><? echo $row->username; ?></i> <b>on</b> <i>Jan 1st, 2006</i> <b>at</b> <i><? echo $row->time; ?></i></td></tr>
</table>
<?
}
?>[/code]

But nothing shows up? What's wrong?

Share this post


Link to post
Share on other sites
Did you check the HTML code that you got (View Source in your browser)?
Does you PHP installation allow the short tags '<?' and '?>' ?
Is the page completly empty? or just the part that should be the query result?

Share this post


Link to post
Share on other sites
RobX can you show me how it looks i always wanted to use thsi featuer u got msn?

Share this post


Link to post
Share on other sites
Your calling mysql_fetch_assoc in your while, but your code expects an object. Try...
[code]
<?php
$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";
if ($result = mysql_query($query)) {
  while ($row = mysql_fetch_object($result))
  {
?>
  <center><table class="news">
  <tr><td><b>Title</b>:<i><? echo $row->title; ?></i></td></tr>
  <tr><td><? echo $row->newsbody;?></td></tr>
  <tr><td><b>Posted by:</b> <i><? echo $row->username; ?></i> <b>on</b> <i>Jan 1st, 2006</i> <b>at</b> <i><? echo $row->time; ?></i></td></tr>
  </table>
<?php
  }
}
?>
[/code]
I also added a little error checking around your result.

PS: Get out of the habit right NOW of using short <? php tags. Use only <?php or your heading for trouble.

Share this post


Link to post
Share on other sites
Bingo!

Thanks a lot.


Also, how common is it for php short tags to NOT be useable? Just seems like all the hosts I've been on have had short tags enabled.

Share this post


Link to post
Share on other sites
i answered your 2nd question in my very first post. dunno why you went and asked it again...

Share this post


Link to post
Share on other sites

×

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.