Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)
recategorized by

I am getting this below error in my code and I am trying to solve it, but couldn't. please check my code below:

<?php

session_start();

include_once"connect_to_mysql.php";

$db_host = "localhost";

// Place the username for the MySQL database here

$db_username = "root"; 

// Place the password for the MySQL database here

$db_pass = "****"; 

// Place the name for the MySQL database here

$db_name = "mrmagicadam";

// Run the actual connection here 

$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");

mysql_select_db("mrmagicadam") or die ("no database");        

$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";

$query=mysqli_query($myConnection, $sqlCommand) or die(mysql_error());

$menuDisplay="";

while($row=mysql_fetch_array($query)) {

    $pid=$row["id"];

    $linklabel=$row["linklabel"];

$menuDisplay='<a href="index.php?pid=' .$pid . '">' .$linklabel. '</a><br/>';

}

mysqli_free_result($query);

?>

and below is the error:

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\limitless\connect_to_mysql.php on line 17

 Where I am doing wrong?

1 Answer

0 votes
by (12.7k points)

You are getting confusing and using the mysqli and mysql extensions, which will not work.

You need to be using:

$myConnection= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); 

mysqli_select_db($myConnection, "mrmagicadam") or die ("no database");   

mysqli has substantial improvements over the original mysql extension, so it is recommended to you to use the mysqli

Interested in SQL ? Check out this SQL Certification by Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jan 3, 2021 in SQL by Appu (6.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...