Jump to content
Larry Ullman's Book Forums

Recommended Posts

MySQL Version: 5.0.45

PHP Verson: 5.2.17

 

I am using a code modified from chapter 17.

but I cannot get mysql to display query results using php.

I made a page to make a summary (title and date only) page that turns the title into a link to the detailed page

it is working correctly

 

when i click on entry #4 (all work the same way) for example my url seems to be correct

 

...blogpage.php?pid=4

 

but I get this result at the top of the resulting page

 

SELECT entry_id , title , date_updated , p1 , p2, FROM blog WHERE entry_id = 1

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in **redacted**walterlovett/htdocs/cityofwinfield/blogpage.php on line 15 (shows as row 10 in the inserted code (while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {)

 

this is my mysql table structure

 

entry_id int(5) No (auto increments)

date_updated timestamp No CURRENT_TIMESTAMP (auto updates)

title varchar(40) No Entry Title (default text)

p1 text No

p2 text No

 

PRIMARY entry_id

 

 

 

here is my code

 

<?php
// This page displays the details of the post.
$row = FALSE; // Assume nothing!
if (isset($_GET['pid']) && is_numeric($_GET['pid']) ) { // Make sure there's an entry ID!
 $pid = (int) $_GET['pid'];
// Get the post info:
require_once ('../../mysqli_connect_city_of_winfield.php');
echo $q = "SELECT entry_id , title , date_updated , p1 , p2, FROM blog WHERE entry_id = $pid";
$r = mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
// Fetch the information:
$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
// Start the HTML page:
$title='Unit Details';
include('includetop.html');
echo "<p>{$row['title']}</p>";
echo "<p>{$row['date_updated']}</p>";
echo "<p>{$row['p1']}</p>";
echo "<p>{$row['p2']}</p>";
} // End of the mysqli_num_rows() IF.
mysqli_close($dbc);
}
if (!$row) { // Show an error message.
include('includetop.html');
echo '<h1>Error 404; File Not Found<br></br>
The page you are looking for has most likely been upgraded, <br></br>
please use the  navigation at the left to find what you need,<br></br>
Thanks.</h1>';
}
// Complete the page:
include('includefooter.html');
?>

 

 

 

when the page is called i get the "else" option (my little 404 message in the code above) and this is printed at the top of the page (i just put the echo of the query in there to help me debug)

 

SELECT entry_id , title , date_updated , p1 , p2, FROM blog WHERE entry_id = 1

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in **redacted**walterlovett/htdocs/cityofwinfield/blogpage.php on line 15(shows as row 10 in the inserted code (while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {)

 

when i run that query in mysql phpadmin i get this response from mysql

 

Error

SQL query:

SELECT entry_id, title, date_updated, p1, p2,

FROM blog

WHERE entry_id =4

LIMIT 0 , 30

MySQL said:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM blog WHERE entry_id = 4 (the 4 just happens to be the entry i clicked on)

LIMIT 0, 30' at line 1

 

I really can not see what is wrong with my syntax.

Any help would be greatly apprecitated

 

walt

Link to comment
Share on other sites

I think the following is your issue:

 

while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {

// Fetch the information:

$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

 

The expression in the while statement is already storing the MySQL result into the $row variable. Doing it again will cause an error (assuming there is only one record returned from the DB). Erase the $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); line after // Fetch the information:, and see what happens.

Link to comment
Share on other sites

Thank you, I see what you mean, however, when I erased the "$row = mysqli_fetch_array ($r, MYSQLI_ASSOC); line after // Fetch the information:" I received the exact same resulting error message.

any other thoughts?

Link to comment
Share on other sites

Hmmm...something must be wrong with your query. I can imagine one of two things:

 

1) The syntax for the query string stored in $q is wrong, a field name or table name is off, or something of the like.

 

2) $pid is not properly acquired, and thus the query cannot be properly executed. Maybe try echoing $pid to the screen before the query to guarantee it's what you want.

 

Please lemme know how that goes.

Link to comment
Share on other sites

I have not tried this but it's just something that I noticed that could possibly be wrong...

echo $q = "SELECT entry_id , title , date_updated , p1 , p2, FROM blog WHERE entry_id = $pid";

You say you entered echo in front of it, so I am ignoring that, but is it possibly the fact that you have a comma after p2?

Link to comment
Share on other sites

The echo wasn't causing the problem, however. The assignment of the string to $q was still taking place, and the echo was printing the results of that statement's execution, which would have been the string itself (as shown by the original poster). So while there's no reason to do echo $q = ..., that wouldn't affect the assignment of the query string to the $q variable.

Link to comment
Share on other sites

 Share

×
×
  • Create New...