wrathican Posted June 14, 2007 Share Posted June 14, 2007 hi thanks for looking at my post what i need help with is displaying data from a database field. the data to be displayed is determined by the value of a variable. i am using a switch statement to define the variable $content. the query is something like this: SELECT * FROM table WHERE field=$content is this right? the table has multiple fields, ID, Title, Page and content. and i will only want to echo the Title and Content fields. what would the best way to do this be? this thing is i will be closing the php tag after the query has been exceuted and then reopening it when i need to use the Title and content. is this possible or does php think it doesnt need it after i close the php tag? Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/ Share on other sites More sharing options...
Wuhtzu Posted June 14, 2007 Share Posted June 14, 2007 What you need are the very basics of interacting with MySQL from PHP. I will suggest you read some tutorials on the subject: Get data from database: (what you are asking about in your thread) http://www.php-mysql-tutorial.com/php-mysql-select.php General PHP and MySQL: http://www.php-mysql-tutorial.com/mysql-tutorial/index.php Best regards Wuhtzu Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274566 Share on other sites More sharing options...
aniesh82 Posted June 14, 2007 Share Posted June 14, 2007 hello Close or open a php tag is not a problem. Regards Aniesh Joseph anieshjoseph@gmail.com Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274567 Share on other sites More sharing options...
wrathican Posted June 14, 2007 Author Share Posted June 14, 2007 hmm i have tried the site you suggested but i keep getting this error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fhlinux179/l/ls12style.co.uk/user/htdocs/cycleyorks/cycleyorks/index.php on line 12 i am using this code: <?php include 'switch.inc'; include 'misc.inc'; include 'opendb.inc'; //sets the query $query = "SELECT * FROM cy_content WHERE cont_page=".$content; //sets the result as the query $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $title = $row['cont_title']; $maincontent = $row['cont_content']; } ?> and then echoing the maincontent and title variables later down my page but nothing shows in return any idea whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274599 Share on other sites More sharing options...
wrathican Posted June 14, 2007 Author Share Posted June 14, 2007 argh! ive tried so much to correct the problem but its not working my query doesnt return any result as far as i can see the query is this : $query = "SELECT * FROM cy_content WHERE cont_page=".$content; the $content variable is declared in a switch statement before the query and the default is set to 'index'. i then have the result variable containing the mysql_query and $query: $result = mysql_query($query); and then i have the while statement: while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $title = $row['cont_title']; $maincontent = $row['cont_content']; } but even when i just echo $result i do not see anything. this is a screen shot of my tables list this is a screen shot of the data in my cy_content table this is my database connection info file: <?php $host = "**************"; $user = "*********"; $pass = "******"; $dbname = "ls12style"; ?> this is my open DB file: <?php $con = mysql_connect($host, $user, $pass) or die ('Error connecting to mysql'); mysql_select_db($dbname); ?> both of the above files are .inc files and are included at the start of the php code. this is the switch statement i am using: the 'a' is from the url which is set when i click a link, the switch statement is fine there are no erros with it. its for referencing purposes. switch($_GET['a']) { case "1": $homepic = "homeoff.jpg"; $aboutpic = "abouton.jpg"; $coursepic = "coursesoff.jpg"; $tourpic = "toursoff.jpg"; $hirepic = "hireoff.jpg"; $accompic = "accomoff.jpg"; $datespic = "datesoff.jpg"; $bookingpic = "bookingoff.jpg"; $bringpic = "bringoff.jpg"; $contactpic = "contactoff.jpg"; $commentpic = "commentsoff.jpg"; $gallerypic = "galleryoff.jpg"; $challengespic = "challengesoff.jpg"; $linkspic = "linksoff.jpg"; //this is the variable that decides on what content to show $content = "about"; break; the reason i have posted all this is so that you can look at everything and see if all the variables are the same, and there are no errors Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274661 Share on other sites More sharing options...
Illusion Posted June 14, 2007 Share Posted June 14, 2007 $content="about'; there are no matching records in table cy_content with cont_title as 'about'. Obviously no results would be returned. Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274670 Share on other sites More sharing options...
wrathican Posted June 14, 2007 Author Share Posted June 14, 2007 $content="about'; there are no matching records in table cy_content with cont_title as 'about'. Obviously no results would be returned. my query isnt asking about cont_title equalling about its asking cont_page to equal it well i have gotten rid of the error i had because i realised i had missed out some quotes. now i have something different... when i echo my query i get this: SELECT * FROM cy_content WHERE cont_page='index'Resource id #6 the 'Resource is #6' is a new thing... i havent ever seen it happen before. any idea what that means and i still return no results Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274685 Share on other sites More sharing options...
Illusion Posted June 14, 2007 Share Posted June 14, 2007 Sorry for my mistake and Resource is #6 is the value stored in $result. You might be echoed $result also. add this code after $result = mysql_query($query) if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } if(mysql_num_rows($result)==0) { echo 'No results are found'; } then post what you are getting. Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274698 Share on other sites More sharing options...
wrathican Posted June 14, 2007 Author Share Posted June 14, 2007 i still get this..... SELECT * FROM cy_content WHERE cont_page='index' Resource id #6 and i dont get anything else... Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274710 Share on other sites More sharing options...
Illusion Posted June 14, 2007 Share Posted June 14, 2007 Post php code you are using . Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274721 Share on other sites More sharing options...
wrathican Posted June 14, 2007 Author Share Posted June 14, 2007 <?php echo $query; echo "<br>"; if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } if(mysql_num_rows($result)==0) { echo 'No results are found'; }?> and sorry i dont get the resource is #6 anymore but i do not get anything from this, but by the looks of it im not meant to.... Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274725 Share on other sites More sharing options...
Illusion Posted June 14, 2007 Share Posted June 14, 2007 I don't know what you have understand form my posts what I mean is, use the following code <?php if ((include 'switch.inc') == 'OK') { echo 'OK';} if ((include 'misc.inc') == 'OK') { echo 'OK';} if ((include 'opendb.inc') == 'OK') { echo 'OK';} //sets the query $query = "SELECT * FROM cy_content WHERE cont_page=".$content; //sets the result as the query $result = mysql_query($query); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } if(mysql_num_rows($result)==0) { echo 'No results are found'; } while($row = mysql_fetch_assoc($result)) { $title = $row['cont_title']; $maincontent = $row['cont_content']; } ?> If you are getting three OKs then your files are included and if you got the error "Could not run query: . mysql_error()" there went something wrong with the query tell me what you are getting........ Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274744 Share on other sites More sharing options...
wrathican Posted June 14, 2007 Author Share Posted June 14, 2007 ah i am sorry for my mistake altough i really am not seeing anything i do not even see the OK's i really dont know what is happening here ive been working on this all day Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274759 Share on other sites More sharing options...
wrathican Posted June 14, 2007 Author Share Posted June 14, 2007 ahh thanks i dont know how you fixed it but its working now thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/#findComment-274776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.