Jump to content

Problem with getting php to talk to mysql


Strahan

Recommended Posts

PHP version: 5.2.6

MySQL version: 5

----------------------

I'm just learning php and decided to try to make my own version of a tinyurl.com like service.  I have a form that collects the url to link to and the name to use for the shortened url then tries to write to a database.  This is the code:

 

echo "Creating new URL ...<BR><BR>";
$query = "INSERT INTO URLs (owner, sourceurl, tagname) VALUES (1, '" . $_REQUEST["sourceurl"] . "', '" . $_REQUEST["newtag"] . "')";
$db = mysql_connect(localhost,"username","password");
mysql_select_db("urls") or die( "Unable to select database");
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
echo "New record inserted with ID ".mysql_insert_id(); 
mysql_free_result($result);
mysql_close($db);

 

When I run it, I get:

 

Fatal error: Call to undefined function mysql_connect() in D:\sites\url\index.php on line 22

 

Now I did see the post here talking about how to fix it.  I followed all the steps...  I set the following things in my d:\apps\php\php.ini:

extension_dir = "d:\apps\php"

extension=php_mysqli.dll

 

I made sure there weren't extra copies of the DLLs floating around.  I copied libmysql.dll from d:\apps\mysql\bin to c:\windows\system32.  I stopped and restarted IIS, but still no dice.  In my PHP folder (d:\apps\php) I have the following files:

install.txt

license.txt

news.txt

php.exe

php.gif

php.ini

php_mbstring.dll

php_mysqli.dll

php5embed.lib

php5isapi.dll

php5ts.dll

 

When I installed php and mysql, it didn't give me any mysql stuff so I had to download that separately.  However, I couldn't find php_mysql.dll.  If I understood what I read (and that's not alltogether likely I guess hehe), php_mysqli.dll is the newer version of that so I downloaded it.  Do I still need php_mysql.dll as well?  When I went to the mysql download link and searched for "php", the only thing I could find was that mysqli.  I did a Google for php_mysql.dll and found a dll download site with it, but not coming from mysql or php themselves I am kinda distrustful of it. 

 

PS, phpMyAdmin works fine which really perplexes me because to me, that indicates php and mysql are talking just fine.  Ugh!

 

Thanks!

 

Link to comment
Share on other sites

Try this for you connection:

 

$host="localhost";
$user_name="username";
$password="password";
$db="urls";

$connection = mysql_connect($host, $user_name, $password) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");

Link to comment
Share on other sites

Thanks guys.  Man I feel stupid, banging my head against the wall looking at the php install when it was just bad code hehe.  I noticed most of the tutorials I see are using mysql_ calls.  Is the mysqli thing new or something?  Do mysqli_ and mysql_ functions generally take the same parms or should I only be looking for tuts with mysqli?

 

Thanks again, I really appreciate it!

Link to comment
Share on other sites

mysqli_ was introduced when PHP5 was released. It allows you you take advantage of MySQL5's features.

 

the mysqli_* functions behave differently compared to the older mysql_* functions. I would recommend you to read the manual for working with MySQLi.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.