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
https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/
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]
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]
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]
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?
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.

Archived

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

×
×
  • 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.