My sql connection does not work

8 replies
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:

hi i have a database like this

id | sitename | url
1 ABC abc.com
2 XYZ xyz.com
3 PPP ppp.com

i wanted to create a redirect script like www.mysite.com/goto.php?url=(id number) and that id number would lead to related url respectively.

for example if i try www.mysite.com/goto.php?url=2 then xyz.com will open
and if i try www.mysite.com/goto.php?url=1 then abc.com will open
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; 
?>
#connection #sql #work
  • Profile picture of the author Havenhood
    Change this: $query = "SELECT url FROM tableName WHERE id = '$id';";

    to

    $query = "SELECT url FROM tableName WHERE id = '{$id}'";

    See if that helps.
    Signature

    --= -Spazzle- =--

    {{ DiscussionBoard.errors[3097281].message }}
    • Profile picture of the author Jeremy Morgan
      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
      Signature
      Jeremy Morgan, Software Developer / SEO
      Learn AI Powered Content Creation
      at Prompt and Prosper
      {{ DiscussionBoard.errors[3097727].message }}
      • Profile picture of the author artekweb
        According to me you should replace


        $query = "SELECT url FROM tableName WHERE id = '$id';";

        with


        $query = "SELECT url FROM tableName WHERE id = " . $id";

        because id is numeric
        Signature
        Wizhoo.com Social Media Reseller Panel - 30+ Services
        Facebook, Youtube like/dislike/ safe views , Twitter, Instagram
        Google Plus, Pinterest, Instagram, Vimeo, Vine​
        {{ DiscussionBoard.errors[3098056].message }}
  • Profile picture of the author xrvel
    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()); 
    {{ DiscussionBoard.errors[3098787].message }}
  • Profile picture of the author dgently42
    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.
    {{ DiscussionBoard.errors[3101745].message }}
  • Profile picture of the author raj23
    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()); 
     
    ?>
    {{ DiscussionBoard.errors[3257537].message }}
  • Profile picture of the author stma
    And your username is probably accountname_nameyouchose, your database is probably accountname_nameyouchose if you just aren't connecting.
    {{ DiscussionBoard.errors[3265566].message }}
  • Profile picture of the author webpro4hire
    Can you paste the error message you're getting?

    WP4H
    {{ DiscussionBoard.errors[3266521].message }}

Trending Topics