0

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?

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Dragonbaki
1

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

0

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?

Edited by Cranial Fool: n/a

1

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

0

Hi Dragonbaki,

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.