This question already has answers here:
PHP Warning: mysqli_fetch_assoc() expects exactly 1 parameter, 3 given in
(2 answers)
Closed 6 years ago.
I am trying to populate a table with data from the database. I have googled extensively but nothing seems to work
I always keep getting an empty table with following error:
Warning: mysqli_fetch_assoc() expects exactly 1 parameter, 2 given in
The thing is if I only give it 1 parameter then the page does gets stuck on the spinning loader.
Part of my html code:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
session_start();
include 'config.php';
$query_string = "SELECT * FROM tbl_User";
$query = mysqli_query($con, $query_string);
?><!DOCTYPE html>
<html lang="en">
<head>
....
</head>
<body>
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Profile Picture</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($con, $query)){
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['email']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
Code for config.php:
<?php
$con = mysqli_connect("localhost", "root", "root", "chall") or die("Error " . mysqli_error($con));
?>
How to solve this?
Use
while($row = mysqli_fetch_array($query)){
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['email']."</td>";
echo "</tr>";
}
Dont pass $con inside fetch_array/fetch_assoc
Related
I am getting the following validation error, it claims I have "misplaced non-space characters inside the table" I have tried everything and for the life of me cannot fix this error. The error is the following
Here is the PHP/HTML being used for
<?php
$title = "Alter Records";
include('includes/head.php');
include('includes/nav.php');
ini_set('display_errors', 0);
require_once('config.php');
require_once('db_class.php');
$connection = new dbController(HOST,USER,PASS,DB);
$sql = "select id,name, image,location from location";
$results = $connection->getAllRecords($sql);
//var_dump($results);
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Location</th>
<th>Image</th>
<th colspan="2">Make Changes</th>
</tr>;
<?php
foreach ($results as $row){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['location']}</td>";
echo "<td><img class='thumb' src='{$row['image']}' alt='{$row['name']}'> </td>";
echo "<td><a href=''>Update</a></td>";
echo "<td><a href='delete_record.php?id={$row['id']}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
include('includes/footer.php')
?>
Here is the code inside validator.w3.org
↩
<!DOCTYPE html>↩
<html lang="en">↩
<head>↩
<meta charset="utf-8">↩
<title>Alter Records</title>↩
<link href="https://fonts.googleapis.com/css2?family=Quicksand&display=swap" rel="stylesheet">↩
<link href="https://fonts.googleapis.com/css2?family=Bellota+Text:ital,wght#1,300;1,400&display=swap" rel="stylesheet">↩
<link rel="stylesheet" href="css/style.css">↩
</head>↩
<body>↩
<header><h1> TOURISM VICTORIA</h1></header>↩
<nav>↩
<ul class="flex-nav">↩
<li>Home</li>↩
<li>Add</li>↩
<li>Alter</li>↩
<li>↩
<form class="search-form" action="search.php" method="get">↩
<input type="text" name="search">↩
<button type="submit">Search</button>↩
</form>↩
</li>↩
</ul>↩
</nav>↩
<table>↩
<tr>↩
<th>Id</th>↩
<th>Name</th>↩
<th>Location</th>↩
<th>Image</th>↩
<th colspan="2">Make Changes</th>↩
</tr>;↩
<tr><td>1</td><td>Melbourne ShowGrounds</td><td>Royal Melbourne Showgrounds. Ascot Vale, Victoria, Austra
Any assistance with this would be greatly appreciated. If I am still unclear, please ask for further clarification.
just seeing your last include will also result in an error, you will need to end with ;
I want to get data from my sql database into a table with html.
I have writed 1st part of code ended with ?>
and started with html table and end with html
My code getting only Empty table without getting data from my Database, here the Code, i dont know how to get data with that
<td>
<?php echo $result['nazwiskoimie'] ?>
</td>
Here's the full code :
<?php
$con=mysqli_connect("localhost","root","","listap");
if (mysqli_connect_errno()) {
echo "Blad przy polaczeniu z baza: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM 'lista'");
?>
<html>
<table border='1'>
<tr>
</tr>
<tr>
<td> <?php echo $result['nazwiskoimie'] ?> </td>
//in this part have problem to get my data from database
</html>
Here's also a screenshot of a result: Final After a Save my file
Please try this:
$result = mysqli_query($con,"SELECT * FROM lista");;
?>
<html>
<table border='1'>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['nazwiskoimie']?></td>
</tr>
<?php
}
} ?>
</table>
</html>
I've written up a pretty simple query to output all the data from the Artist table and output them in the established tables. I've double checked the database and all the spelling is correct, but I'm not getting any data being outputted for some reason.
Connector Code
<?php
$conn = mysqli_connect("localhost", "b4014107", "Windows1", "b4014107_db2") or die (mysqli_connect_error());
?>
Main Code
!DOCTYPE HTML>
<html>
<head>
<title>View Artist Table</title>
</head>
<body>
<?php
//Includes speicifed details in order to connect to MySQL
include('ConnectorCode.php');
//mysql_query command is used to select data from Artist table
$result = mysqli_query("SELECT * FROM tbl_Artist");
echo "<table border='1'>";
echo "<tr> <th>Artist ID</th> <th>Artist Name</th> </tr>";
//Results are looped and then displayed in tables
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row ['Artist_id'] . "</td>";
echo "<td>" . $row ['Artist_Name'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Connection is closed
mysqli_close($conn);
?>
<p>Add a new Artist</p>
<p>Edit a current Artist</p>
</body>
</html>
What am I doing wrong?
I think this is your problem:
Use: $result->fetch_assoc()
Instead of: mysqli_fetch_array($result)
I found the solution! I just need to add $conn within the mysqli_query.
$result = mysqli_query($conn, "SELECT * FROM tbl_Artist");
I want to retrieve data from MySql table. I am using Xampp, phpMyAdmin etc... I followed this tutorial step by step: https://www.youtube.com/watch?v=IHdd02IK2Jg
But I get this error:
Notice: Undefined variable: records in C:\XAMPP\htdocs\display_prsc.php on line 29
And a warning:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\XAMPP\htdocs\display_prsc.php on line 29
It just displays the column names when i run it.
<? php
//make connection
mysql_connect('localhost','root','');
//select_db
mysql_select_db('mynewdb');
$sql="SELECT * FROM new";
$records=mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
<tr>
<?php
while ($new=mysql_fetch_assoc($records)){
echo"<tr>";
echo"<td>".$new['py']."</td>";
echo"<td>".$new['pcrit']."</td>";
echo"<td>".$new['psco']."</td>";
echo"</tr>";
}
?>
</table>
</body>
</head>
</html>
You have a few errors in your code:
1. Wrong php opening tag
You have to remove the space in your php opening tag, e.g.
<?php
2. Weird html
Almost your entire html is wrong! I would recommend you to read a book or watch a basic html tutorial
So your entire code should look something like this:
<?php
//make connection
mysql_connect('localhost', 'root', '');
//select_db
mysql_select_db('mynewdb');
$sql = "SELECT * FROM new";
$records = mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
</head> <!-- head tag closed here -->
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
</tr> <!-- forgot / to close the tag -->
<?php
while ($new = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" . $new['py'] . "</td>";
echo "<td>" . $new['pcrit'] . "</td>";
echo "<td>" . $new['psco'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
Side Notes:
1. What can be improved?
I would do some basic error checking e.g. if the connection failed and so on
Use mysqli_* with prepared statements, or PDO with prepared statements, they are much safer! (We live in 2015)
(Also if the guy in your tutorial uses mysql_*, change the tutorial!)
So that you don't only have your old code working, here your improved code:
<?php
//Database stuff
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "mynewdb";
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT * FROM new");
$stmt->execute();
//Close Connection
$dbh = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
<html>
<head>
<title>Program Scores Table</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
</tr>
<?php
while ($new = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>";
echo "<td>" . $new['py'] . "</td>";
echo "<td>" . $new['pcrit'] . "</td>";
echo "<td>" . $new['psco'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
2. Coding side notes
Add error reporting to the top of your file(s) which will help find errors.
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
Error reporting should only be done in staging, and never production.
Note the space in your first php tag.
You have <? php , it should be <?php
And since it seems you are starting to learn, avoid learning things deprecated. Don't use MySQL, use PDO or Mysqli.
try
<?php
//make connection
$con= mysql_connect('localhost','root','');
//select_db
$select= mysql_select_db('mynewdb',$con);
$sql="SELECT * FROM new";
$records=mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
<tr>
<?php
while ($new=mysql_fetch_array($records)){
echo"<tr>";
echo"<td>".$new['py']."</td>";
echo"<td>".$new['pcrit']."</td>";
echo"<td>".$new['psco']."</td>";
echo"</tr>";
}
?>
</table>
</body>
</head>
</html>
I recommend you using Prepared statements for MySQL instead of clean MySQL, it's not secure at all, and will give you problems later on.
A lille guide here.
http://www.w3schools.com/pHp/php_mysql_prepared_statements.asp
There are multiple errors in your code. But for the error, it is due to mysql_query() returns FALSE at line:
$records=mysql_query($sql);
Then, the line while ($new=mysql_fetch_assoc($records)){ cannot get the value of $records, thus produce the errors.
Other errors are:
HTML
missing DOCTYPE
missing meta charset
a missing </tr>
a misplaced </head>
be modern, use CSS to style your HTML instead of using HTML tag attributes
PHP
didn't check the return value of functions before using them
use of deprecated mysql_* functions; use PDO / MySQLi instead
extra space at <? php
Im very new to PHP and was trying to get record information to display on a php page using the MAMP server environment.
The attributes are productID, productName, productDescription and productPrice.
I've been able to get really basic PHP to run fine but every time I open this php file, nothing displays, I was wandering if it might be to do with the location I placed the php file. It is currently in htdocs. would appreciate any help.
Thanks
<?php
//connection to db
mysql_connect('localhost', 'root', 'root');
//choosing db
mysql_select_db(primrose);
$sql= "SELECT * FROM products";
$records mysql_query(sql);
?>
<html>
<head>
<title>Product Data</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
//loops over each record and for each a new record is set into the variable products
while(products=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$products['productName']."</td>";
echo "<td>".$products['productDescription']."</td>";
echo "</tr>";
} //end while
?>
</table>
</body>
</html>
This is because (I think) this line is bad:
mysql_select_db(primrose);
Add quotes around the name of db:
mysql_select_db("primrose");
Also this line:
$records mysql_query(sql);
change to
$records = mysql_query($sql);
and this:
while(products=mysql_fetch_assoc($records)){
to
while($products=mysql_fetch_assoc($records)){
NOTE 1:
Do not use mysql functions since, they are deprecatid. Use mysqli or PDO instead.
NOTE 2:
Let's turn on your error reporting with these two rows in the top of your PHP file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
You have a lot of syntax errors. Let's use an IDE to identify them.
So your final code like this:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//connection to db
mysql_connect('localhost', 'root', 'root');
//choosing db
mysql_select_db("primrose");
$sql= "SELECT * FROM products";
$records = mysql_query($sql);
?>
<html>
<head>
<title>Product Data</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
//loops over each record and for each a new record is set into the variable products
while($products=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$products['productName']."</td>";
echo "<td>".$products['productDescription']."</td>";
echo "</tr>";
} //end while
?>
</table>
</body>
</html>
Try setting error_reporting(E_ALL); to on just after to open'ed PHP.
If not, make sure that error reporting is turned on in your php.ini
http://php.net/manual/en/errorfunc.configuration.php