Hi Im new to this forum and new to php and currently working on a php for dummies book for my project I need to by March. I show you the problems

<?php
/* Program name: displayForm
* Descrption: Script displays a form that asks for
* the customer phone number
*/

?>

<html>
<head>
<title></title>
<style type='text/css'>

<!--
    form {margin: 1.5em 0 0 0; padding: 0;}
    .field{font-weight: bold; float: left; width: 20%; margin-right: 1em; text-align:right;}
    #submit{margin-left: 35%; padding-top:1em;}
    -->
    </style>
    </head>
    
    <body>
    
    <h3>Please enter your phone number </h3>
<form action='processform.php' method='POST'

<?php    
    //loop that displays the form fields
    foreach($labels as $field => $label)
    {
        echo "<div class= 'field'>
                <label for='$field'>$label</label>
                 <input type='text' name='$field' id='$field'
                 size='65' maxlength='65' /></div>\n";
                
    }
    
    echo"<div id='submit'><input type='submit' value='Submit Phone Number' />\n";
    echo"</div>\n</form>\n</body>\n</html>";
    
    
?>

displays error

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\petstore\displayform.php on line 29

second error on another file im working on

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
require_once("config.php");


$type ="Horse";
$petInfo = getPetsOfType($type); //call function

/*Displays results in a table*/

echo "<h1>{$type}s</h1>\n";
echo "<table cellspacing='15'>\n";
echo "<tr><td colspan='4'><hr /></td></tr>\n";
for($i=1; $i<=sizeof($petInfo);$i++)
{

    $f_price = number_format($petInfo[$i] ['price'], 2);
    echo "<tr>\n
          <td>$i.</td>\n
          <td>{$petInfo[$i] ['petName']}</td>\n
          <td>{$petInfo[$i] ['petDescription']}</td>\n
          <td style='text-align: right'>\$$f_price</td>\n
          </tr>\n";
          
          echo"<tr><td colspan='4'> <hr /></td></tr>\n";
          
}

echo "</table>\n";

?>

<?php

function getPetsOfType($petType)
{
    $j = 1;
    while($row=mysqli_fetch_assoc($result))
    {
        foreach($row as $colname => $value)
        {
            $array_multi[$j] [$colname] = $value;
        }
        
    }
    $j++;
}

return $array_multi;



?>

<body>
</body>
</html>

Error displays : Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\petstore\getpettype.php on line 43

I hope i made sense hope to hear from you peeps soon thanks :).

In both cases it doesn't look like you've initialized the variables. Both of them need an array from that results from a query of the database.

Thanks for replying :).

I did realize my first error that I forgot to add a label array which looks like this

$labels = array("first_name" =>"First_Name",
				 "middle_name" =>"Middle_Name",
				 "last_name" =>"Last_Name",
				 "phone" =>"Phone");

However for my second error I have checked through the code from the book and I still can't figure out what is missing?, ive checked code for code but it seems fine can youy spot anything or can you show me what needs to be added ? I know its hard but anything you reccomend is appreciated :)

So what you're missing is an executed query that returns results. If you look at the code closely, you'll see that $results never gets any information, so when it hits the while loop it's empty. You'll need to add a query that populates the $results array prior to the while loop.

Here is an example of a select query, however, you'll need to refer to the book to make it work correctly.

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

mysql_close($con);

?>

1.
<?php
2.
$con = mysql_connect("localhost","peter","abc123");
3.
if (!$con)
4.
{
5.
die('Could not connect: ' . mysql_error());
6.
}
7.

8.
mysql_select_db("my_db", $con);
9.

10.
$result = mysql_query("SELECT * FROM Persons");
11.

12.
mysql_close($con);
13.

14.
?>

I see what you mean now thank you :), if you noticed i included my config file that has my database connection etc and I used a require_once("config.php"); so I won't need to retype the connection info, so do I just ass in the query on the page im working on or shall i put this in my config file, I show you my example;

<?php
/*
	Config file
	@author Imran Rashid
*/

