Reputation: 41
I've installed XAMPP and have been attempting to interrogate my database using PHP although I keep getting the same error.
<?php
$servername = "localhost";
$username = "root";
$password = "secret";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Perform queries
$result = mysqli_query($conn,
"use edgeserver; call ShowAll") or die("Query fail: " . mysqli_error());
//loop the result set
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
$conn->close();
?>
However this presents me with the following error:
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\amit\ShowAll.php on line 16
Query fail:
Upvotes: 0
Views: 826
Reputation: 41
Thanks, it ended up being a combination of what you both said:
<?php
// Create connection
$conn = new mysqli('localhost', 'root', 'secret', 'edgeserver');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Perform queries
$result = mysqli_query($conn,
"call ShowAll") or die("Query fail: " . mysqli_error($conn));
//loop the result set
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
$conn->close();
Great response, hopefully this will be the base to me developing some good code.
Upvotes: 1
Reputation: 123
I think the problem is from use edgeserver; call ShowAll
.
use edgeserver
, where edgeserver is the database that you want to use.
call ShowAll
- what you try to accomplish with this?
Your new mysqli must be like this:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
source: http://php.net/manual/ro/mysqli.construct.php
And on mysqli_query you must do the queries like "Select * from table".
Upvotes: 0
Reputation: 11830
Mysql_error requires db connection to be passed. Try below:
mysqli_error($conn);
Upvotes: 1