Back

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

I had done a PHP page which is actually supposed to select two names from a database and displays them.

It just says:

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/tdoylex1/public_html/dorkhub/index.php on line 4

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/tdoylex1/public_html/dorkhub/index.php on line 8

My code is:

<?php mysqli_connect(localhost,tdoylex1_dork,dorkk,tdoylex1_dork);

$name1 = mysqli_query("SELECT name1 FROM users

ORDER BY RAND()

LIMIT 1");

$name2 = mysqli_query("SELECT name FROM users

ORDER BY RAND()

LIMIT 1");

?>

<title>DorkHub. The online name-rating website.</title>

<link rel="stylesheet" type="text/css" href="style.css">

<body bgcolor='EAEAEA'>

<center>

<div id='TITLE'>

    <h2>DorkHub. The online name-rating website.</h2>

</div>

    <p>

    <br>

    <h3><?php echo $name1; ?></h3><h4> against </h4><h3><?php echo $name1; ?></h3>

    <br><br>

    <h2 style='font-family:Arial, Helvetica, sans-serif;'>Who's sounds the dorkiest?</h2>

    <br><br>

    <div id='vote'> 

    <h3 id='done' style='margin-right: 10px'>VOTE FOR FIRST</h3><h3 id='done'>VOTE FOR LAST</h3>  

1 Answer

0 votes
by (12.7k points)

The problem is that you are not saving the mysqli connection. Modify your connect to:

$aVar = mysqli_connect('localhost','tdoylex1_dork','dorkk','tdoylex1_dork');

And then add this in your query:

$query1 = mysqli_query($aVar, "SELECT name1 FROM users

    ORDER BY RAND()

    LIMIT 1");

$aName1 = mysqli_fetch_assoc($query1);

$name1 = $aName1['name1'];

Also, you have to enclose your connections variables as strings as I had done above. This is the reason that's causing the error but you are using the function wrong, mysqli_query returns a query object but to get the data out of this you need to use something like mysqli_fetch_assoc to actually get the data out into a variable as I have above. 

Want to be a SQL expert? Come and join this SQL Certification by Intellipaat.

For more information visit :

Related questions

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

Browse Categories

...