// global variables
$base_url = "";
$base_path = "";

// database stuff
$host="localhost";
$user="username";
$password="password";
$dbname = "db_name";

//firstmethod

$cxn = mysqli_connect($host,$user,$password) or die("could not connect to database");

// now connection2 is set up, select the database
mysqli_select_db($cxn, $dbname);

//$cxn = mysqli_connect($host, $user,$password,db_name)
//or die ("could not connect server");
?>

As you can see I have not have a mysqli_close because I had to get rid of it due to an error but may insert it later if I need to. I have couple of methods of my db connection so ignore it :)

<?php
/*
	Config file
	@author Imran Rashid
*/

// global variables
$base_url = "";
$base_path = "";

// database stuff
$host="localhost";
$user="username";
$password="password";
$dbname = "db_name";

//firstmethod

$cxn = mysqli_connect($host,$user,$password) or die("could not connect to database");

// now connection2 is set up, select the database
mysqli_select_db($cxn, $dbname);

//$cxn = mysqli_connect($host, $user,$password,db_name)
//or die ("could not connect server");

$query = "SELECT * FROM Pet WHERE petType='petType'";
$result= mysqli_query($cxn, $query)
		 or die ("couldnt execute query.");

?>

As you can notice I added the code at the bottom because I did what you said but yours comes up with an error and the code I added in is from the book and the die statment "couldnt execute query."

your error is

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\petstore\config.php  on line 28

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\petstore\config.php on line 28

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\petstore\getpettype.php on line 43

Sorry for the double post just missed the edit limit

Don't put the query in the config file and also don't use the code I gave you, it was only an example. Change your code back to what you had initially, just put the query in the file that uses it.

The important part is having the $results variable filled so it doesn't give you the error you previously had.

$result = mysql_query("SELECT * FROM Persons");

I was aware that you told me not to use your example but I tried to see if it works, either way it still doesn't work man :(

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
require_once("config.php");


$type ="Horse";
$petInfo = getPetsOfType($type); //call function 

/*Displays results in a table*/

echo "<h1>{$type}s</h1>\n";
echo "<table cellspacing='15'>\n";
echo "<tr><td colspan='4'><hr /></td></tr>\n";
for($i=1; $i<=sizeof($petInfo);$i++)
{

	$f_price = number_format($petInfo[$i] ['price'], 2);
	echo "<tr>\n
		  <td>$i.</td>\n
		  <td>{$petInfo[$i] ['petName']}</td>\n
		  <td>{$petInfo[$i] ['petDescription']}</td>\n
		  <td style='text-align: right'>\$$f_price</td>\n
		  </tr>\n";
		  
		  echo"<tr><td colspan='4'> <hr /></td></tr>\n";
		  
}

echo "</table>\n";

?>

<?php

function getPetsOfType($petType)

{

$query = "SELECT * FROM Pet WHERE petType='petType'";
$result= mysqli_query($cxn, $query)
		 or die ("Couldn't execute query.");


{
	$j = 1;
	while($row=mysqli_fetch_assoc($result))
	{
		foreach($row as $colname => $value)
		{
			$array_multi[$j] [$colname] = $value;
		}
		
	}
	$j++;
  }
}


return $array_multi;




?>

<body>
</body>
</html>

Starting from line 44 I added the query and result )exactly the same as it shows on the book and the same error shoots up

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\petstore\getpettype.php on line 45
Couldn't execute query.

Really confused wonder if im doing something really stupid that causing an error.

why dont you just use mysql_query instead of mysqli_query.

also you could write your query like this too.

// run the query
$result = mysql_query("SELECT * FROM Pet WHERE petType='petType'");


//check the query. if not successful the print error
if ($result) {
	$message = 'Yay the query was successfully run555.';
}
else{
	$message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: <em>' . $query . '</em>';
    die($message);
}

thats the way i do it. hope i helped

Hey its_not_me thanks for your helpful post:) but unforunatly it did not work comes with an error

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\petstore\buildcheckbox.php  on line 10

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\petstore\buildcheckbox.php on line 10
Whole query:

