Jump to content
giannis196

I have define the variable $query but it says it's undefined variable

Recommended Posts

Hi everyone, so today i started writing a page in PHP until i got this error message:
Notice: Undefined variable: query in C:\Users\Ioann\usbwebserver\root\carshop\index.php on line 19
The variable query is defined in functions.php and yet it still thinks it's undefined

my functions.php

<?php
$host="localhost";
$username="root";
$password="usbw";
$dbname="test";
$pdo= new PDO("mysql:host=$host; dbname=$dbname", $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES utf8"]);
try{
}
catch(PDOException $e){
    echo "error please try again";
}

function appearcars(){
$select = $pdo ->prepare("SELECT * FROM `carshopdatabase` ");
$select ->execute();
$query = $select ->fetch();
}

?>

and the index.php
 

<?php
require("functions.php");
?>
<html>
<head><title>car shop - educational purposes please don't take anything seriously!!</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="utf-8" http-equiv="encoding">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="a">Our shop have the best cars around!!!</div>
<div id="lmenu"><ul>
    <li class="top">Home</li>
    <li>Browse cars</li>
    <li>Spare parts</li>
    <li class="bottom">Contuct us</li>
</ul></div>
<div id="carlist"><?php echo $query[1]["cost"]; ?></div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="scripts.js"></script>
</body>

</html>

so, what's going wrong here?

Share this post


Link to post
Share on other sites
44 minutes ago, giannis196 said:

Hi everyone, so today i started writing a page in PHP until i got this error message:
Notice: Undefined variable: query in C:\Users\Ioann\usbwebserver\root\carshop\index.php on line 19
The variable query is defined in functions.php and yet it still thinks it's undefined

my functions.php


<?php
$host="localhost";
$username="root";
$password="usbw";
$dbname="test";
$pdo= new PDO("mysql:host=$host; dbname=$dbname", $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES utf8"]);
try{
}
catch(PDOException $e){
    echo "error please try again";
}

function appearcars(){
$select = $pdo ->prepare("SELECT * FROM `carshopdatabase` ");
$select ->execute();
$query = $select ->fetch();
}

?>

and the index.php
 


<?php
require("functions.php");
?>
<html>
<head><title>car shop - educational purposes please don't take anything seriously!!</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="utf-8" http-equiv="encoding">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="a">Our shop have the best cars around!!!</div>
<div id="lmenu"><ul>
    <li class="top">Home</li>
    <li>Browse cars</li>
    <li>Spare parts</li>
    <li class="bottom">Contuct us</li>
</ul></div>
<div id="carlist"><?php echo $query[1]["cost"]; ?></div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="scripts.js"></script>
</body>

</html>

so, what's going wrong here?

Ok, i found it!
it couldn't found it because it was in the function that i wasn't run it yet!

Share this post


Link to post
Share on other sites

Have you tried inserting the following

var_dump($query);

after the expression

$query = $select ->fetch();

to see, if anything is contained therein?

Roddy

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...