Whats wrong with this code?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Whats wrong with this code?

Post by nick2 »

I am pretty sure I left something out.. lol refreshing up again.

Code: Select all

<?php
<?
if (isset($_COOKIE["logged_in"])) {
} else {
header( "Location: Http://slices.net/tradingsystem/nologin.html");
exit;
}
?>
<?
$db_name = "slices_net";
$table_name = "trading";
#
$connection = mysql_connect("localhost", "slices", "password")
or die(mysql_error());
#
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
#
$result = my_query("Select * from trading") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "User: " & $row["ID"];
echo "Location: " & $row["location"];
echo "Signature: " & $row["signature"];
}
mysql_free_result($result);
?>
#
?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Code: Select all

<?php
if ($_COOKIE&#1111;"logged_in"])
&#123;
  $db_name = "slices_net"; 
  $table_name = "trading"; 

  $connection = mysql_connect("localhost", "slices", "password") 
  or die(mysql_error()); 

  $db = mysql_select_db($db_name, $connection) or die(mysql_error()); 
  $result = mysql_query("Select * from trading") or die(mysql_error()); 
  while ($row = mysql_fetch_array($result)) 
  &#123;
    echo "User: " . $row&#1111;"ID"];
    echo "Location: " . $row&#1111;"location"];
    echo "Signature: " . $row&#1111;"signature"]; 
  &#125;
  mysql_free_result($result);
&#125;
else
&#123;
  header( "Location: Http://slices.net/tradingsystem/nologin.html"); 
&#125; 
?>
I have a few questions myself.

1) Why did you use & instead of the concatenation operator( . )?

2) Why did you have end part of your php code and then start it once more on the very next line:

Code: Select all

<?php
 //some code
?>
<?php
 //some more code
?>
3) Would you like me to recommend some excellent php tutorials?
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

rofl.. sorry.. that could be why.. I used & cuz i'm use to Visual basics, I started anotehr one up because I heard header doesn't work with other comamnds in the same "thing"

my error:
Fatal error: Call to undefined function: my_query() in /home/virtual/site9/fst/var/www/html/tradingsystem/members.php on line 15

also I removed the double <? tags so theres only 1 now.

the error accurs on:

Code: Select all

<?php
$result = my_query("Select * from trading") or die(mysql_error());

?>
Last edited by nick2 on Sun Aug 17, 2003 5:30 pm, edited 1 time in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

No problem. I am happy to be of any assistance whatsoever.

But I am curious to know if the changes I made to the code made the script work?
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

noep they didn't.. and I edited my post look please. ;P see my error.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

no such thing as my_query()
maybe your meaning mysql_query() ?
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

thx coco... I am having a problem.. how do I make it loop all the ids, locations, and signatures?

it only displays 1 records.

heres the code:

Code: Select all

<?php
<?
if (isset($_COOKIE["logged_in"])) {
} else {
header( "Location: Http://slices.net/tradingsystem/nologin.html");
exit;
}
#
$dbhost = "localhost"; // host name, usually localhost
$dbuser = "slices"; // username used to login
$dbpwd = "editedout"; // password used to login
$dbname = "slices_net"; // the name of your database

mysql_connect($dbhost, $dbuser, $dbpwd);
mysql_select_db($dbname);
#
$result = mysql_query("select * from trading") or die(mysql_error());
$row = mysql_fetch_array($result);
$ID = $row["ID"];
$location = $row["location"];
$signature = $row["signature"];
mysql_free_result($result);
?>
<html>

<head>
<title>Members View</title>
</head>

<body>

<div align="center">
  <center>
  <table border="1" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="51%" id="AutoNumber1">
    <tr>
      <td width="33%" bgcolor="#999999"><font size="2" face="Verdana">User(s):</font></td>
      <td width="33%" bgcolor="#999999"><font size="2" face="Verdana">Location:</font></td>
      <td width="34%" bgcolor="#999999"><font size="2" face="Verdana">Signature:</font></td>
    </tr>
  </table>
  </center>
</div>
<div align="center">
  <center>
  <table border="1" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="51%" id="AutoNumber2">
    <tr>
      <td width="33%"><?php echo "$ID"; ?></td>
      <td width="33%"><?php echo "$location"; ?></td>
      <td width="34%"><?php echo "$signature"; ?></td>
    </tr>
  </table>
  </center>
</div>

</body>

</html>

?>
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

please help me.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Code: Select all

while ($row = mysql_fetch_array($result))
&#123;
  // Do stuff to each row (i.e. print their values, etc.)
&#125;
And I belive I posted a revised version of your code in which my_query had been changed to mysql_guery...
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

This time it posted my second user record not both..

heres my code:

Code: Select all

<?php
#
$result = mysql_query("select * from trading") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$ID = $row["ID"];
$location = $row["location"];
$signature = $row["signature"];
}
mysql_free_result($result);
?>
?>
I then do <? echo "$ID"; ?>
etc in the html.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

You should get rid of the #

You should get rid of one of the '?>'

Also, you don't need to do:

Code: Select all

<?php echo "$ID"; ?>
You can just do:

Code: Select all

<?php echo $ID; ?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

You also might even be able to do the following depending on how your php.ini file is setup:

Code: Select all

<?=$ID ?>
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

None of that made a difference. lol >.<
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Code: Select all

<?php
// You have to connect to mySQL server and select a database!

$result = mysql_query("select * from trading") or die(mysql_error()); 
while ($row = mysql_fetch_array($result)) 
&#123; 
  $ID = $row&#1111;"ID"]; 
  $location = $row&#1111;"location"]; 
  $signature = $row&#1111;"signature"]; 
&#125; 
mysql_free_result($result); 
?>
You should get a book on PHP or at least take advantage of some tutorials or something. You need help finding any contact me.
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

I'm not a complete idiot.. I didn't post my full code.. I posted the part that I needed help on.. common sense really -_-...


erm.
Post Reply