Jump to content

PHP newb question


elementis0

Recommended Posts

Ok well im learning php right now and im on the mysql part of the tutorial and im having trouble understand something. but first heres the code:

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("my_db", $con);$sql="INSERT INTO person (FirstName, LastName, Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 record added";mysql_close($con)?>

I understand everything until it says if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";when I read that part of the script heres how i understand it: if the mysql query is having a problem it wont use the $sql variable and if its not having an error then it will say one record added. But when in the script does it say that it will use $sql to add the records. I only see it using the variable in order to show the error, and the script doesnt say to use the variable in order to add the records.Oh and one more thing im having trouble understanding, as Im learning the mysql part of php Ive noticed that I have to keep using $con to keep connecting to the database. Why does it require you to keep reconnecting it so often in the statements and whats the logic to know when to use it, with my logic I would think you would only have to connect to it once and you wouldnt need to use $con again until you used something like mysql_close($con); but my thinking is obviously wrong. Can somebody please explain the script a bit more thoroughly so I can get what its saying.

Link to comment
Share on other sites

When you see a function call like this:mysql_query($sql,$con)That is calling the mysql_query function and sending it the $sql and $con variables. The $sql variable contains the SQL statement for the database to execute, and the $con variable is the connection to use. If running the SQL query fails or results in an error, the mysql_query function will return a value of false. So this if statement:if (!mysql_query($sql,$con))is checking if the return value of mysql_query is false. If so, then it displays the error message. The $sql variable there just holds the query, you could also call mysql_query like this:

if (!mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')", $con))

It would do the same thing.The $con variable holds a resource that represents the connection to the MySQL server. Using the $con variable does not cause the web server to re-connect to the database server, you are just telling the web server which existing connection to use. You create the connection with mysql_connect. You can also leave out the connection to use a default connection. This code will still run fine:

<?phpif (!mysql_connect("localhost","peter","abc123"))  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("my_db");$sql="INSERT INTO person (FirstName, LastName, Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql))  {  die('Error: ' . mysql_error());  }echo "1 record added";mysql_close();?>

You would use your own connection variable if you are developing an application that may run inside or be included in another application, and you want to make sure your application will use a specific database connection instead of the default connection.

Link to comment
Share on other sites

ok well you really helped me understand why that the info in $con just specifies what to use so thank you for that but, im still having trouble understanding the other part"is checking if the return value of mysql_query is false. If so, then it displays the error message. The $sql variable there just holds the query, you could also call mysql_query like this"Yes, I understand that the script does that, but the $sql data in the script is only used to return a value of false. I do not see how it uses the info in $sql to return a value of true in order to insert the data in the database.Why does it insert that $sql data in the database when it is true, when it is only using the data to return a false statement.

Link to comment
Share on other sites

You're misunderstanding how it works.

but the $sql data in the script is only used to return a value of falseit is only using the data to return a false statement
Why do you think that is true? That doesn't make sense to me.The mysql_query function sends the SQL statement to the MySQL database. The database server runs the SQL statement, and returns the result back to mysql_query. If the query was a SELECT statement that returned records, mysql_query will get back a set of records from the database. If it is a query like INSERT, UPDATE, or DELETE, the database will just indicate that the operation succeeded and mysql_query will return a value of true to indicate that. If there was an error running the query, the database will indicate that to mysql_query, and the function will return false. If it returns false, then the mysql_error function will read the error message from the database server.It doesn't "use the query to return false", IF the query returned false, THEN it will display the error.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...