Hi,

I am working on a script that populates a table with a user's news entries. First I get the username from the URL, then go to the database and get the user_id. From the user_id, I query the news table for the user's news entries.

$idgrabber = "select id from $db_table where urlname = '".$_GET['username']."';"; 

$result = mysql_query($idgrabber) or die(mysql_error());

if ($result) {

echo "got a result";

$newsresults = "select * from $newstable where user_id = '$result'"; 

$result2 = $mysqli->query($newsresults) or die(mysqli_error($mysqli));

echo "<form method='post' action='delete.php'>"; 
   echo "<table cellspacing='0' cellpadding='15'>
 
   <th width='15%'>NEWS</th>
   <th width='55%'>SOMETHING ELSE</th>
   <th width='15%'>SOMETHING ELSE</th>
   <th width='15%'>SOMETHING ELSE</th>
   ";
 
   while ($row = $result2->fetch_object()) {

}

Echo "got a result" works but then I don't get any news articles in the table unless I insert an actual user_id value in the line "$newsresults = ...".

I think this might be down to a basic misunderstanding of objects and variables. Can anyone put me straight please?

Recommended Answers

All 5 Replies

does $result actually contain a result?

wont it return as true if there is any value in it? (not to sure if that is the case however)

If so you will need to pull the userid from the mysql result like

$USER_DETAILS = mysql_fetch_row($result);

and use that, as the $result var will be a mysql result object

Hi OnIIcE,

Thank you so much for your help. I think I am almost there now, and the script now incorporates your line in two places.

$idgrabber = "select id from $db_table where urlname = '".$_GET['username']."';"; 

$result = mysql_query($idgrabber) or die(mysql_error());

if ($result) {

$USER_DETAILS = mysql_fetch_row($result);

echo $USER_DETAILS[1];

$newsresults = "select * from $newstable where user_id = '$USER_DETAILS[1]'"; 

$result2 = $mysqli->query($newsresults) or die(mysqli_error($mysqli));

$NEWS_ARTICLES = mysql_fetch_row($result2);

echo $NEWS_ARTICLES[2];
echo $NEWS_ARTICLES[3];
echo $NEWS_ARTICLES[4];

   echo "<form method='post' action='delete.php'>"; 
   echo "<table cellspacing='0' cellpadding='15'>
 
   <th width='15%'>NEWS</th>
   <th width='55%'>SOMETHING ELSE</th>
   <th width='15%'>SOMETHING ELSE</th>
   <th width='15%'>SOMETHING ELSE</th>
   ";
 
 
   while ($row = $result2->fetch_object()) {
 
}

I think I see what you mean about $result. Echo $USER_DETAILS[1] gives the answer I was looking for, so that is all good now. However for the $NEWS_ARTICLES... line I get Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in .../profile.php

Any ideas why this doesn't work like the first one?

Use mysql_fetch_array() function rather using mysql_fetch_row().. This will returns the result as an array element and later you can display the results easily without errors..

Hi Dragonbaki,

Many thanks for your suggestion. I have done that and now it works! Cheers!

Hmmm.... have fun... Cheers....!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.