Page 1 of 2

random quote php mysql

Posted: Tue Apr 07, 2009 3:02 am
by gwolff2005
Hi,
I have a problem. I created a table in mysql database and have the following script as .php file but i get always the same error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/guntmar/public_html/indexmain.php on line 147

Can anyone please help me??? :banghead:
Thank you!!

Below is the code of the file...



<?php require_once('Connections/Login.php'); ?>
<?php
mysql_select_db($database_Login, $Login);
$query_Recordset1 = "SELECT * FROM quotes";
$Recordset1 = mysql_query($query_Recordset1, $Login) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

<?php

define ('HOSTNAME', 'localhost_or_hostname');
define ('USERNAME', 'username');
define ('PASSWORD', 'database_password');
define ('DATABASE_NAME', 'database_name');


mysql_select_db(DATABASE_NAME);

$query = "SELECT entry_title,entry_text FROM mt_entry WHERE (entry_blog_id=1) and (entry_status=2) ORDER by rand() LIMIT 1";

($result = mysql_query($query));

while ($row = mysql_fetch_array($result)) {
echo "<p>" , ($row['entry_title']) , "</p> \n <p>" , nl2br($row['entry_text']) , "</p>";
}

mysql_close();
?>

Re: random quote php mysql

Posted: Tue Apr 07, 2009 3:26 am
by papa

Code: Select all

 
mysql_select_db(DATABASE_NAME);
 
$query = "SELECT entry_title, entry_text FROM mt_entry WHERE entry_blog_id=1 AND entry_status=2 ORDER by rand() LIMIT 1";
 
$result = mysql_query($query);
 
while ($row = mysql_fetch_array($result)) {
echo "<p>" . $row['entry_title'] . "</p> \n <p>" . nl2br($row['entry_text']) . "</p>";
}
 
mysql_close();
 

Re: random quote php mysql

Posted: Tue Apr 07, 2009 3:33 am
by gwolff2005
Hi,
thanks for your reply.
I tried your code. I get the same error.
The page where I run the file is http://www.guntmarwolff.com/indexmain.php


The full code for the page is below....
Thanks so much!



<?php require_once('Connections/Login.php'); ?>
<?php
mysql_select_db($database_Login, $Login);
$query_Recordset1 = "SELECT * FROM quotes";
$Recordset1 = mysql_query($query_Recordset1, $Login) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Guntmar Wolff Life Mastery, Inc.</title>
<style type="text/css">
<!--
.style19 {
font-family: Verdana;
font-size: 11px;
}
#Layer1 {
position:absolute;
left:15px;
top:23px;
width:245px;
height:22px;
z-index:1;
}
#Layer2 {
position:absolute;
left:17px;
top:219px;
width:231px;
height:68px;
z-index:2;
}
#Layer3 {
position:absolute;
left:15px;
top:226px;
width:204px;
height:46px;
z-index:3;
}
#Layer4 {
position:absolute;
left:259px;
top:139px;
width:235px;
height:26px;
z-index:4;
}
#Layer5 {
position:absolute;
left:258px;
top:165px;
width:245px;
height:74px;
z-index:5;
}
.style20 {font-family: Verdana; font-size: 11px; font-weight: bold; }
#Layer6 {
position:absolute;
left:259px;
top:241px;
width:252px;
height:44px;
z-index:6;
}
#Layer7 {
position:absolute;
left:259px;
top:273px;
width:245px;
height:71px;
z-index:7;
}
.style21 {font-size: 9px}
#Layer12 { position:absolute;
left:564px;
top:307px;
width:198px;
height:42px;
z-index:8;
}
#Layer8 {
position:absolute;
left:15px;
top:262px;
width:234px;
height:88px;
z-index:9;
}
.style22 {font-size: 2px}
.style24 {font-size: 3px; color: #FFFFFF; }
-->
</style>
<script type="text/JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a)&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>

<body onload="MM_preloadImages('pics/poll.jpg','pics/newsletterbanner.jpg')">
<div id="Layer1"><span class="style19"><img src="pics/newsbutton.jpg" width="235" height="17" /></span></div>
<div id="Layer12">
<div align="right" class="style19"><span class="style21">&copy; 2008 Wolff Life Mastery, Inc. <br />
All Rights Reserved</span> <br />
</div>
</div>
<div id="Layer3"><img src="pics/tvappearance.jpg" width="235" height="19" /></div>
<div id="Layer4"><img src="pics/think about.jpg" width="245" height="17" /></div>
<div id="Layer5">
<p align="justify" class="style20"><?php

define ('HOSTNAME', 'localhost_or_hostname');
define ('USERNAME', 'username');
define ('PASSWORD', 'database_password');
define ('DATABASE_NAME', 'database_name');


mysql_select_db(DATABASE_NAME);
$query = "SELECT entry_title, entry_text FROM mt_entry WHERE entry_blog_id=1 AND entry_status=2 ORDER by rand() LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<p>" . $row['entry_title'] . "</p> \n <p>" . nl2br($row['entry_text']) . "</p>";
}
mysql_close();
?>


