Jump to content

HELP Using MYSQL in php


kasitzboym

Recommended Posts

Hey everyone this is my first post so hopefully i am doing this right...

 

I have a basic php file to use to test my idea which seams to work just fine but when i try to pull the information from a mysql database and implement the same idea with database variables the information doesn't seem to work. I have tried multiple ways to get the code to read correctly but i cant seam to find the error.. The code below is the starter code i have to work with then below i will post the code which i am trying to work with... if someone could help explain this to me i would greatly appreciate it.

 

----------------------WORKING BASIC CODE

 

 

<?php

//set up a "database" of names, descriptions, and image locations for two staff members: jane and bob

 

$names = array('jane'=>'Jane Dow', 'bob'=>'Bob Smith');

$descriptions = array('jane'=>'Jane is an ECE major', 'bob'=>'Bob is a physics major');

$images = array('marcus'=>'/images/marcus.jpg', 'bob'=>'/images/bob.jpg');

 

//determine which staff member the user wants to see info for

$input = $_GET['name']; //for staff.php?name=[name]

//could also use $keys = array_keys($_GET); $input = $keys[0]; if you want something like staff.php?[name]

 

//this section takes the input from the URL (what we want to see information for) and selects the appropriate content from the arrays

$name = $names[$input];

$description = $descriptions[$input];

$image = $images[$input];

 

?>

 

 

<html>

<body>

<a href="testscript.php?name=jane">Jane</a><br>

<a href="testscript.php?name=bob">Bob</a>

 

<img src="<?php echo $image; ?>">

<br><b>Name:</b> <?php echo $name; ?></b>

<br><b>Description:</b> <?php echo $description; ?>

</body>

</html>

 

 

-----------------------This is the code that i was trying to actually use

 

<?php

 

require("connect.php");

 

$id = ($_GET['id']);

 

$query="SELECT * FROM staff";

$result=mysql_query($query);

 

mysql_close();

 

 

$result = mysql_fetch_array($result);

$id=$result['id'];

$firstname=$result['firstname'];

$lastname=$result['lastname'];

$bio=$result['bio'];

 

 

 

 

if (isset($_GET['id']))

{

echo "$firstname <br> $bio ";

}

else

{

echo "The Staff Member Does Not Exist";

}

 

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/195392-help-using-mysql-in-php/
Share on other sites

Hi kasitzboym, please surround your code with


tags for readability purposes.

 

the information doesn't seem to work

 

As in the data is not displaying?  Please elaborate.

 

Before going into depth with your code you can filter out obvious and fatal errors by temporarily setting your error_reporting to max by adding this code to the TOP of your .php files:

 

ini_set ("display_errors", "1"); 
error_reporting(E_ALL);

I apologize for not putting the tags around the code i will do that this time... As for the information not working...

 

I can put staff.php in the url and it places the The Staff Member Does Not Exist text on the page which is correct but i have 9 staff members in the mysql database table called staff... when i place staff.php?id=1 it desplays the first member in the table but when i place the id=2, id=3 and so on it still only displays the first staff members information on the page.

 

The page connect.php actually has the errors enabled and does not show any errors with the code.

 

Once again i will place the code i am trying to use that i am having problems with

 


<?php

require("connect.php");

$id = ($_GET['id']);

$query="SELECT * FROM staff";
$result=mysql_query($query);

mysql_close();


$result = mysql_fetch_array($result);
$id=$result['id'];
$firstname=$result['firstname'];
$lastname=$result['lastname'];
$bio=$result['bio'];




if (isset($_GET['id']))
{
echo "$firstname <br> $bio ";
} 
else
{
echo "The Staff Member Does Not Exist";
}

?>

Archived

This topic is now archived and is closed to further replies.

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