1

I am not sure what is happening with my code, My project is running smoothly under XAMPP Server, i am able to make connection between mysql and php, But when i moved this to another system, which has mysql community server, their the code is not working, i mean database is connecting, but not able to fetch data from the database.

Sample Code is as follows:

$host='localhost'; // Host name
$username='root'; // Mysql username
$password=''; // Mysql password
$db_name='test'; // Database name
$connect=@mysql_connect($host,$username,$password) or die("Cannot Connect to database");
@mysql_select_db($db_name,$connect) or die ("Cannot find database");

app Code:

 $sql="SELECT * FROM test.userdet WHERE emailId='$uName' and pwd='$pwd'";
 $result=mysql_query($sql);
 $count=mysql_num_rows($result);

according to $count i am redirecting it to other page.

I able to run it on other systems which has XAMPP itself, but its not running with mysql community server.

//Thanks to NullPointer, i got it by PDO, from now onwords i will continue with PDO for transactions.

Now i facing a new problem, i am calling a php_db script via ajax, after executing all PDO statements its not returning to ajax, Code snippet is below:

ajax call:-

  $.ajax({
      type: "POST",
      url: "common/dbPhpScripts/updateEvent.php",
      data: 'json=' + det,
      dataType: 'json',
      cache: false,
      success: function () {
          $('#cal').fullCalendar('refetchEvents');
          $('#edit_event').dialog('close');
      }
  });

sample php code after using PDO:

$stringData = $_POST['json'];
$mynewarray = json_decode($stringData, true);
$title=$mynewarray['title'];
$start= $mynewarray['start'];
$end= $mynewarray['end'];
$createDate=$mynewarray['createDate'];
$insertQuery=$db->exec("INSERT INTO calendar (User_Id, StartDateTime, EndDateTime, Event_Title, Create_Date) VALUES ('07','$start','$end','$title','$createDate')");

where i am going wrong?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Raghuveer
  • 707
  • 7
  • 18
  • Dont suppress warnings, remove @ – swapnesh Feb 04 '13 at 05:59
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя Feb 04 '13 at 05:59
  • @NullPointer Ya username and password are same, and about your tutorial suggestion i will go through it. – Raghuveer Feb 04 '13 at 06:01
  • check error using die(mysql_error()) – iJay Feb 04 '13 at 06:14
  • @NullPointer Please post your suggestion as answer.. I modified it to PHP Data Objects, and nice tutorial he has given.. – Raghuveer Feb 05 '13 at 04:41
  • @Raghuveer about pdo ? – NullPoiиteя Feb 05 '13 at 04:42
  • @NullPointer ya about pdo, one more problem i am facing, i made call to the php through ajax and sending json object, and saving in db with PDO, but now ajax is not getting success call back. – Raghuveer Feb 05 '13 at 04:47
  • what error you getting ? – NullPoiиteя Feb 05 '13 at 04:50
  • @NullPointer Please look into the question, i have updated it with new code, i am not getting any error, but statements in success block are not executing – Raghuveer Feb 05 '13 at 05:03
  • what you getting in `success: function (data) {alert("data");}` also you need to echo result in `common/dbPhpScripts/updateEvent.php` not redirect if you redirect you wont get result in ajax .. – NullPoiиteя Feb 05 '13 at 05:07
  • @NullPointer I got it .. when i echo it, its returning to ajax success call back.. thank you.. please post it as answer, i want it to accept it as answer – Raghuveer Feb 10 '13 at 04:24

2 Answers2

1

its good to see what you getting in

success: function (data) {
   alert("data");
}

also you need to echo result in common/dbPhpScripts/updateEvent.php not redirect if you redirect you wont get result in ajax

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • 1
    i am accepting this answer for all the above suggestions by NullPointer, 1st suggestion, use PDO method to connect mysql, and the other one is for echoing the results of it, to get back to jquery ajax call. – Raghuveer Feb 10 '13 at 04:40
0

It defiantly should be your credentials. Make sure your Username and Password are correct as well as the Database name and the Host. Different server have different credentials.

EG :

WAMP

$username='root'; // Mysql username
$password=''; // Mysql password

MAMP

$username='root'; // Mysql username
$password='root'; // Mysql password

Given remove the @ sign. Don't suppress warnings using @SuppressWarnings

Techie
  • 44,706
  • 42
  • 157
  • 243
  • i dont know about the host sir, i checked about the user name and password, and accordingly i modified it, still no results, – Raghuveer Feb 04 '13 at 06:04
  • @Raghuveer Host does matter when you are connection to a database. Please check the error log if possible – Techie Feb 04 '13 at 06:11
  • one more thing to add in "check " my.ini file also – Raul Feb 04 '13 at 06:14