</p>
</div>
<div id="Layer6"><img src="pics/talkscoachingandmore.jpg" width="245" height="17" /></div>
<div class="style19" id="Layer7">
<div align="justify">So you would like to book Guntmar for a talk in your company? Would you like to attend a seminar? Or have you decided to invest in personal coaching?<br />
Whatever you need you will find it <a href="events.html" target="mainFrame"><em>here</em></a>...</div>
</div>
<div class="style19" id="Layer8">
<div align="justify">Guntmar Wolff is an acclaimed expert and therefore often asked to be on television. Watch <a href="http://www.guntmarwolff.com/media.html" target="mainFrame"><em>here</em></a> the latest interviews... </div>
</div>
<table width="760" border="0" cellspacing="5" cellpadding="3">
<tr>
<td><p class="style19">&nbsp;</p>
<p align="justify" class="style19"> The new book by Guntmar Wolff will be published in June 2009. If you want to know what it is about, before everyone else...<br />
Click <a href="http://www.guntmarwolff.com/newsletter.html" title="Newsletterbook" target="mainFrame"><em>here</em></a>, for more information. </p></td>
<td><a href="poll.html" target="mainFrame" onmouseover="MM_swapImage('Image10','','pics/poll.jpg',1)" onmouseout="MM_swapImgRestore()"><img src="pics/poll.jpg" name="Image10" width="245" height="97" border="0" id="Image10" /></a></td>
<td><a href="newsletter.html" target="mainFrame" onmouseover="MM_swapImage('Image11','','pics/newsletterbanner.jpg',1)" onmouseout="MM_swapImgRestore()"><img src="pics/newsletterbanner.jpg" name="Image11" width="245" height="95" border="0" id="Image11" /></a></td>
</tr>
<tr>
<td valign="top"><div align="justify">
<p class="style19">We are building up a community, whose only purpose is to help and support you with your ideas and how to become successful. Join <a href="http://www.guntmarwolff.com/newsletter.html" target="mainFrame"><em>here</em></a>. </p>
</div>
<p>&nbsp;</p></td>
<td>&nbsp;</td>
<td><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shoc ... on=7,0,0,0" width="245" height="147">
<param name="flash_component" value="ImageViewer.swc" />
<param name="movie" value="filmfirst.swf" />
<param name="quality" value="high" />
<param name="FlashVars" value="flashlet={imageLinkTarget:'_blank',captionFont:'Verdana',titleFont:'Verdana',showControls:true,frameShow:false,slideDelay:5,captionSize:10,captionColor:#333333,titleSize:10,transitionsType:'Random',titleColor:#333333,slideAutoPlay:false,imageURLs:['img1.jpg','img2.jpg','img3.jpg'],slideLoop:false,frameThickness:2,imageLinks:['http://macromedia.com/','http://macrome ... media.com/'],frameColor:#333333,bgColor:#FFFFFF,imageCaptions:[]}" />
<embed src="filmfirst.swf" quality="high" flashvars="flashlet={imageLinkTarget:'_blank',captionFont:'Verdana',titleFont:'Verdana',showControls:true,frameShow:false,slideDelay:5,captionSize:10,captionColor:#333333,titleSize:10,transitionsType:'Random',titleColor:#333333,slideAutoPlay:false,imageURLs:['img1.jpg','img2.jpg','img3.jpg'],slideLoop:false,frameThickness:2,imageLinks:['http://macromedia.com/','http://macrome ... media.com/'],frameColor:#333333,bgColor:#FFFFFF,imageCaptions:[]}" pluginspage="http://www.macromedia.com/shockwave/dow ... kwaveFlash" type="application/x-shockwave-flash" width="245" height="147"> </embed>
</object></td>
</tr>
</table>
</body>
</html>

