justin7410 Posted March 27, 2013 Share Posted March 27, 2013 I have a variable called $sqlCommand , variable is listed twice with two different values. the first $sqlCommand is holding a value of creating a table called page the second $sqlCommand is holding a value of create a table called blog As you can see in the code below : // Create the table 1 for storing pages $sqlCommand = "CREATE TABLE pages ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, page_title VARCHAR(255), page_body TEXT, page_views INT NOT NULL default '0', FULLTEXT (page_title,page_body) ) ENGINE=MyISAM"; $query = mysql_query($sqlCommand) or die(mysql_error()); echo "<h3>Success creating Pages table</h3>"; // Create the table 2 for storing Blog entries $sqlCommand = "CREATE TABLE blog ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, blog_title VARCHAR(255), blog_body TEXT, blog_views INT NOT NULL default '0', FULLTEXT (blog_title,blog_body) ) ENGINE=MyISAM"; $query = mysql_query($sqlCommand) or die(mysql_error()); echo "<h3>Success creating Blog table</h3>"; My question is how is php reading this as two separate values ?? they both seem to be declared in a global fashion , yet when they are inputted into the $query function they both are produced separately ? anybody know the reason for this ? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/276237-a-variable-in-php-holding-two-different-values/ Share on other sites More sharing options...
akphidelt2007 Posted March 27, 2013 Share Posted March 27, 2013 (edited) You are declaring the variable, then running the query, then re-declaring the variable erasing the previous value. if you did this... it would only take the 2nd $sqlCommand. $sqlCommand = "CREATE TABLE pages ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, page_title VARCHAR(255), page_body TEXT, page_views INT NOT NULL default '0', FULLTEXT (page_title,page_body) ) ENGINE=MyISAM"; // Create the table 2 for storing Blog entries $sqlCommand = "CREATE TABLE blog ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, blog_title VARCHAR(255), blog_body TEXT, blog_views INT NOT NULL default '0', FULLTEXT (blog_title,blog_body) ) ENGINE=MyISAM"; $query = mysql_query($sqlCommand) or die(mysql_error()); Edited March 27, 2013 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/276237-a-variable-in-php-holding-two-different-values/#findComment-1421509 Share on other sites More sharing options...
justin7410 Posted March 27, 2013 Author Share Posted March 27, 2013 Yes this is what i thought would happen originally, but at the moment i'm watching a PHP tutorial where the person uses this code and implements this code in his video. the code spits out perfectly when he connects to a live server , and echos out all the tables have been created and the data has been inserted. this is just the script from his website to copy and paste to follow along THE ENTIRE CODE HERE: <?php/********************************************************* MySQL PHP Search Exercises by Adam Khoury @ developphp.com MySQL Database Version Used In the Lessons: 5.1.58 PHP Version Used in the Lessons: 5.2.17 For Code Logic and Code Explanations Watch the Videos *********************************************************/ // This script is for auto creating and populating two tables // needed for the exercises in this series // ------------------------------------------------ // Force script errors and warnings to show during production only error_reporting(E_ALL); ini_set('display_errors', '1'); // Connect to your MySQL database here include_once("connect_to_mysql.php"); // Create the table 1 for storing pages $sqlCommand = "CREATE TABLE pages ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, page_title VARCHAR(255), page_body TEXT, page_views INT NOT NULL default '0', FULLTEXT (page_title,page_body) ) ENGINE=MyISAM"; $query = mysql_query($sqlCommand) or die(mysql_error()); echo "<h3>Success creating Pages table</h3>"; // Create the table 2 for storing Blog entries $sqlCommand = "CREATE TABLE blog ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, blog_title VARCHAR(255), blog_body TEXT, blog_views INT NOT NULL default '0', FULLTEXT (blog_title,blog_body) ) ENGINE=MyISAM"; $query = mysql_query($sqlCommand) or die(mysql_error()); echo "<h3>Success creating Blog table</h3>"; // Insert dummy data into the pages table $sqlCommand = "INSERT INTO pages (page_title,page_body) VALUES ('PHP Random Number','Learn to generate a random number using PHP blah blah blah'), ('Javascript Clock','Build a Javascript digital clock in this tutorial blah blah blah'), ('Flash Gallery','Use Actionscript to build a Flash Photo gallery blah blah blah'), ('XML RSS FEED','Learn to assemble XML RSS Feeds for your website blah blah blah'), ('CSS Shadows','Learn text and container shadow techniques using CSS blah blah blah'), ('HTML5 Canvas Tag','Learn how to animate the HTML5 Canvas tag using blah blah blah'), ('PHP Calculator','Learn to build a working calculator using PHP and blah blah blah'), ('Flash Website','Learn to create a full flash website template blah blah blah')"; $query = mysql_query($sqlCommand) or die(mysql_error()); echo "<h3>Success populating the pages table with data</h3>"; // Insert dummy data into the blog table $sqlCommand = "INSERT INTO blog (blog_title,blog_body) VALUES ('Trip to Disney World','Disney world would have been fun if I were 10 blah blah blah'), ('Refrigeration School','I graduated school finally after blah blah blah'), ('Big Giant Doodoo','In the bathroom today I made the biggest doodoo blah blah blah'), ('New Pet Bird','Today I got a new bird that repeats everything blah blah blah'), ('Pet Bird Died','My cat ate my bird today so as punishment I blah blah blah')"; $query = mysql_query($sqlCommand) or die(mysql_error()); echo "<h3>Success populating the blog table with data</h3>"; ?> At first, i thought he was placing a variable that you yourself are suppose to rename to whatever you want, then watching along further he was just using the same variable. is there something i'm simply missing or not understanding ? Quote Link to comment https://forums.phpfreaks.com/topic/276237-a-variable-in-php-holding-two-different-values/#findComment-1421515 Share on other sites More sharing options...
Barand Posted March 27, 2013 Share Posted March 27, 2013 Put query into the variable Run query to create table Put second query into variable Run query to create second table Put tea in cup Drink it Put coffee in cup Drink it Quote Link to comment https://forums.phpfreaks.com/topic/276237-a-variable-in-php-holding-two-different-values/#findComment-1421521 Share on other sites More sharing options...
mac_gyver Posted March 27, 2013 Share Posted March 27, 2013 variables are called variables because what they hold can vary - "Something that varies or is prone to variation." this allows you to have and reuse one variable who's name indicates the purpose of the data in the variable, $sqlCommand in this case. the alternative would be a defined constant - "Something that is unchanging or invariable." then you would need to think of and keep track of different names for every defined constant you use in a program, but that makes more work when programming and actually uses more memory. Quote Link to comment https://forums.phpfreaks.com/topic/276237-a-variable-in-php-holding-two-different-values/#findComment-1421522 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.