Reply
Problems getting results from MySQL database
Old 06-14-2004, 11:16 AM Problems getting results from MySQL database
Novice Talker

Posts: 10
Hi, I'm having some problems with accessing the results from a MySQL database. This script is the edit page of a news script. If gets the existing news from the database and puts it into a form. However whenever I add the line:

$row = mysql_fetch_row($result)

I get the following error message.

Parse error: parse error, unexpected T_VARIABLE in /home/sites/site69/web/church/editnews.php on line 53

I'm quite new to PHP and just can't figure this one out. The code I'm using is shown below. I have blanked the MySQL username and password fields so they aren't the problem. I hope you can help.


<?

// Define database connection details

$dbHost = "localhost";
$dbUser = "*****";
$dbPass = "*****";
$dbName = "news";
$table = "news";

// Attempt to connect the the MySQL server

$link = @mysql_connect($dbHost, $dbUser, $dbPass);

// If the connection was unsuccessful

if (!$link)

{ // Report error and exit

echo "<P>Couldn't connect to server</P>";
exit;

}

// Attempt to select database. If unsuccessful....

if (!@mysql_select_db($dbName))

{ // Report error and exit

echo "<P>Could not select $dbName database</P>";
exit;

}

// Build query to fetch news item from database

$query = "SELECT * FROM news WHERE newsID=$newsID";

// Execute query

$result = @mysql_query($query);

// If query was okay AND we have returned a result

if ($result && @mysql_num_rows($result) > 0)

{ // Start building the HTML form

$row = mysql_fetch_row($result)

$newsText = "";
$newsText .= "<FORM METHOD='POST' ACTION='addnews.php'>";

// Add a hidden field to hold the newsID

$newsText .= "<INPUT TYPE='HIDDEN' NAME='newsID' VALUE=" . $row['newsID'] . ">";

// Add username and password fields

$newsText .= "<BR>Username:";
$newsText .= "<BR><INPUT TYPE='TEXT' NAME='username'>";
$newsText .= "<BR>Password:";
$newsText .= "<BR><INPUT TYPE='PASSWORD' NAME='password'>";

// Retrieve the title from the database

$newsText .= "<BR>Title:";
$newsText .= "<BR><INPUT TYPE='TEXT' NAME='title' VALUE=" . stripslashes($row['title']) . ">";

// Retrieve the author from the database

$newsText .= "<BR>Author:";
$newsText .= "<BR><INPUT TYPE='TEXT' NAME='author' VALUE=" . $row['author'] . ">";

// Retrieve the body from the database

$newsText .= "<BR>Body:";
$newsText .= "<BR><TEXTAREA NAME='body' ROWS='5'>";
$newsText .= stripslashes($row['body']);
$newsText .= "</TEXTAREA>";

// Add submit button

$newsText .= "<BR><BR><INPUT TYPE='SUBMIT' VALUE='Submit'></FORM>";

// Output the form

echo "$newsText";

}

else

{ // If no news items were found

echo "<P>That news item does not appear to be in the database</P>";

}

// Close link to MYSQL server

mysql_close($link);

?>
Dajjal is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 06-14-2004, 01:23 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Dajjal,

First, could you identify which line is line 53?

I looked through your code and the first thing that jumped out at me is that your SQL query is dependent on the variable $newsID, but it is not previously defined anywhere. Without knowing which line is 53, I would say that is the likely problem if this is the entire code.

Hope this helps....

Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 06-14-2004, 03:09 PM
Novice Talker

Posts: 10
Hi, the variable newsID is passed in from another PHP document. Thanks for your help but I think I've fixed it.
I always try to use:

$row = mysql_fetch_row($result)

to filter threw any results, however for some reason this didn't work. So after ages of trawling threw my PHP book I found an alternative

$row = mysql_fetch_array($result)

I don't know why the first one doesn't work. I'd appreciate it if anyone could point out the differences between these two functions. Thanks
Dajjal is offline
Reply With Quote
View Public Profile
 
Old 06-14-2004, 09:25 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Quote:
Originally Posted by Dajjal
I'd appreciate it if anyone could point out the differences between these two functions. Thanks
mysql_fetch_row() fetches a row of data from a result handle and returns it as a numerically keyed array.

mysql_fetch_array() fetches a row of data as an array from a result handle. The default behavior for this function is to return an array that is both associative and numerically indexed.

I also don't see any reason why the mysql_fetch_row() does not work -- but again, if we knew which line was line 53, that might help.

Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 06-15-2004, 09:10 AM
Novice Talker

Posts: 10
Row 53 was the row with the line:

$row = mysql_fetch_row($result)
Dajjal is offline
Reply With Quote
View Public Profile
 
Old 06-16-2004, 11:25 AM Think I figured it
Novice Talker

Posts: 10
Think I know why it didn't work, I was using $row['thing'] which of course would define row as an array, therefore I would have to pass in an array for it to work. Tell me if this makes any sense, and thanks for all your help.
Dajjal is offline
Reply With Quote
View Public Profile
 
Old 06-16-2004, 04:02 PM
Junior Talker

Posts: 3
Can you add the semicolon?

$row = mysql_fetch_row($result);
binary is offline
Reply With Quote
View Public Profile
 
Old 06-16-2004, 05:23 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Quote:
Originally Posted by Dajjal
Think I know why it didn't work, I was using $row['thing'] which of course would define row as an array, therefore I would have to pass in an array for it to work. Tell me if this makes any sense, and thanks for all your help.

That's definitely possible -- probable in fact. I supposed that if you wanted to use numerical indices to access the result of the array returned by the call mysql_fetch_row($result), that would have worked fine....

Awesome.

I think the associate array (using column names to access array elements coming out of mysql_fetch_array($result)) is more intuitive anyway, but it's good to figure these things out anyway.

Thanks for the follow-up.

Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Reply     « Reply to Problems getting results from MySQL database
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16685 seconds with 13 queries