Jump to content

Recommended Posts

Hello users on phpfreaks!

I have recently started my journey to master PHP coding. I have started creating my own website that uses php but I have been receiving one error that I can't quite fix

"( ! ) Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp64\www\link.php on line 4"

I have been trying to fix this for quite a while now and I can't seem to get my link.php to work properly! If anyone can give me a fix it would be highly appreciated! My code will be shown below!

 

<?php
@include_once 'settings.php';
//session_start
$link = mysqli_connect($servername, $username, $password); //MySQL Host, Username, password
$db_selected = mysqli_select_db('database', $link); //MySql database
mysqli_query("SET NAMES utf8");


function fetchinfo($rowname, $tablename, $finder, $findervalue)
{
    if ($finder == "1")
        $result = mysqli_query("SELECT $rowname FROM $tablename");
    else
        $result = mysqli_query("SELECT $rowname FROM $tablename WHERE `$finder` =`$findervalue`");
    $row = mysqli_fetch_assoc($result);
    return $row[$rowname];
}


function secureoutput($string)
{
    $string = mysqli_real_escape_string($string);
    $string = htmlentities(strip_tags($string));
    $string = str_replace('>', '', $string);
    $string = str_replace('<', '', $string);
    $string = htmlspecialchars($string);
    return $string;
}


?>
 
Link to comment
https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/
Share on other sites

Have you tried using the PHP manual as part of your 'mastering' experience?  It would be helpful in showing you the proper syntax for the functions you want to use.
 

bool mysqli_select_db ( mysqli $link , string $dbname )

it's not possible to write code or convert old code you have found on the web, without using the php.net documentation.

 

next, the php mysqli extension is not the best choice to use. learn and use the php PDO extension instead. you should be using prepared queries to supply data to any sql query statement to protect against sql injection.

 

don't use @ error suppressors, ever. all they do is hide problems and make it harder to write code that works.

 

don't write and use functions like the fetchinfo() function. it is currently not secure and by having a function that selects a single column at a time, you will end up killing your database server with too many queries.

 

don't write and use functions like the secureoutput() function. this function is repeating what it is doing, using a msyqli function, which has nothing to do with output, and is just nonsense code. to secure data being output to a web page, just use htmlentities() with the appropriate flag values.

Edited by mac_gyver
  • Like 1

You should use mysqli_connect() to select the database. More information can be found here:

http://php.net/manual/en/function.mysqli-connect.php

 

Note: the documentation recommends that you only use mysqli_select_db() to change the default database. More information can be found here:

http://php.net/manual/en/mysqli.select-db.php

Edited by cyberRobot
removed the ginerjm quote since it was misleading and unnecessary...and I wanted to increase the confusion :-)

... the OP has mysqli_select_db () bass akwards.

 

I remember telling one guy on the forum that he had two parameters in the wrong order.

 

His reply was "What order should they be then?"

  • Like 3

I think you have mis-quoted me.  My post was to show the OP what the manual shows as the proper syntax and nothing more.

 

Yeah, sorry about that. I only "quoted" you to refer to the unnecessary function. As soon as I hit post, I realized that wasn't the best way...but was in too much of a rush to correct it.  :-[ I figured it would be better to ask for forgiveness.  :cry:  :happy-04:

 

Edit: I removed the quote since it was misleading and unnecessary.

 

 

@cyberRobot, the OP has mysqli_select_db () bass akwards.

 

Yep  :happy-04:

Edited by cyberRobot
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.