Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am attempting to connect a mysql database to a php code. This is the first time i am using php. I have used everything i know. the following code is only to check if the connection is working well. This is a part of the code where the php code is. It is inside the easy php folder and i have a database with the name "tennis". the connection has no password.

PHP
<?php
		$con = mysql_connect("localhost","root","");
		if (!$con)die('Could not connect: ' . mysql_error());
		mysql_select_db("tennis", $con);
		mysql_query("INSERT INTO users VALUES ('user', 'password', 'Male', 'full name', 'email@yahoo.com');");
		mysql_close($con);
	?>


when i open it in chrome i don't see any row added in mysql.
Posted

You should always check the return value of the mysql_ functions (e.g. mysql_select_db, mysql_query) and call mysql_error on failure (see, for instance, the code sample at mysql_select_db documentation page[^].
 
Share this answer
 
v2
Comments
Neville Nazerane 2-May-12 7:12am    
i am unable to view anything i echo so i can't test it. this php code is inside a html code.... in the head tags... but i barely see an echo at all so i dont even know if it is being called
PHP
mysql_query("INSERT INTO users VALUES ('user', 'password', 'Male', 'full name', 'email@yahoo.com');");
In the above line u should not use (;) in the query string check the link click .
 
Share this answer
 
v3
Comments
Neville Nazerane 2-May-12 7:13am    
i have removed it i can still not see any progress. i can't even see an echo that i set before any of this code.
Loke.mysore 2-May-12 7:19am    
As mentioned in the solution 1 use the mysql_error() method get error and echo it and tell us the error.
Mohibur Rashid 2-May-12 19:37pm    
Would you explain what could went wrong with ; in the sql query?

and why are you suggesting mysql_real_escape_string to support your statement?
Loke.mysore 3-May-12 0:27am    
Its my mistake and sorry for that, Instead of giving link to the mysql_query i gave it to mysql_real_escape_string.
And i updated the link.

http://www.php.net/manual/en/function.mysql-query.php Its is written in this website "The query string should not end with a semicolon." now i tried with semicolon , Its working.
I think they mean that semicolon is not required because mysql_query() will not support multiple queries.
Mohibur Rashid 3-May-12 1:12am    
yup, that is so :)
as cPallini says check your return value all the time.
C++
    $con = mysql_connect("localhost","root","");
    if (!$con)die('Could not connect: ' . mysql_error());
    mysql_select_db("tennis", $con);
    $ret=mysql_query("INSERT INTO users VALUES ('user', 'password', 'Male', 'full name', 'email@yahoo.com');",$con);
    if(!ret)
            {
             echo mysql_error($con);;
            }
            mysql_close($con);
?>


a bit suggestion if required also put the name of the fields to confirm your insertion column name
 
Share this answer
 
Before going to code in php try to get basic knowledge GET and POST after that create a dummy page in php with select statement and runs that before writing the query read mysql rules and syntax. Then you will get it easy to handle the output.

Never code directly from middle if new in any language.
 
Share this answer
 
As i said i was using easy php and i just did not know where to open the file from thats all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900