Jump to content


msql_fetch_array


6 replies to this topic

#1 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 01 October 2006 - 02:48 AM

Ok well what im actually trying to do is get my mysql_num_rows to work, but I singled out my fetch array and it doesnt work, thus my num_rows doesnt work. Here is the code...
$select1 = mysql_query("SELECT `username` FROM `users` WHERE `id` = '$id'") or die(mysql_error());
$row1 = mysql_fetch_array($select1);
	
$select2 = mysql_query("SELECT * FROM `news` WHERE `author` = '$row1'") or die(mysql_error());
$row2 = mysql_num_rows($select2);

When I run the array by itself, it returns "ARRAY" instead of the username. Thanks for the help guys! :D

#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 01 October 2006 - 04:50 AM

mysql_fetch_array returns an array....you either need to do:

echo '<pre>';
print_r($row1);
echo '</pre>';

to see the results, and to use them you use:

while ($row1 = mysql_fetch_array($select1)) {

	 echo $row1."\n";

}

failing that, make sure the queries, either that or your queries are not returning anything because I don't fully follow what you mean,

Matt

#3 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 01 October 2006 - 05:16 AM

This is why i suggested checking $row1 via pm :D

As "Mr.Matt" (rofl) said, mysql_fetch_array returns an array, so you must use it like this:
$row1 = mysql_fetch_array($select1);
	
$select2 = mysql_query("SELECT * FROM `news` WHERE `author` = $row1['key']") or die(mysql_error());
$row2 = mysql_num_rows($select2);

That should work fine. Note that $row1['key'] should be changed to whatever array key you wish to use from the data you are pulling from 'username'.

edit:i looked at your post again (heh actually read it this time)

Use this:

$select2 = mysql_query("SELECT * FROM `news` WHERE `author` = $row1['username']") or die(mysql_error());
$row2 = mysql_num_rows($select2);


#4 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 01 October 2006 - 05:31 PM

Mr. Matt, what I mean is it doesnt send me the array I want, as in the username in this case. It just sends me the word "Array". Ive backtracked into my phpMyAdmin and the query I ask it to do, should work. I just don't understand it.

Matt(hew.) when I try what you said, It gives this error.

Quote

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/faktionb/public_html/Admin/memberprofile.php on line 24
line 24 is the line w/ $select2 on it. My server does not like me determining which field it wants inside a query, I think I am going to have to determine it before I put it in the query. Which puzzles me because from the query I have right now it should work. But it's not giving me the array I ask for.

#5 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 01 October 2006 - 05:52 PM

Smells like you are using:
$select2 = mysql_query("SELECT * FROM `news` WHERE `author` = '$row1['username']'") or die(mysql_error());
$row2 = mysql_num_rows($select2);

...Which is not what i posted, i guess you just edited yours. If you did use mine (exactly mine) then just do this:

$var = $row1['username'];

$select2 = mysql_query("SELECT * FROM `news` WHERE `author` = '$var'") or die(mysql_error());
$row2 = mysql_num_rows($select2);

These are simple errors you should be able to fix yourself by now tbh.

View PostKorndawg, on Oct 1 2006, 11:31 PM, said:

Mr. Matt, what I mean is it doesnt send me the array I want, as in the username in this case. It just sends me the word "Array". Ive backtracked into my phpMyAdmin and the query I ask it to do, should work. I just don't understand it.

That's because you are trying to echo an array. You need to echo Array[Key]. Like: $row1['username']

#6 Scott

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 01 October 2006 - 07:16 PM

if you want to echo the returned values of your array, you need to use print_r($array); This function will actually read all of the values of your array and print them.

also, if you're just executing a query to get the # of results it is much more efficient to structure your query more like this
SELECT as COUNT(*) FROM `news` WHERE `author` = {$row['username']}
By using curly brakets around a value, it tells PHP to look for the value of the enclosed without trying to interpret it, allowing you to name array values properly inside mysql queries.

#7 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 01 October 2006 - 09:45 PM

Thanks Scott and Matt both ways worked. I appreciate it. :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users