Jump to content

Recommended Posts

Hey,

I have a previous snippet of code which I have been using for ages now,

[code]<?PHP
include("login.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM people";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

echo "<b><center>2005-2006</center></b><br><br>";

?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Nickname</font></th>
<th><font face="Arial, Helvetica, sans-serif">Class</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-Mail</font></th>
</tr>

<?PHP
$i=0;
while ($i < $num) {
$surname=mysql_result($result,$i,"surname");
$name=mysql_result($result,$i,"name");
$nickname=mysql_result($result,$i,"nickname");
$class=mysql_result($result,$i,"class");
$email=mysql_result($result,$i,"e_mail");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$name"; echo" $surname"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$nickname"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$class"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo "$email"; ?>"><? echo "$email"; ?></a></font></td>
</tr>
<?
++$i;
}
echo "</table>";?>[/code]

but however when I port the whole thing to another environment it dosent work... the MySQL query only returns the first line in this case rather than all the database entries as specified in the code...

[code]<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Symbol</font></th>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date</font></th>
<th><font face="Arial, Helvetica, sans-serif">Last</font></th>
<th><font face="Arial, Helvetica, sans-serif">Change</font></th>
<th><font face="Arial, Helvetica, sans-serif">High</font></th>
<th><font face="Arial, Helvetica, sans-serif">Low</font></th>
</tr>

<?php
include("login.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM stocks";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$quote = mysql_result($result,$i,"symbol");
$standard = ".hk";
$findquote = "$quote$standard";

Class yahoo
{
function get_stock_quote($symbol)
{
$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=snl1d1t1c1ohgv" ,$symbol);
$fp = fopen($url, "r");
if(!fp)
{
echo "error : cannot recieve stock quote information";
}
else
{
$array = fgetcsv($fp , 4096 , ', ');
fclose($fp);
$this->symbol = $array[0];
$this->name = $array[1];
$this->last = $array[2];
$this->date = $array[3];
$this->time = $array[4];
$this->change = $array[5];
$this->open = $array[6];
$this->high = $array[7];
$this->low = $array[8];
$this->volume = $array[9];
}
}
}
$quote = new yahoo;
$quote->get_stock_quote("$findquote");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->symbol"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->name"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->time"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->date"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->last"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->change"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->high"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->low"; ?></font></td>
</tr>
<?
++$i;
}
?>
</table>[/code]

Can someone please help?

Thanks a lot,

Jeff
this now should work,

fixed localhost to "localhost"
removed the mysql_numrows() function, misspelled must be mysql_num_rows()
changed the loop from while($i < $num) to while ($row = mysql_fetch_array($result))
removed mysql_result();
changed all variables inside the while loop
fixed some unneccessary typing echo $name; echo $surname;
mysql_close() now executes before php is closed instead of behind the mysql_num_rows() function

[code]
<?PHP
include("login.php");
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM people";
$result=mysql_query($query);

echo "<b><center>2005-2006</center></b><br><br>";

?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Nickname</font></th>
<th><font face="Arial, Helvetica, sans-serif">Class</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-Mail</font></th>
</tr>

<?PHP
$i=0;
while ($row = mysql_fetch_array($result)) {
   $surname=$row['surname'];
   $name=$row['name'];
   $nickname=$row['nickname'];
   $class=$row['class'];
   $email=$row['e_mail'];
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$name" . " $surname"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$nickname"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$class"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo "$email"; ?>"><? echo "$email"; ?></a></font></td>
</tr>
<?
++$i;
}
echo "</table>";

mysql_close();
?>
[/code]
[code]
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Symbol</font></th>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date</font></th>
<th><font face="Arial, Helvetica, sans-serif">Last</font></th>
<th><font face="Arial, Helvetica, sans-serif">Change</font></th>
<th><font face="Arial, Helvetica, sans-serif">High</font></th>
<th><font face="Arial, Helvetica, sans-serif">Low</font></th>
</tr>

<?php
include("login.php");
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM stocks";
$result=mysql_query($query);
$i=0;
while ($row = mysql_fetch_array($result)) {
$quote = $row['symbol'];
$standard = ".hk";
$findquote = "$quote$standard";

Class yahoo
{
function get_stock_quote($symbol)
{
$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=snl1d1t1c1ohgv" ,$symbol);
$fp = fopen($url, "r");
if(!$fp)
{
echo "error : cannot recieve stock quote information";
}
else
{
$array = fgetcsv($fp , 4096 , ', ');
fclose($fp);
$this->symbol = $array[0];
$this->name = $array[1];
$this->last = $array[2];
$this->date = $array[3];
$this->time = $array[4];
$this->change = $array[5];
$this->open = $array[6];
$this->high = $array[7];
$this->low = $array[8];
$this->volume = $array[9];
}
}
}
$quote = new yahoo;
$quote->get_stock_quote("$findquote");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->symbol"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->name"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->time"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->date"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->last"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->change"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->high"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->low"; ?></font></td>
</tr>
<?
++$i;
}
?>
</table>
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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