PHP Help

He has: 1,380 posts

Joined: Feb 2002

Hey guys...I'm getting better! (lol)

Ok, here's the deal...i'm connecting to a dB and printing information into a page (.php). It doesn't print the data from the dB. Here's the code:

<?php
echo \"xml version=\\"1.0\\" encoding=\\"UTF-8\\"\n\";

...connection...

$qry = mysql_query(\"SELECT * FROM news WHERE data='$data' AND date='$date'\");
$text = $qry[text];
$date = $qry[date];

echo \"xml version=\\"
1.0\\" encoding=\\"UTF-8\\"\n\";
?>

and then in the page i have:
<p><?=$date?>:<br />&nbsp;&nbsp;&nbsp;<?=$text?></p>
<?php
mysql_close
($dbh);
?>
'

what's wrong? i have data in the dB, correct fields and everything! thanks for the help!

Busy's picture

He has: 6,151 posts

Joined: May 2001

your missing the fetch data bit
something like:
$row = mysql_fetch_array($qry);

and then $row[text] etc

why do you have two xml tags? and you should also have an if statement incase of error
$qry = mysql_query("SELECT * FROM news WHERE data='$data' AND date='$date'");
if(!$qry) {
echo ("error performing task: ". mysql_error() . "");
} else {
$row = mysql_fetch_array($qry);
...

the mysql_error is handy for trouble shooting

He has: 296 posts

Joined: May 2002

Try

<?php
echo \"xml version=\\"1.0\\" encoding=\\"UTF-8\\"\n\";

... connection ...

$qry = mysql_query(\"SELECT * FROM `news` WHERE data='$data' AND date='$date'\");
$rslt = mysql_fetch_array($qry);
$text = $rslt['text'];
$date = $rslt['date'];
?>

Also, how come you have the same echo statement twice for the XML?

[James Logsdon]

He has: 1,380 posts

Joined: Feb 2002

it's telling me that mysql_fetch_array isn't a valid "result resource", and i checked php.net, it looks good to me....?!

i was told that you should send the xml header twice, or it will confuse the browser, and possibly screw up your presentation, but speaking of which, i just re-ran it, it had an error, got rid of the last declaration, and it works fine now. lol.

He has: 296 posts

Joined: May 2002

Try this code:

<?php
$con
= mysql_connect('locahost','user','pass') or die(mysql_error());
mysql_select_db('db',$con) or die(mysql_error());

if ((!isset(
$data))||(!isset($date))) {
    echo
'Error Returned: Proper variables not set.';
} else {
   
$query = mysql_query(\"SELECT * FROM `news` WHERE data='$data' AND date='$date'\") or die(mysql_error());
   
$result = mysql_fetch_array($query) or die(mysql_error());
   
$text = $result['text'];
   
$date = $result['date'];
}
?>

[James Logsdon]

He has: 1,380 posts

Joined: Feb 2002

As I'm sure you suspected, it returned "Error Returned: Proper variables not set".

What does this mean? i have set the proper variables in both the dB and the php!

He has: 296 posts

Joined: May 2002

How are you obtaining the variabls $data and $date? From the URL? If yes, try $_GET['data'] and $_GET['date']

He has: 1,380 posts

Joined: Feb 2002

the variables will be submitted via form, although I have been inserting them directly into the table

He has: 296 posts

Joined: May 2002

If they are submitted via form and its a POST form then you need to use $_POST['var'].

He has: 1,380 posts

Joined: Feb 2002

even in a dB?

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Yes even in a dB, where ever you need to use the variables. Where else does "it" know where to get it from?

He has: 1,380 posts

Joined: Feb 2002

now i'm getting nothing displayed at all! not even the html!

i changed the text='$text' to text='$_POST[text]' and same for date

Busy's picture

He has: 6,151 posts

Joined: May 2001

$text = $_POST['text'];

dollar sign infront of the variable name, and single quotes inside the boxed brackets, not outside.

Just remember the method in the form MUST be post to use $_POST, if it's get, use $_GET

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.