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
Related
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
I've looked through the "recommendations" based on the title I placed this as and the topics either weren't answered or didn't relate to my situation (from what I could see).
I've got a PHP HTML page that calls information from a database and inputs it into a table. I've got a a tag ready for a delete function but I just can't seem to get it right. I was hoping someone here would be able to help me out.
These are all the relevant pages.
connection.php
<?php
try{
$handler = new PDO('mysql:host=127.0.0.1;dbname=data', 'root', 'root');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo 'ERROR: ' . $e->getMessage();
}
?>
location1.php
<?php
include('connection.php');
$query = $handler->query('SELECT * FROM subsordered WHERE location ="location1"');
$delete = $handler->query('DELETE * FROM subsordered WHERE id = :id');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1 align="center">Location 1</h1>
<table align="center" border="1" height="10%" width="80%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Desc</th>
<th>Location</th>
<th></th>
</tr>
<?php while($row = $query->fetch()){ '<tr>
<td align="center">';echo $row[''],'</td>
<td align="center">';echo $row['id'],'</td>
<td align="center">';echo $row['name'],'</td>
<td align="center">';echo $row['desc'],'</td>
<td align="center">';echo $row['location'],'</td>
<td align="center">Delete</td>
</tr>';} ?>
</table>
When I try to keep the mysql delete call in the first php section in location1 it breaks the page, I'm pretty sure it has something to do with the fact that I am assigning 2 calls to 1 function but I other than making a brand new page jsut for a single delete call I don't know what else to do
Try:
<?php
include('connection.php');
$query = $handler->query('SELECT * FROM subsordered WHERE location ="location1"');
if (isset($_GET["id"])) {
$delete = $handler->exec('DELETE FROM subsordered WHERE id = \'' . $_GET["id"] . '\'');
}
?>
This should work. Anyway, i would consider changing the code in order to prevent SQL injection (Check this)
I'm not so familiar with PDO but from what I see this line has a problem:
$delete = $handler->query('DELETE * FROM subsordered WHERE id = :id');
It should be this I think:
$delete = $handler->query('DELETE FROM subsordered WHERE id = :id');
Have a look at the example here: http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-pdo/
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
I"m having trouble with my code hopefully someone can help.
I'm trying to call information using "php echo" to display information in table form and it works except for the links which doesn't recognize the $id. If I don't put it in the table form it works fine but it is not aesthetically appealing.
Any suggestions would be greatly appreciated!
<?php
session_start();
if(!isset($_SESSION['name'])){
header("location: ../index.php");
exit();
}
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("../scripts/connect.php");
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {
echo 'Do you really want to delete messages with ID of ' . $_GET['deleteid'] .'? Yes | No';
exit();
}
if (isset($_GET['yesdelete'])) {
// delete from database
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("DELETE FROM `mystore`.`messages` WHERE `messages`.`id` = '$id_to_delete' LIMIT 1") or die (mysql_error());
}
$messages = "";
$sql = mysql_query("SELECT * FROM messages ORDER BY msg_date DESC LIMIT 20");
$count = mysql_num_rows($sql);
if($count > 0){
while($row = mysql_fetch_array($sql)){
echo '<tr>';
echo '<td>'.$row['msg_name'].'</td>';
echo '<td>'.$row['msg_email'].'</td>';
echo '<td>'.$row['msg_subject'].'</td>';
echo '<td>'.$row['msg_date'].'</td>';
echo '<td>Reply</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
}else{
$messages = "<b>There are no messages in the database at this moment</b>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Messages</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style/forms.css" media="screen">
<link rel="stylesheet" href="style/main.css" media="screen">
</head>
<body>
<div id="main_wrapper">
<?php include_once("templates/tmp_header.php"); ?>
<?php include_once("templates/tmp_nav.php"); ?>
<section id="main_content">
<h2 class="page_title">Messages</h2>
<br/>
<table width="730" cellspacing="0" cellpadding="3" border="1">
<tr>
<td align="center" width="100">From</td>
<td align="center" width="300">Email</td>
<td align="center" width="300">Subject</td>
<td align="center" width="100">Date</td>
<td align="center" width="100">Actions</td
></tr>
<?php echo $messages; ?>
</table>
</section>
<?php include_once("templates/tmp_aside.php"); ?>
<?php include_once("templates/tmp_footer.php"); ?>
</div>
Please change
echo '<td>Delete</td>';
to
echo "<td><a href='admin_messages.php?deleteid=$id'>Delete</a></td>";
when trying to print out a variable the main string has to be wrapped in double quotes.
If you want to interpolate variables in PHP, you need to use double quotes. echo '$id' will literally print $id, whereas echo "$id" will print the value of the variable. However, I would recommend an alternative approach. Don't use PHP where it isn't needed. There's no need to use echo so much.
I would change the contents of your loop to this:
?>
<tr>
<td><?=$row['msg_name']?></td>
<td><?=$row['msg_email']?></td>
<td><?=$row['msg_subject']?></td>
<td><?=$row['msg_date']?></td>
<td>Reply</td>
<td>Delete</td>
</tr>
<?php
The <?=$id?> is shorthand for <?php echo $id?> and is supported by default in PHP versions >=5.4.0. You can also use it in previous versions if you enable short_open_tags.
As stated in the comments, you should really be using mysqli functions, as mysql functions are deprecated.
I'm a complete newbie when it comes to HTML, I've never needed to do any webpage stuff before so apologies if I'm being a simpleton.
I have a high score table for an iOS app, and I want to display it on the website for the app as well. The leaderboard is working fine in the app using PHP and mySQL, but when I try to access the database using PHP inside an HTML file it doesn't seem to pay any attention to the logic of the PHP (i.e. if I have an if/else statement, it will run both even cases).
Here is a simplified version of my code, if connection to the database is successful then the first column in the table header will say "success", otherwise it will say "failure".
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
}
else
{
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
}
?>
</table>
</body>
Can anyone see what has been done incorrectly here? I assumed that the if/else would work as normal, but instead I get both rows inserted into the table, "failure" and "success".
Edit:
I have also tried using echo ""; etc, but not sure which way is correct as they both give me the same error where it ignores the PHP.
Change your code to
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
?>
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
else
{
?>
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
?>
</table>
I think that you need to put your HTML tags in the echo command like this
echo '<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th> <th>Draws</th><th>Losses</th>
</tr>';
this should work
Check extension of your file. It need to be filename.php
You need a server to launch php code
You can use echo heredoc like here
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
echo <<<_END
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
_END
}
else
{
echo <<<_END
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
_END
}
?>
</table>
</body>
That seems to be working
Between <?php and ?> is just PHP code. It is treating you HTML as PHP.
Either
Use echo
put ?> before the HTML and <?php> after (i.e. stop and start PHP processing)
try this:
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
?>
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
else
{
?>
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php } ?>
</table>
</body>