Author Topic: Fatal error: Call to undefined function mysql_real_connect()  (Read 1157 times)

ghostrider1965

  • New Poster
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Fatal error: Call to undefined function mysql_real_connect()
« on: March 18, 2008, 12:24:29 PM »
Fatal error: Call to undefined function mysql_real_connect()
Hello all,
 I get the above error when trying to work through a tutorial.
I am running Vista with a xamp server setup. phpMyAdmin says that MySQL is running ok. The PHP.ini file has the appropriate bits selected, the php_mysql.dll etc
The  website http://dev.mysql.com/doc/refman/5.0/en/ ... blems.html tells me that this error is caused by PHP not being compiled with MySQL support.
Can somebody tell me how and where I can do this.
The tutorial  comes from the book  PHP_MySQL_Programming_For_The_Absolute_Beginner_2003.
The server works well enough, runs php files ok. Just when I am trying to access the database, which is there as seen with phpMyAdmin, that I get the error message.

Mike
 :(

ghostrider1965

  • New Poster
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: Fatal error: Call to undefined function mysql_real_connect()
« Reply #1 on: March 18, 2008, 12:39:52 PM »
Oops, the error message points to the following line,
   $conn = mysql_real_connect("localhost", "", "");
   $select = mysql_select_db("chapter7", $conn);

I have added this at the beginning,
Code: [Select]
<?php
$link 
mysql_connect('localhost''''');
if (!
$link) {
die(
'Could not connect&#58; ' &#46; mysql_error());
}
echo 
'Connected successfully Dude';

mysql_close($link);
//?>

and get the message, Connected successfully Dude, but then get the error following that.

Here is all the script
Code: [Select]
<html>
<head>
<title>SaveRoom.php</title>
</head>
<body>



<?php
$link 
mysql_connect('localhost''''');
if (!
$link) {
die(
'Could not connect&#58; ' &#46; mysql_error());
}
echo 
'Connected successfully Dude';

mysql_close($link);
//?>










//<?PHP
//Once a room has been edited by editSegment, this program
//updates the database accordingly.

//check to see what values came in

foreach ($_REQUEST as $key=>$value){
  //print "$key: $value<br>";
} // end foreach


//connect to database
$conn = mysql_real_connect("localhost", "", "");
$select = mysql_select_db("chapter7", $conn);

$sql = <<<HERE
UPDATE adventure
SET
 name = $name,
 description = $description,
  north = $north,
  east = $east,
  south = $south,
 west = $west
WHERE
  id = $id

HERE;
//print $conn;
//print $select;


//print $sql;
$result = mysql_real_query($sql);
print $result;

if ($result){
  print "<h3>$name room updated successfully</h3>n";
  print "<a href = "listSegments.php">view the rooms</a>n";
} else {
  print "<h3>There was a problem with the database</h3>n";
} // end if

?>
</body>
</html>
 
I am running this on my machine at home.
Thanks
Mike

Zyppora

  • Global Moderator
  • *****
  • Posts: 1401
  • Karma: +0/-0
    • View Profile
Re: Fatal error: Call to undefined function mysql_real_connect()
« Reply #2 on: March 19, 2008, 02:12:03 PM »
mysql_real_connect() is not a default function, so unless you initialized a custom function with that name, the fatal error will keep occurring.
HAVE YOU TRIED DEBUGGING? Example code in this reply deliberately contains BUGS.

ghostrider1965

  • New Poster
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: Fatal error: Call to undefined function mysql_real_connect()
« Reply #3 on: March 20, 2008, 04:29:36 AM »
Thanks for that.
         The php.net site suggested that the function mysql_connect() was deprecated and that I should use mysql_real_connect() instead.
That aside, I have used the former in my script.I added some echo statements to find what output there was.
The real problem was here.
$sql = <<<HERE
   UPDATE adventure
   SET
     name = '$name',
     description = ',$description',
    north = '$north',
    east = '$east',
    south = '$south',
     west =' $west'
   WHERE
    id =' $id'

HERE;

There were some comma's and apostrophes missing, see red.

That part of the procedure now works and saves what it is supposed to save.
Now I have the next bit to fix.


The next piece of code is the game itself. Select a button and hit 'go'.
However the game simply refreshes the page without updating the database and moving on to the next page. I am sure it is a simple small error somewhere, but suggestions are welcome.


Code: [Select]
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Show Segment</title>
<style type = "text/css">
body {
  color:red
}
td {
  color: white;
  background-color: blue;
  width: 20%;
  height: 3em;
  font-size: 20pt
}
</style>
</head>

<body>

<?PHP
if (empty($room)){
  $room = 1;
} // end if

//connect to database
$conn = mysql_connect("localhost", "", "");
$select = mysql_select_db("chapter7", $conn);
$sql = "SELECT * FROM adventure WHERE id = '$room'";
$result = mysql_query($sql);
$mainRow = mysql_fetch_assoc($result);
$theText = $mainRow["description"];


/*
//print "theText is $theText<br>n";
foreach ($mainRow as $key=>$value){
  print "$key: $value<br>n";
} // end foreach
*/

$northButton = buildButton("north");
$eastButton = buildButton("east");
$westButton = buildButton("west");
$southButton = buildButton("south");
$roomName = $mainRow["name"];

print <<<HERE
<center><h1>$roomName</h1></center>
<form method = "post">
<table border = 1>
<tr>
  <td></td>
  <td>$northButton</td>
  <td></td>
</tr>

<tr>
  <td>$eastButton</td>
  <td>$theText</td>
  <td>$westButton</td>
</tr>

<tr>
  <td></td>
  <td>$southButton</td>
  <td></td>
</tr>

</table>
<center>
<input type = "submit"
       value = "go">
</center>
</form>

HERE;

function buildButton($dir){
  //builds a button for the specified direction

 global $mainRow, $conn;
  $newID = $mainRow[$dir];
  //print "newID is $newID";
  $query = "SELECT name FROM adventure WHERE id = $newID";
  $result = mysql_query($query, $conn);
  $row = mysql_fetch_assoc($result);
  $roomName = $row["name"];

  $buttonText = <<< HERE
  <input type = "radio"
         name = "room"
         value = "$newID">$roomName

HERE;

  return $buttonText;

} // end build button
?>
</body>
</html>


Thanks
Mike

ghostrider1965

  • New Poster
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: Fatal error: Call to undefined function mysql_real_connect()
« Reply #4 on: March 20, 2008, 05:11:16 AM »
Yes I have found the problem. After going away for a cup of coffee, emptying my mind and starting afresh, I have discovered a small error.
I needed to add this statement $room = $_POST[room]; in the beginning of the code just after the <?PHP. Now it works as advertised.
Thanks
Mike
:D

Zyppora

  • Global Moderator
  • *****
  • Posts: 1401
  • Karma: +0/-0
    • View Profile
Re: Fatal error: Call to undefined function mysql_real_connect()
« Reply #5 on: March 20, 2008, 12:54:41 PM »
mysql_connect() is, as far as I know, not deprecated at all, and I can't find any such information on php.net. Furthermore, php.net does not contain mysql_real_connect() in its list of MySQL functions, so if you found it somewhere, could you please post the link to the webpage here, so I can see it for myself?

Also, when something is 'deprecated', it usually is starting from a certain version of PHP. Always be sure to check which version you're running and which function is to be used in that version.
HAVE YOU TRIED DEBUGGING? Example code in this reply deliberately contains BUGS.

ghostrider1965

  • New Poster
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: Fatal error: Call to undefined function mysql_real_connect()
« Reply #6 on: March 20, 2008, 01:24:47 PM »
http://dev.mysql.com/doc/refman/5.1/en/ ... nnect.html

Zyppora

  • Global Moderator
  • *****
  • Posts: 1401
  • Karma: +0/-0
    • View Profile
Re: Fatal error: Call to undefined function mysql_real_connect()
« Reply #7 on: March 20, 2008, 04:33:09 PM »
That page has the following breadcrumb:

MySQL 5.1 Reference Manual :: 25 APIs and Libraries :: 25.2 MySQL C API :: 25.2.3 C API Function Descriptions :: 25.2.3.7 mysql_connect()

In the C library, mysql_connect() is deprecated, yes. But C and PHP are two different languages.
HAVE YOU TRIED DEBUGGING? Example code in this reply deliberately contains BUGS.