Re: random quote php mysql

Posted: Tue Apr 07, 2009 3:52 am
by papa
Sorry should be mysql_fetch_assoc

What does your MySQL table look like?

Re: random quote php mysql

Posted: Tue Apr 07, 2009 3:56 am
by gwolff2005
Field Type Collation Attributes Null Default Extra Action
quotes varchar(25) latin1_swedish_ci Yes NULL

quotes
Field Type Null Default Comments
quotes varchar(25) Yes NULL


Space usage: Type Usage
Data 48 B
Index 1,024 B
Total 1,072 B
Row Statistics: Statements Value
Format dynamic
Rows 2
Row length ø 24
Row size ø 536 B
Creation Apr 06, 2009 at 12:50 PM
Last update Apr 06, 2009 at 12:50 PM

Re: random quote php mysql

Posted: Tue Apr 07, 2009 4:10 am
by papa
The fields names for mt_entry are?

Re: random quote php mysql

Posted: Tue Apr 07, 2009 4:16 am
by gwolff2005
:banghead: :banghead: :banghead:

I have no clue... Where can I see that?

Re: random quote php mysql

Posted: Tue Apr 07, 2009 4:25 am
by papa
Are you using phpmyadmin for example or console ?

Re: random quote php mysql

Posted: Tue Apr 07, 2009 4:27 am
by gwolff2005
phpmyadmin

Thanks so much for helping me!

Re: random quote php mysql

Posted: Tue Apr 07, 2009 4:43 am
by papa
First select the db you are working in, then on the left menu you have all the tables listed for that db. Select mt_entry and you will see all the columns listed for that table.

Re: random quote php mysql

Posted: Tue Apr 07, 2009 5:05 am
by gwolff2005
On the left it says
database: guntmar
then underneath: table: quotes
then on the right it says this...


Server: localhost Database: guntmar Table: quotes
BrowseStructureSQLSearchInsertExportImportOperationsEmptyDrop
Field Type Collation Attributes Null Default Extra Action
quotes varchar(25) latin1_swedish_ci Yes NULL
Check All / Uncheck All With selected:
--------------------------------------------------------------------------------
Print view Propose table structure
Add field(s)At End of Table At Beginning of Table After quotes
--------------------------------------------------------------------------------

Indexes: Keyname Type Cardinality Action Field
quotes FULLTEXT None quotes
Create an index on columns
Space usage Type Usage
Data 48 B
Index 2,048 B
Total 2,096 B
Row Statistics Statements Value
Format dynamic
Collation latin1_swedish_ci
Rows 2
Row length ø 24
Row size ø 1,048 B
Creation Apr 07, 2009 at 02:19 AM
Last update Apr 07, 2009 at 02:19 AM

Open new phpMyAdmin window

Re: random quote php mysql

Posted: Tue Apr 07, 2009 5:08 am
by gwolff2005
here is the monitor printout

Re: random quote php mysql

Posted: Tue Apr 07, 2009 6:22 am
by papa
You can see there that the tables in database guntmar is:
quotes
test
users

field/columns for quotes is 1 called quotes.

Code: Select all

if($result = mysql_query($query)) {
 while ($row = mysql_fetch_array($result)) {
 echo "<p>" . $row['entry_title'] . "</p> \n <p>" . nl2br($row['entry_text']) . "</p>";
 }
} else echo mysql_error();
  
mysql_close();
Change your code accordingly.

Re: random quote php mysql

Posted: Tue Apr 07, 2009 6:31 am
by gwolff2005
ok. I changed it with what you said, but now I get the mistake: Table 'guntmar.mt_entry' doesn't exist. Just to be sure that I made myself clear. I took the code you wrote on your last post and replaced my code with it...

Re: random quote php mysql

Posted: Tue Apr 07, 2009 6:56 am
by papa
As you can see in phpmyadmin no table with that name exists. The code I gave you displays the error mysql generates if any.

I think you need to start with reading a little bit about MySQL in general to get an understanding how a database work.