I paste my code in

<?php
// Program name: buildradio.php
// Description: Program displays list of radio buttons from databse info

require_once("config.php");

   
      // run the query
   
      $result = mysql_query("SELECT * FROM Pet WHERE petType='petType'");
  
      
   
       
   
      //check the query. if not successful the print error
   
      if ($result) {
   
      $message = 'Yay the query was successfully run555.';
   
      }
   
      else{
  
      $message = 'Invalid query: ' . mysql_error() . "\n";
  
      $message .= 'Whole query: <em>' . $query . '</em>';
  
      die($message);
  
      }
	  ?>

<html>
<title></title>
<head>
<body>

      <div style='margin-left: .5in; margin-top:=.5in;'>
          <p style='font-weight:bold;'>
              which type of pet are you interested in></p>
                <p>Please choose one type of pet from the following list</p>
                
               <form action='processform.php' method-'POST'>
               
     <?php
	 	while ($row =mysql_fecth_assoc($result))
		
		{
			extract ($row);
			echo"<input type ='checkbox' name='interest[$petType]' value='$petType />'$petType< br />\n"; //displays checkbox
		}

	 
	 ?>
	<p><input type='submit' value='Select Type of Pet' /></p>
    </form>
    </div>
    </body>
    </head>
                
           
                 

<?php



?>

I put the query on my third php tag and ended up with a different error

which type of pet are you interested in>

Please choose one type of pet from the following list

Fatal error: Call to undefined function mysql_fecth_assoc() in C:\xampp\htdocs\petstore\buildcheckbox.php on line 24

If I remember correctly from the book, mysqli is a new function or method that is teaching me to run a program cause im using php 5 but im not too sure if you can use other methods as im very new to php, sorry for being a persistant but im very grateful with the help i got so far despite its giving us a hard time.


