Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/55575-solved-display-data-from-database/
Share on other sites

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

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?

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

tables.gif

 

this is a screen shot of the data in my cy_content table

data.gif

 

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

$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

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.

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

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

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.