Jump to content

Grabbing results from sql db


Unseeeen

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.
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
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?
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.