NameSilo

MYSQL results not showing up properly

Spaceship Spaceship
Watch

NetworkTown.Net

Account Closed
Impact
2
Hi

I have this code:
PHP:
$result = mysql_query("SELECT DISTINCT * FROM category WHERE type = 1 ORDER BY Id DESC"); 

while($row = mysql_fetch_array($result))
  {
 $name = $row['Name'];
}
echo $name
echo '<br />';

and it will not show the results properly it wiill only show the first result and not all of them. Any one got any suggestions?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Try

PHP:
foreach(mysql_fetch_array($result) as $row){

Instead of

PHP:
while($row = mysql_fetch_array($result)){
 
0
•••
Well it shows me
PHP:
0
as the result
 
0
•••
Well that's plain simple: the statement
PHP:
echo $name
is outside the loop ;)
The variable is assigned several times (in the loop) but only the last value will be printed.
BTW put the echo '<br />'; inside the loop too.
 
0
•••
So i will change it to
PHP:
 $result = mysql_query("SELECT DISTINCT * FROM category WHERE type = 1 ORDER BY Id DESC"); 

while($row = mysql_fetch_array($result))
  {
 $name = $row['Name'];

echo $name
echo '<br />';  
}

Well just tried it and it dose not give any result at all if i move the }
 
0
•••
Try:

PHP:
 $result = mysql_query("SELECT DISTINCT * FROM category WHERE type = 1 ORDER BY Id DESC");  

foreach(mysql_fetch_array($result) as $row){
     $name = $row['Name']; 
     echo $name;
     echo '<br />';   
}
 
0
•••
Mikor said:
Try:

PHP:
 $result = mysql_query("SELECT DISTINCT * FROM category WHERE type = 1 ORDER BY Id DESC");  

foreach(mysql_fetch_array($result) as $row){
     $name = $row['Name']; 
     echo $name;
     echo '<br />';   
}
wired becuase the results came out like this: 22DD22111100
 
0
•••
Sample code:
<?php

mysql_connect("localhost", "user", "password") or
die("Could not connect: " . mysql_error());
mysql_select_db("your_db");

$result = mysql_query("SELECT DISTINCT * FROM category WHERE type = 1 ORDER BY Id DESC");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row["name"] ."<br />";
}

mysql_free_result($result);
?>
 
0
•••
sdsinc said:
Sample code:
<?php

mysql_connect("localhost", "user", "password") or
die("Could not connect: " . mysql_error());
mysql_select_db("your_db");

$result = mysql_query("SELECT DISTINCT * FROM category WHERE type = 1 ORDER BY Id DESC");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row["name"] ."<br />";
}

mysql_free_result($result);
?>
aaaaa this is getting annying still is not working it is showing blank results, iv never had this prob before now suddenly i get this prob
 
0
•••
Do you see errors reported by the server ?
Perhaps if you add this on top of your code:
error_reporting(E_ALL);

you will see possible PHP errors
 
0
•••
EDIT: fixed it, it was the capital N i forgot to use in the $row['
 
Last edited:
0
•••
Yeah, MySQL is case sensitive ;)
 
0
•••
Tree said:
Yeah, MySQL is case sensitive ;)
I didnt know that lol thanks.
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Appraise.net
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back