jeff5656 Posted March 2, 2008 Share Posted March 2, 2008 I am learning PHP and MYSQL. I have a question which is very likely to be simple for everyone reading this, but i can't get beyond the book until I understand this. In the following code for creating a database: <?php include 'config.php'; include 'opendb.php'; $query = "CREATE DATABASE phpcake"; $result = mysql_query($query); include 'closedb.php'; ?> How is the database phpcake created if the command to create it is assigned to a variable ($query)? Another example in the book: $connect = mysql_connect ("local host", "bp5am", "bpmpass") or die ("can not connect"); But the rest of the example never refers to the variable $connect! So how did it connect? Shouldn't the code have read: mysql_connect ("local host", "bp5am", "bpmpass") or die ("can not connect"); without the variable assignment? Quote Link to comment https://forums.phpfreaks.com/topic/94030-help-understanding-something-in-php-code/ Share on other sites More sharing options...
imperialized Posted March 2, 2008 Share Posted March 2, 2008 for your $connect question, even though you are setting it to a variable the connect code still runs. For example: if you were to use $number = 1 + 1; it will still make $number = 2. So you see, when using variables the code is still executed. For the other question, how is the Table created when it is assigned to the variable: After you assigned the first variable, your $result = mysql_query($x); <-- this is where the table is created. You query your first variable here, which just helps keep it clean. I hope this helps you, I can further elaborate if you need but I gotta get heading to work. Quote Link to comment https://forums.phpfreaks.com/topic/94030-help-understanding-something-in-php-code/#findComment-481700 Share on other sites More sharing options...
ohdang888 Posted March 2, 2008 Share Posted March 2, 2008 everyone, correct me if i'm wrong on this, but i think i know... when you do this: mysql_query($query) You are saying, carry out the command stored in the string "$query" you really wouldn't even need to put the "$result" in your command (ex. $result = mysql_query($query)), because you are making something, and no info is being drawn from it. But if your query was getting info from a database, then you would need to use $result btw!!!!! modify this in your thread to look like mine below . You just put your database username and password out there for us to see. we don't know your site, but if we every figured out the name of your site, that could be used against you. BUT DON"T change it in your file. $connect = mysql_connect ("local host", "-----", "------") or die ("can not connect"); Quote Link to comment https://forums.phpfreaks.com/topic/94030-help-understanding-something-in-php-code/#findComment-481704 Share on other sites More sharing options...
phpSensei Posted March 2, 2008 Share Posted March 2, 2008 The variables are read by PHP, all of them, any functions used in a variable are launched. So when you put mysql_connect, mysql_query, explode(), date(), all of these functions are initiated when the script is launched. <?php include 'config.php'; include 'opendb.php'; $query = "CREATE DATABASE phpcake"; // The Query $result = mysql_query($query); // Launch The Query // Use the Query include 'closedb.php'; ?> The $result launches the query, so the data is now in $result. How ever you want to manipulate the data is your choice. Quote Link to comment https://forums.phpfreaks.com/topic/94030-help-understanding-something-in-php-code/#findComment-481708 Share on other sites More sharing options...
jeff5656 Posted March 2, 2008 Author Share Posted March 2, 2008 Wow people reply quickly on this site! I see how $connect gets executed now, but then why assign it instead of just running mysql_connect( etc...) alone? As for ohdang88, that was just a test database. the usernames and pws will not be like that when I create the real database after I learn how to do all this! But thanks for pointing that out. you said "...But if your query was getting info from a database, then you would need to use $result" Why would you then need to use $result? Quote Link to comment https://forums.phpfreaks.com/topic/94030-help-understanding-something-in-php-code/#findComment-481709 Share on other sites More sharing options...
phpSensei Posted March 2, 2008 Share Posted March 2, 2008 Wow people reply quickly on this site! I see how $connect gets executed now, but then why assign it instead of just running mysql_connect( etc...) alone? As for ohdang88, that was just a test database. the usernames and pws will not be like that when I create the real database after I learn how to do all this! But thanks for pointing that out. you said "...But if your query was getting info from a database, then you would need to use $result" Why would you then need to use $result? It doesn't make a difference without the function in the variable, or without it. Remember, variables hold the data, if you didnt put it in a variable, then that FUNCTION your using is floating in your script. Things like mysql_connect, or select_db should be used once in your script, but if you use things like split, explode, query, then your data has to be manipulated through the page, thats why you put them in vars. Quote Link to comment https://forums.phpfreaks.com/topic/94030-help-understanding-something-in-php-code/#findComment-481711 Share on other sites More sharing options...
jeff5656 Posted March 2, 2008 Author Share Posted March 2, 2008 any functions used in a variable are launched. Aha! now I get it. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/94030-help-understanding-something-in-php-code/#findComment-481712 Share on other sites More sharing options...
phpSensei Posted March 2, 2008 Share Posted March 2, 2008 any functions used in a variable are launched. Aha! now I get it. thanks. Read my post above, if you want to manipulate the function's return data, you put it in a var and change it through out your script. EXAMPLES: <?php $query = mysql_query("SELECT * FROM table"); // Put the query's data in a var; // do something // do something if(mysql_num_rows($query)==0){ echo "no data"; } ################################################ mysql_query("SELECT * FROM table"); // Launch the function // do something // do something // AH, whats this? You cant manipulate the query you just made because nothing is holding the DATA. Its just floating around, thats why we put them in VARS, but if you used something like MYSQL_CONNECT, then you only need it once, therefor the function doesnt need to be used oftenly. ?> Quote Link to comment https://forums.phpfreaks.com/topic/94030-help-understanding-something-in-php-code/#findComment-481715 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.