Jump to content


Photo

Grabbing results from sql db


  • Please log in to reply
14 replies to this topic

#1 Unseeeen

Unseeeen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 25 June 2006 - 05:13 AM

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.
<?
$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; 
?>

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.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 25 June 2006 - 06:35 AM

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:
<?
$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;
?>
as to your 2nd question, you would just wrap a while statement around it, like so:
while ($row = mysql_fetch_object($result)) {
   echo $row->newsbody;
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Unseeeen

Unseeeen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 25 June 2006 - 06:56 AM

Hey thanks man, I appreciate it.

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 25 June 2006 - 09:45 AM

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


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


you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 Unseeeen

Unseeeen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 25 June 2006 - 10:25 PM

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.

<?
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>


#6 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 25 June 2006 - 10:27 PM

Try this:
while ($row = mysql_fetch_assoc($result))
{
 // do your operations with $row
}

Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#7 Unseeeen

Unseeeen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 25 June 2006 - 10:48 PM

Thanks, but I don't think I quite understand. I tried:

$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>
<?
}
?>

But nothing shows up? What's wrong?

#8 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 25 June 2006 - 10:57 PM

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?
Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#9 Unseeeen

Unseeeen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 25 June 2006 - 10:59 PM

Yes and yes. The part where the query result should be showing up is empty.

#10 scheols

scheols
  • Members
  • PipPipPip
  • Advanced Member
  • 113 posts

Posted 25 June 2006 - 11:00 PM

RobX can you show me how it looks i always wanted to use thsi featuer u got msn?
Dont Piss me Off today

#11 Unseeeen

Unseeeen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 25 June 2006 - 11:05 PM

Yeah I can show you, but only after I finish it up.

#12 Unseeeen

Unseeeen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 26 June 2006 - 01:56 AM

Can anyone see what's possibly wrong? I've been stuck for a while now.

#13 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 26 June 2006 - 01:59 AM

Your calling mysql_fetch_assoc in your while, but your code expects an object. Try...
<?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
  }
}
?>
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.

#14 Unseeeen

Unseeeen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 26 June 2006 - 03:01 AM

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.

#15 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 June 2006 - 05:13 AM

i answered your 2nd question in my very first post. dunno why you went and asked it again...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users