Jump to content

[SOLVED] mysql_fetch_array() problem


techkid

Recommended Posts

the file is blog.php

 

what the file do is blog.php list all the content generated

 

blog.php?act=view&id=6 <-- this one shows individual post

 

blog.php?act=view&id=6 is not working it says

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\supp\b-class.php on line 17

 

here is the code for it

//view the news article!
if(isset($_GET['act']) && $_GET['act'] == "view")
{
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM content WHERE index = '$id'");
while($row = mysql_fetch_array($sql))
{
echo "<div id='post'>";
echo "<h3>" . $row['title'] . "</h3>";
echo '<a href="blog.php?act=view&id='.$row['index'].'">'.$row['title'].'</a>';
echo $row['news'];
echo '<p class="postend">Posted by: '.$row['author'].' | Date:'.$row['date'].'</p></div>';
}

Link to comment
https://forums.phpfreaks.com/topic/173287-solved-mysql_fetch_array-problem/
Share on other sites

Change this

 

$sql = mysql_query("SELECT * FROM content WHERE index = '$id'");

 

to

 

$sql = mysql_query("SELECT * FROM content WHERE index = '$id'") or trigger_error(mysql_error());

 

and see if there is an error.  Are you sure that the field is index and not Index, or something like that?

 

Now I've got 2 errors

 

Notice: 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 'index = '6'' at line 1 in C:\wamp\www\techmv.net\b-class.php on line 16

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\techmv.net\b-class.php on line 17

 

Here is the database scheme:

2cdhfdt.jpg

Add die(mysql_error()) so we can see the output. (See below code for addition)

 

//view the news article!
if(isset($_GET['act']) && $_GET['act'] == "view")
{
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM content WHERE index = '$id'");
die(mysql_error());//ADDED HERE
while($row = mysql_fetch_array($sql))
{
echo "<div id='post'>";
echo "<h3>" . $row['title'] . "</h3>";
echo '<a href="blog.php?act=view&id='.$row['index'].'">'.$row['title'].'</a>';
echo $row['news'];
echo '<p class="postend">Posted by: '.$row['author'].' | Date:'.$row['date'].'</p></div>';
}

Archived

This topic is now archived and is closed to further replies.

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