-2

when running this code

<?php
$con = mysql_connect("localhost","Grant","grant"); 
if (!$con)
{
die('Could not connect: ' . mysq_error());
}

if (mysql_query("CREATE DATATBASE my_db",$con))
{
echo "Database Created";
}
else 
{
echo "Error creating database: " . mysqlerror();
}
mysql_close($con);
?>

it prints

Could not connect: No such file or directory

I'm not sure what I'm doing wrong.... I'm running this locally under a user account on Mac OSX 10.7.4

edit: Thanks for catching the non initialization, but it's still not running

JaeGeeTee
  • 517
  • 1
  • 5
  • 23
  • 3
    Pretty sure both `mysq_error()` and `mysqlerror()` don't exist, so this is likely not the original code. – nickb Jul 14 '12 at 22:04
  • Probably duplicate: http://stackoverflow.com/questions/1676688/php-mysql-connection-not-working-2002-no-such-file-or-directory – Gabriel Santos Jul 14 '12 at 22:05
  • turn errors on in your script: error_reporting(E_ALL); ini_set('display_errors', 1); You would have received an error about $con not being set, and you would have fixed the error all by yourself! – Jocelyn Jul 14 '12 at 22:07
  • @nickb I got this straight off of w3schools... Just trying my hand at it. – JaeGeeTee Jul 14 '12 at 22:17
  • change `mysql_error()` and `mysqlerror()` to `mysql_error()` as suggested in the first comment and write what error do you get – Zefiryn Jul 14 '12 at 22:17

2 Answers2

2

You're not assigning $con

perhaps this:

$con = mysql_connect("localhost","Grant","grant");

as per example code at http://www.w3schools.com/php/func_mysql_connect.asp

pjama
  • 2,995
  • 2
  • 25
  • 27
  • Tried initializing with $con (edited it into the question, thank you!) and it's still unable to connect. Could it be a conflict as to where MySql is installed? – JaeGeeTee Jul 14 '12 at 22:13
  • @pjama Please see [W3Fools](http://w3fools.com) – Cole Tobin Jul 14 '12 at 22:18
  • Jae - I don't think we have enough information to help you with the installation. Gabriel mentioned a possible duplicate question, [here](http://stackoverflow.com/questions/1676688/php-mysql-connection-not-working-2002-no-such-file-or-directory) – pjama Jul 14 '12 at 22:21
0

You are testing not initialized variable and (!$con) returns true regardless of connection status try

$con = mysql_connect("localhost","Grant","grant");
Zefiryn
  • 2,230
  • 2
  • 20
  • 30
  • Tried initializing with $con (edited it into the question, thank you!) and it's still unable to connect. Could it be a conflict as to where MySql is installed? – JaeGeeTee Jul 14 '12 at 22:13