Fatal error: Call to undefined function mysql_real_connect()

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

:(

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]<?php
$link = mysql_connect(‘localhost’, ‘’, ‘’);
if (!$link) {
die('Could not connect: ’ . mysql_error());
}
echo ‘Connected successfully Dude’;

mysql_close($link);
//?>
[/code]
and get the message, Connected successfully Dude, but then get the error following that.

Here is all the script[code]

SaveRoom.php <?php $link = mysql_connect('localhost', '', ''); if (!$link) { die('Could not connect: ' . 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
”;
} // 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 “

$name room updated successfully

n”;
print “view the roomsn”;
} else {
print “

There was a problem with the database

n”;
} // end if

?>

[/code] I am running this on my machine at home. Thanks Mike

mysql_real_connect() is not a default function, so unless you initialized a custom function with that name, the fatal error will keep occurring.

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]<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>

Show Segment body { color:red } td { color: white; background-color: blue; width: 20%; height: 3em; font-size: 20pt } <?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
n"; foreach ($mainRow as $key=>$value){ print "$key: $value
n"; } // end foreach */ $northButton = buildButton("north"); $eastButton = buildButton("east"); $westButton = buildButton("west"); $southButton = buildButton("south"); $roomName = $mainRow["name"]; print <<<HERE

$roomName

$northButton
$eastButton $theText $westButton
$southButton
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 $roomName HERE; return $buttonText; } // end build button ?>

[/code]

Thanks
Mike

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

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.

http://dev.mysql.com/doc/refman/5.1/en/ … nnect.html

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.

Sponsor our Newsletter | Privacy Policy | Terms of Service