db error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

db error

Post by doggy »

I get this error on this line

///// mainfile.php ////

Code: Select all

<?php
class DB {
   function DB() {
       $this->host = "localhost";
       $this->db = "phpatlas";
       $this->user = "root";
       $this->pass = "root";
       $this->link = mysql_connect($this->host, $this->user, $this->pass);
       mysql_select_db($this->db);
   }
}
?>
/// test.php ///

Code: Select all

<?php

include("mainfile.php");

$DB = new DB;

$DB->$sql = mysql_query("SELECT id, testfield FROM test");

while (
	$DB->$row = mysql_fetch_array($sql)) {

   printf("ID: %s  Name: %s", $row["id"], $row["testfield"]);
}

?>
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\FoxServ\www\phpatlas\test.php on line RED
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

and this just gives me a blank page again :(

Code: Select all

<?php

include("mainfile.php");

$DB = new DB;

     $DB->sql = "SELECT id, testfield FROM test";
     $DB->result = mysql_query($DB->sql);
     $DB->row = mysql_fetch_row($DB->result);
     $id = $DB->row[id];
	 $testfield = $DB->row[testfield];

echo "$id<br>";
echo "$testfield<br>";

?>
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Post by LonelyProgrammer »

Alas, the code shall read as:

Code: Select all

while ( 
$DB->$row = mysql_fetch_array([b]$DB->sql[/b])) { 
printf("ID: %s Name: %s", $row["id"], $row["testfield"]); 
}


Actually, if you are already using a class, why not have:

Code: Select all

class DB
{

var $result;

// ....constructors and blah...

function query($query)
{
 $this->result = mysql_query($query)
}

function getArray()
{
  return mysql_fetch_array($result);
}

// foobar foobar foobar....
Just a suggestion
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Post by LonelyProgrammer »

Corrections made in bold.
<?php

include("mainfile.php");

$DB = new DB;

$DB->sql = "SELECT id, testfield FROM test";
$DB->result = mysql_query($DB->sql);
$DB->row = mysql_fetch_array($DB->result);
$id = $DB->row["id"];
$testfield = $DB->row["testfield"];

echo "$id<br>";
echo "$testfield<br>";

?>
mysql_fetch_array will return an array with key values same as the column headers.

mysql_fetch_row will return an indexed array. So, you shall code the above as:

Code: Select all

<?php

include("mainfile.php");

$DB = new DB;

     $DB->sql = "SELECT id, testfield FROM test";
     $DB->result = mysql_query($DB->sql);
     $DB->row = mysql_fetch_row($DB->result);
     $id = $DB->row[[b]0[/b]];
	 $testfield = $DB->row[[b]1[/b]];

echo "$id<br>";
echo "$testfield<br>";

?>
Hope this is of help!
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

cant some one maybe just help me out to get a samply select script working with that mainfile i have ... please that i just can see and then play around with the script. ok thank you so much guys.
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

thank you that last post before i made a post helped so much thanks man
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

but now how can i display all the rows in the db lets say my table looks like this
id testfield
1 2213
2 21321
3 1323
4 21312

how will i display all that info .
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Post by LonelyProgrammer »

You almost done it with your previous code. Just add some HTML formatting. If you don't know HTML, it's a good time to learn some.
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

???
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

Code: Select all

<?php 

include("mainfile.php"); 

$DB = new DB; 

$DB->sql = "SELECT * FROM test"; 
$DB->result = mysql_query($DB->sql); 
$DB->row = mysql_fetch_array($DB->result); 
$id = $DB->row[id]; 
$testfield = $DB->row[testfield]; 

echo "<table width="100%" bgcolor="orange"><tr><td>";
echo "<table width="100%" bgcolor="lightyellow"><tr><td bgcolor="orange"><b>id</b></td><td bgcolor="orange"><b>field</b></td></tr>";

echo "<tr><td>$id</td>"; 
echo "<td>$testfield</td></tr>";

echo "</table>";
echo "</td></tr></table>";

?>
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

thanks guys i have found the right script that works very good ... or not found but i wrote this ... hehe and i don`t even know php that well , you see what people can do with a little help.

Code: Select all

<?php 

include("mainfile.php"); 

$DB = new DB; 

$DB->sql = "SELECT * FROM users"; 
$DB->result = mysql_query($DB->sql); 

echo "<table width="100%" bgcolor="orange"><tr><td>";
echo "<table width="100%" bgcolor="lightyellow" border="1"><tr>";
echo "<td bgcolor="orange"><b>id</b></td><td bgcolor="orange"><b>account</b></td>";
echo "<td bgcolor="orange"><b>name</b></td><td bgcolor="orange"><b>surname</b></td>";
echo "<td bgcolor="orange"><b>age</b></td><td bgcolor="orange"><b>sex</b></td>";
echo "<td bgcolor="orange"><b>lang</b></td><td bgcolor="orange"><b>email</b></td>";
echo "<td bgcolor="orange"><b>homea</b></td><td bgcolor="orange"><b>homet</b></td>";
echo "<td bgcolor="orange"><b>worka</b></td><td bgcolor="orange"><b>workt</b></td>";
echo "<td bgcolor="orange"><b>postala</b></td><td bgcolor="orange"><b>medname</b></td>";
echo "<td bgcolor="orange"><b>mednb</b></td></tr>";


while($DB->row = mysql_fetch_array($DB->result)) { 
  $id = $DB->row[id]; 
  $account = $DB->row[account]; 
  $name = $DB->row[name]; 
  $surname = $DB->row[surname]; 
  $age = $DB->row[age]; 
  $sex = $DB->row[sex]; 
  $lang = $DB->row[lang]; 
  $email = $DB->row[email];  
  $homea = $DB->row[homea]; 
  $homet = $DB->row[homet]; 
  $worka = $DB->row[worka]; 
  $workt = $DB->row[workt]; 
  $postala = $DB->row[postala]; 
  $medname = $DB->row[medname]; 
  $mednb = $DB->row[mednb]; 

echo "<tr><td>$id</td>"; 
echo "<td>$account</td>";
echo "<td>$name</td>";
echo "<td>$surname</td>";
echo "<td>$age</td>";
echo "<td>$sex</td>";
echo "<td>$lang</td>";
echo "<td>$email</td>";
echo "<td>$homea</td>";
echo "<td>$homet</td>";
echo "<td>$worka</td>";
echo "<td>$workt</td>";
echo "<td>$postala</td>";
echo "<td>$medname</td>";
echo "<td>$mednb</td></tr>";
  } 

echo "</table>";
echo "</td></tr></table>";

?>
Post Reply