-1

I've been trying to spot the error in my database connection , I read several answers suggesting to make sure of the password grant full access ... I checked everything the problem is still appearing ...

My question is ... I understand mysqli is in this case better (from what I read) so ... if I replace mt $dbcon with

$dbcon = mysqli_connect("$host","$username","$password","$db_name") ;

will that fix the problem and if it does, how do I get rid of the never ending mysqli_error expects exactly 1 parameter, 0 given ... error

My php is

    <?php
        $host="****" ;
        $username="****" ;
        $password="****" ;
        $db_name="****" ;
        $tbl_name="courses" ;
            $dbcon = mysql_connect("$host","$username","$password","$db_name") ;            

            if (!$dbcon) {
            die('error connecting to database'); }

            echo 'Courses successfully registerd , ' ;  

        // escape variables for security
        $studentid = ( $_GET['studentid']);
        $fname = $_POST["name"]; //echo $studentid;
    $query=mysql_query("select * from courses where studentid='".$studentid."' ") or die(mysql_error());
        $duplicate=mysql_num_rows($query);
       if($duplicate==0)
        {
          $query1=mysql_query("insert into user values('".$studentid."')")  or die(mysql_error());
        }
        else
        {
          echo'The ID you entered '.$studentid.' already registered, please wait 24 hours until you can register again';
        }

    // Get Cources
    $name = $_GET['ckb'];
    if(isset($_GET['ckb']))
    {
    foreach ($name as $courcess){
    $cc=$cc. $courcess.',';
    }
    }





    $sql="INSERT INTO courses (studentid, ckb, name)
    VALUES ('$studentid', '$cc', $fname)";

    if (!mysql_query($dbcon,$sql)) {
    die('Error: ' . mysqli_error($dbcon));
}
echo "  Thank you for using IME Virtual Registeration  ";   
        mysql_close($dbcon);
?>
Azizi
  • 151
  • 2
  • 15
  • 2
    You need to pass the database handle to the function, so you need `mysqli_error($dbcon)`. You'll also need to change all your mysql_ functions to use mysqli_ - they're not compatible. – andrewsi Sep 05 '14 at 22:22
  • I don't get it , how do I pass the database handle to the function ? and which function ? – Azizi Sep 05 '14 at 22:27
  • Your question is unclear. Your code uses `mysql_connect`, but then you ask about `mysqli_connect`. Are you converting your code from `mysql` to `mysqli`, and asking how to do it? Or just asking how to fix the problem in the `mysql` code? – Barmar Sep 05 '14 at 22:30
  • The basic problem with your code is that `mysql_connect()` doesn't take a `$db_name` parameter, you have to call `mysql_select_db()` after connecting. – Barmar Sep 05 '14 at 22:30
  • You can't mix `mysql_XXX` and `mysqli_XXX` functions, they're totally unrelated interfaces, except that they both connect to a MySQL database. – Barmar Sep 05 '14 at 22:31
  • @Barmar, I already converted from mysqli to mysql in a previous attempt to fix a previous error , (the one mentioned above in bold) ...after doing that I got this new error No database selected and I got this idea from similar questions that mysqli should be used in my case ... I don't know if there is a solution I can do in the mysql code without plugging mysqli back in... – Azizi Sep 05 '14 at 22:33
  • @Barmar exactly , but its not connecting ... even though I'm using mysql only..why is that ? – Azizi Sep 05 '14 at 22:34
  • Why do you think it's not connecting? If you get the error "No database selected", it means the connection succeeded, but you never called `mysql_select_db()`. – Barmar Sep 05 '14 at 22:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60732/discussion-between-azizi-and-barmar). – Azizi Sep 05 '14 at 22:48

2 Answers2

1

Although we are mostly using a PDO interface now, here is the code also use to connect to the database with some simple command line scripts:

$sql_link=mysql_connect ('host','user','password') or die('Cannot connect to the database because: ' . mysql_error());
if (!$sql_link) {
    echo('Could not connect to database : ' . mysql_error());
    exit;
}
mysql_select_db('database_name') or die(mysql_error());

Hope that helps!

Keith Grey
  • 151
  • 7
1

The problem is in Your PHP on line 7. mysql_connect() has those parameters:

mysql_connect(string $server, string $username, string $password, @optional bool $new_link = false, @optional int $client_flags = 0)

You are giving the fourth parameter $db_name =MISTAKE (fourth parameter must be boolean as you can see.) There's a function mysql_select_db() to tell PHP which database are you using. So here's a rework of your 7th line:

$dbcon = mysql_connect("$host","$username","$password");
mysql_select_db("$db_name");

but I recommend you to use mysqli or PDO ->they are inbuilt PHP libraries, so you don't have to download anything and what's more important they are easy to use. Good Luck :)

  • Table '1662822_db1.user' doesn't exist , now its mistaking my db_name with my table name "courses" .. – Azizi Sep 05 '14 at 22:47