Mysqli version of mysql template

Here's the MySQL template in PHP:

$username="username"; $password="password"; $database="username-databaseName"; // Opens a connection to a mySQL server $connection=mysql_connect (localhost, $username, $password); if (!$connection) { die("Not connected : " . mysql_error()); } // Set the active mySQL database $db_selected = mysql_select_db($database, $connection); if (!$db_selected) { die ("Can\'t use db : " . mysql_error()); } // Search the rows in the markers table $query = some query $result = mysql_query($query); 

I tried replacing most of it with the mysqli template, and then pasting the query part below:

 //Database Information $db_host = "localhost"; //Host address (most likely localhost) $db_name = "username-databaseName"; //Name of Database $db_user = "username"; //Name of database user $db_pass = "password"; //Password for database user /* Create a new mysqli object with database connection parameters */ $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); GLOBAL $mysqli; if ($mysqli->connect_errno) { echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; exit(); } // Search the rows in the markers table $query = some query $result = mysql_query($query); 

However, I get this error message:

 Invalid query: No database selected 

What am I doing wrong?

+5
source share
1 answer

First of all, you call mysql_query , not mysqli_query , as you intended.

Secondly, since you are using an object-oriented form, you need to call mysqli_query as a method:

 $result = $mysqli->query($query); 
+2
source

Source: https://habr.com/ru/post/1205064/


All Articles