giannis196 Posted October 6, 2019 Share Posted October 6, 2019 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? Link to comment Share on other sites More sharing options...
giannis196 Posted October 6, 2019 Author Share Posted October 6, 2019 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! Link to comment Share on other sites More sharing options...
iwato Posted October 7, 2019 Share Posted October 7, 2019 Have you tried inserting the following var_dump($query); after the expression $query = $select ->fetch(); to see, if anything is contained therein? Roddy Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now