My sql connection does not work

by 8 replies
9
Hi

I found this partial script on another forum and thought this would be useful for aff link management. I've tried adding this:

PHP Code:
mysql_connect("localhost""admin""1admin") or die(mysql_error()); 
but it still does not connect. I've created the database, table, user, password and I know the info is
correct but for some reason it won't connect. Am I mission something?


Here is all of it:

PHP Code:
 <?php 
$id 
$_GET['id']; 
//validate id 
if(!is_numeric($id)) : 
?> 
Invalid url id 
<?php 
else : 
     
$query "SELECT url FROM tableName WHERE id = '$id';"
     
$res = @mysql_query($query); 

     if(!
$res) : 
?> 
An error has occured 
<?php 
     
else : 
          if(
$row = @mysql_fetch_array($res)) : 
               
header('Location: ' $row[0]); 
          else : 
?> 
URL could not be found 
<?php 
          
endif; 
     endif; 
endif; 
?>
#programming #connection #sql #work
  • Change this:

    See if that helps.
    • [1] reply
    • Make your script a little more verbose:

      change line to

      PHP Code:
      else : 
            = 
      "SELECT url FROM tableName WHERE id = '';"
            = @
      mysql_query() or die(mysql_error()); 

           if(!) : 
      This will give you more insight to what your problem might be. The top 3 things that go wrong:

      Problem in Query
      Wrong Database login information
      Server not working properly

      Making your code more verbose will help point it out. Remove it when you get the problem fixed and move into production.

      EDIT: For some reason the variables are being stripped from the code I'm posting. Just put the "or die(mysql_error()) after your mysql_query
      • [1] reply
  • Dont use "mysql_query".
    Use "mysql_query" for debugging.

    Putting "" in front of function name suppresses any error message (if any).
    So you don't know what's wrong with the script.
    Plus doing so consumes more memory (i read it somewhere), so use
    PHP Code:
    mysql_query("MY QUERY HERE") or die(mysql_error()); 
  • Sounds like a user permission or something...
    Can you log into PHPMyAdmin and make sure that the userid has permissions for the database? Just grant everything to it for your database.
    In all my years as a developer, user permissions are the issue for 99.99% of database connection problems.
  • Hi,

    Use this code for connecting MYSQL.

    PHP Code:
    <?php 
     
    // Connects to Our Database 
     
    mysql_connect("your.hostaddress.com""username""password") or die(mysql_error()); 
     
    mysql_select_db("Database_Name") or die(mysql_error()); 
     
    ?>
  • And your username is probably accountname_nameyouchose, your database is probably accountname_nameyouchose if you just aren't connecting.
  • Can you paste the error message you're getting?

    WP4H

Next Topics on Trending Feed