Or its just me doing some amature mistake :(

that error is that you are not connecting to the DB.

idk what you have in the config file but this is what i use

function connect_to_db()
{

// database connection settings:

	$hostname = 'localhost';
	$database = 'DBname';
	$username = 'username';
	$password = 'password';

// establish a connection to the database:
	$connection = mysql_connect($hostname, $username, $password);
	if (!$connection)
	{
	  die('Could Not Connect to Database: ' . mysql_error());
	}

// select the database for use:
	$selected_db = mysql_select_db($database,$connection);
	if (!$selected_db)
	{
	  die('Error Selecting Database: ' . mysql_error());
	}
}

then in the file that you are trying to connect to the DB you do this

//the file with the DB connections
require_once("functions.php");

//call function to connect to DB
connect_to_db();

Hey I have done your method buuuut it still runs an error :(

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php

// file with database connection
require_once("config2.php");

//call function to conect databse
connect_to_db();

$type ="Horse";
$petInfo = getPetsOfType($type); //call function 

/*Displays results in a table*/

echo "<h1>{$type}s</h1>\n";
echo "<table cellspacing='15'>\n";
echo "<tr><td colspan='4'><hr /></td></tr>\n";
for($i=1; $i<=sizeof($petInfo);$i++)
{

	$f_price = number_format($petInfo[$i] ['price'], 2);
	echo "<tr>\n
		  <td>$i.</td>\n
		  <td>{$petInfo[$i] ['petName']}</td>\n
		  <td>{$petInfo[$i] ['petDescription']}</td>\n
		  <td style='text-align: right'>\$$f_price</td>\n
		  </tr>\n";
		  
		  echo"<tr><td colspan='4'> <hr /></td></tr>\n";
		  
}

echo "</table>\n";

?>

<?php

function getPetsOfType($petType)

{

   
      // run the query
   
      $result = mysql_query("SELECT * FROM Pet WHERE petType=petType");
   
          
   
      //check the query. if not successful the print error
   
      if ($result) 
	  {
      $message = 'Yay the query was successfully run555.'; 
      }
   
      else
	  {
      $message = 'Invalid query: ' . mysql_error() . "\n";
      $message .= 'Whole query: <em>' . $query . '</em>';
      die($message);
      }

{
	$j = 1;
	while($row=mysqli_fetch_assoc($result))
	{
		foreach($row as $colname => $value)
		{
			$array_multi[$j] [$colname] = $value;
		}
		
	}
	$j++;
  }
}


return $array_multi;




?>

<body>
</body>
</html>
Invalid query: Table 'pets.pet' doesn't exist Whole query:

Im really confused my previous database connection did work I show u my config file again

1.
      <?php
   2.
      /*
   3.
      Config file
   4.
      @author Imran Rashid
   5.
      */
   6.
       
   7.
      // global variables
   8.
      $base_url = "";
   9.
      $base_path = "";
  10.
       
  11.
      // database stuff
  12.
      $host="localhost";
  13.
      $user="username";
  14.
      $password="password";
  15.
      $dbname = "db_name";
  16.
       
  17.
      //firstmethod
  18.
       
  19.
      $cxn = mysqli_connect($host,$user,$password) or die("could not connect to database");
  20.
       
  21.
      // now connection2 is set up, select the database
  22.
      mysqli_select_db($cxn, $dbname);
  23.
       
  24.
      //$cxn = mysqli_connect($host, $user,$password,db_name)
  25.
      //or die ("could not connect server");
  26.
       
  27.
      $query = "SELECT * FROM Pet WHERE petType='petType'";
  28.
      $result= mysqli_query($cxn, $query)
  29.
      or die ("couldnt execute query.");
  30.
       
  31.
      ?>

and my first file that works when connecting databse

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pet catalog</title>
</head>

<body>

<?php
//include config
/* Program: petDescriptionfor.php
*Desc: Displays a numbered list of all pets in selected category
*/

	require_once("config.php");
		
		$pettype = "Dragon";
		$query= "SELECT * FROM petCatalog WHERE petType='$pettype'";
		$result = mysqli_query($cxn,$query)
		or die("couldn execute query."); 
		
		$nrows = mysqli_num_rows($result);
		
		$pettype = ucfirst($pettype). "s";
		echo"<h1>$pettype</h1>\n";
		echo"<table cellspacing='15'>\n";
		echo"<tr><td colspan='4'><hr/></td></tr>\n";
		
		
		for($i=0;$i<$nrows;$i++)
		{
			$n = $i + 1; #add 1 so numbers don't start with 0
			$row = mysqli_fetch_assoc($result);
			extract($row);
			$f_price = number_format($price,2);
			echo"<tr>\n
				 <td>$n.</td>\n
				<td>$petName</td>\n
				<td>$petDescription</td>\n
				<td style='text-align:right'>\$$f_price</td>\n
				</tr>\n";
				
				echo "<tr><td colspan='4'><hr></td></tr>\n";
				
		}
		
		echo"</table>\n";




echo $base_url;

?>
</body>
</html>

result:

Dragons
1. 	ASian Dragon 	Serpentine Dragon 	$0.00
2. 	Medieval Dragon 	Lizard -likebody 	$30,000.00
http://localhost/petstore/

As you can see it does not look this on my webpage but it does connect to the databse but the rest of the files should work right?. It must be something else but I worry about that later Im going to carry on with the book and keep it consistant for now. But feel free to still help me fix the solution :)

first off dont run a query in the config file. that is only for functions or just the connection.

make sure that you are calling the correct table. it doesnt give a line so it is hard to debugg it.

I call my connect to DB in a function cause it is easier to add other functions to that file. instead of creating more and having to call them all the time.

for example you could put the getPetsOfType() in the config so you dont have to copy and paste it everytime. you call the config file once cause you have to anyways then you call your function when you need it. organization.

this probably didnt help but there is not much i can do with that error.

No no no you been a great help, so far the best user in this forum that helped me (not to isolate anyone here your all good :D). I do like your method and I do think I need to get it checked out with freinds so they could see the real problem but I have your method in one of the files so thank you and everyone who helped, I learned more about php syntax thanks to you guys :D:D:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.