0

I have come across a problem when i try to display data from my database now I have never had this problem before and it will not show me any errors! can anyone see the problem with this? Any and all help is appreciated.

<!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" />
<link rel="stylesheet" type="text/css" href="Styles.css"/>
<?php
    error_reporting(E_ALL);
    $pagename=$_GET['Recipe'];
    $recipeID=$_GET['id'];
?>
<title><?php echo"$pagename"?></title>
</head>
<body>
<?php
    $mySqlHost='localhost';
    $mySqlUsername='';
    $mySqlPassword='';
    $mySqlDatabase='';
    $mySqlConnect=mysql_connect($mySqlHost,$mySqlUsername,$mySqlPassword) or die("Error Connecting to Database");
    mysql_select_db($mySqlDatabase,$mySqlConnect) or die ("Could not Select Database".mysql_error());

    $selectRecipeQuery="SELECT recipe_name,food_type_english FROM recipe WHERE recipe_id='$pagename'";
    $result=mysql_query($selectRecipeQuery) or die ("could not select data from table".mysql_error());

$tableRow=mysql_fetch_array($result);
        $recipe_name=$tableRow['recipe_name'];
        $food_type_english=$tableRow['recipe'];


    echo "<p>$recipe_name</p>";
    echo "<p>$food_type_english</p>";
?>
</body>
</html>

Please take a look to see if you can find out why it is not letting me display the data

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    I'm going to be the first to say this, since someone always does. Before you continue development consider switching to an up-to-date mysql library. The recommended one would be pdo, but there are others as well. – aurbano Mar 20 '14 at 14:45
  • You are missing a semicolon on this line: but that's the only problem I can see other than what @Chevi mentioned already. Did your host upgrade PHP and/or change the php.ini? – larsAnders Mar 20 '14 at 14:49
  • 1
    Did someone delete your database? maybe using SQL Injections? http://en.wikipedia.org/wiki/SQL_injection – Andresch Serj Mar 20 '14 at 14:51
  • @Chevi my sql is out of date? im a second year web student and this is the way i have been taught and im using the internal server supplied for us. Can you explain to me what you mean by the mysql library? – Website_Crazy Mar 20 '14 at 14:53
  • @Website_Crazy there are many resources on the subject, this question being the first I found: http://stackoverflow.com/questions/12097245/php-mysql-v-mysqli-v-pdo. Basically the mysql library dates from the very beginning and lacks many useful and necessary elements. – aurbano Mar 20 '14 at 14:56
  • @Website_Crazy he means use mysqli or PDO. Just because it's what you've been taught in school, doesn't mean it's necessarily right or up-to date, fyi – thescientist Mar 20 '14 at 14:58
  • @Chevi ah i get it now! i was just unsure what you meant i have seen mysqli around but a bit unsure what the difference between sqli and sqli thanks for the link :) – Website_Crazy Mar 20 '14 at 15:11
  • you should accept my answer if it fixed your prob. :) – Jage Mar 20 '14 at 17:39
  • @Jage i did not see this sorry but i allready tried to accept it but i need 15 rep to accept the awnser i only have 3 :/ – Website_Crazy Mar 22 '14 at 19:32
  • @larsAnders actually you can omit the ending semi-colon in a one-liner of PHP - though I'd never advise it. Magento2 seems to have opted for `= 'something' ?>` which makes me hate M2 even more. But that aside - it's not a syntax error – treyBake Nov 26 '18 at 17:13
  • as aurbano said - mysql is outdated - and even more so, it's removed in PHP7 - avoid using and switch to PDO or (less pref.) mysqli – treyBake Nov 26 '18 at 17:13
  • ^^just realised the age of the post #wubbalubbadubdub – treyBake Nov 26 '18 at 17:14

2 Answers2

1

You're passing $pagename rather than $recipeID to your query. Your query should be:

$selectRecipeQuery="SELECT recipe_name,food_type_english FROM recipe WHERE recipe_id='$recipeID'";

By passing the wrong value to your query, you get no results with no error.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jage
  • 7,990
  • 3
  • 32
  • 31
0

What about this?

Just change myDatabase to your database name

<!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" />
<link rel="stylesheet" type="text/css" href="Styles.css"/>
<?php
    error_reporting(E_ALL);
    $pagename=$_GET['Recipe'];
    $recipeID=$_GET['id'];
?>
<title><?php echo $pagename ?></title>
</head>
<body>
<?php
try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

    $stmt = $conn->prepare('SELECT recipe_name,food_type_english FROM recipe WHERE recipe_id = :id');
    $stmt->execute(array('id' => $recipeID));

    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo "<p>".$row['recipe_name']."</p>";
        echo "<p>".$row['food_type_english']."</p>";
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>
</body>
</html>
AdRock
  • 2,959
  • 10
  • 66
  • 106