PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
tags when posting PHP code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hi all
I'm just starting to learn PHP and have used a tutorial to help me come up with the following code:
<?php
$username="mark";
$password="****";
$database="gamecal";
mysql_connect(127.0.0.1,$username,$password) or die( "Unable to connect");
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO games VALUES ('abc','123')";
mysql_query($query);
mysql_close;
?>
When I open the page, I get a blank page (to be expected) but the row is not inserted in to the table. To check the script is actually running, I stopped the MySQL server and refreshed the page, and got the result of the die() function ("Unable to connect" was printed on the screen).
I have checked the syntax of the commands and it all seems ok. I have also checked the names of the database, the table, and the user's login credentials.
Thanks Joe. I'll try that when I get home tonight. If I use error_reporting(E_ALL), do I need the die() statements? Or will PHP automatically display the errors at these points for me?
Also, should the field names be in quotes? (single or double?)
MarkAshley wrote:If I use error_reporting(E_ALL), do I need the die() statements? Or will PHP automatically display the errors at these points for me?
No, the error_reporting(E_ALL) function call will just ensure that you see all of PHP's errors - it will not capture your MySQL errors so keep the or die()s. But to make your MySQL errors more useful you will need the mysql_error() function and you would add this to the or die() statements:
<?php
$username = 'mark';
$password = '****';
$database = 'gamecal';
// it may be the 127.0.0.1 in this function causing the problem, try
// changing it to 'localhost' (in quotes) and see if it makes any
// difference
@mysql_connect(127.0.0.1, $username,$password)
or die('Unable to connect: '.mysql_error());
@mysql_select_db($database)
or die('Unable to select database: '.mysql_error());
$query = "INSERT INTO games VALUES ('abc','123')";
mysql_query($query)
or die('Query (<b>'.$query.'</b>) Failed: '.mysql_error());
mysql_close;
?>
MarkAshley wrote:Also, should the field names be in quotes? (single or double?)
Neither, but you can use backticks (`) around them.
Thanks everyone. I tried the code suggested but it still didn't work I then tried changing 127.0.0.1 to localhost, and got the following error:
Client does not support authentication protocol requested by server; consider upgrading MySQL client
I added "echo mysql_get_client_info();" to the beginning of the php script and the version was reported as 3.23.49. When I installed MySQL I downloaded MySQL Standard 4.1.15. I'm quessing I need to upgraded PHP on my iBook, but I can't find a version that matches the verison of MySQL. Anybody know what version of PHP will match my version of MySQL?
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
hi,
im also new to PHP and MySQL, but i dont received any problem such as yours. could you atlist show us the changes you made after the sugestions from other user? so that we can follow.
[color=orange]// try again with this code[/color]
[color=red]<?php[/color]
$host [color=blue]=[/color] [color=brown]"localhost"[/color];
$username [color=blue]=[/color] [color=brown]"mark"[/color];
$password [color=blue]=[/color] [color=brown]"somepass"[/color];
$database [color=blue]=[/color] [color=brown]"gamecal"[/color];
[color=blue]mysql_connect[/color]($host,$username,$password) or [color=blue]die[/color]( "Unable to connect");
[color=orange]// if you notice i romove the '@' symbol[/color]
[color=blue]mysql_select_db[/color]($database) or [color=blue]die[/color]( "Unable to select database");
[color=orange]// columnames... are the receiving variable from your database like ex: id, name, address, etc.[/color]
$query = [color=brown]"INSERT INTO games (columname1,columname2,columnames...) VALUES ('abc','123')"[/color];
[color=blue]mysql_query[/color]($query);
[color=blue]mysql_close[/color];
[color=red]?> [/color]
[color=red][b]Sami[/b] | Please use