Jump to content

Help understanding something in php code


jeff5656

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.