I'm trying to make it so that when a button is clicked on my main page, a php script takes action and fetches the information from the SQL tables, and displays it in a HTML/CSS table.
Here's the code of my main page -
<form id="myForm" action="select.php" method="post">
<button type="submit" class="btn btn-info" >
<span class="glyphicon glyphicon-tint"></span> View
</button>
<br /> <span class="badge alert-info"> Find out what is currently in the database. </span><br />
</form>
<br />
<br />
And here's what I currently have in my select.php -
<?php
/*** mysql hostname ***/
$hostname = '192.xx.xxx.xx';
/*** mysql username ***/
$username = 'Mitchyl';
/*** mysql password ***/
$password = 'root1323';
/*** database name ***/
$dbname = 'test';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM increment";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
I just don't know how to take the data from the SQL query and put it inside a HTML table.
Any suggestions would be great!
Thanks!
Try this
<?php
$hostname = '192.xx.xxx.xx';
$username = 'Mitchyl';
$password = 'root1323';
$dbname = 'test';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$sql = $dbh->prepare("SELECT * FROM increment");
if($sql->execute()) {
$sql->setFetchMode(PDO::FETCH_ASSOC);
}
}
catch(Exception $error) {
echo '<p>', $error->getMessage(), '</p>';
}
?>
<div id="content">
<table>
<?php while($row = $sql->fetch()) { ?>
<tr>
<td><?php echo $row['column1_name']; ?></td>
<td><?php echo $row['column2_name']; ?></td>
<td><?php echo $row['column3_name']; ?></td>
...etc...
</tr>
<?php } ?>
</table>
</div>
Let's say your sql returns an array named $data and its format is sth like $data = [['name' => 'name1'], ['name' => 'name2'], ...];.
//First declare your data array
$data = [];
// Then execute the query
$result = $mysqli->query($sql)
// Then read the results and create your $data array
while($row = $result->fetch_array())
{
$data[] = $row;
}
Now that you have your data check if it is empty or not and then use foreach to present the results.
<?php if(empty($data)): ?>
<h1>No results were found!</h1>
<?php else: ?>
<h1><?= count($data) ?> results were found!</h1>
<table class="table">
<thead>
<th>#</th>
<th>Name</th>
</thead>
<tbody>
<?php foreach ($data as $key => $value): ?>
<tr>
<td><?= ++$key ?></td>
<td><?= $value['name'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
Of course you can add any class you like to the table apart from the default one that bootstrap uses (.table).
Related
I've been trying to find an easy way for this. A search (dropdown menu) of all tables in mysql, and show their content when I click the table I want to show on the page. Instead of showing just every table on the page I thought it can be easier? Any help would be appreciated! My code so far:
<?php
$host = "localhost";
$user = "heijsdb_user";
$pass = "maus";
$db_name = "heijsdb";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "borsten HFP controle";
$result = mysqli_query($connection,"SELECT * FROM borstenHFPcontrole");
$all_property = array(); //declare an array for saving property
//showing property
echo '<table class="data-table w3-table-all" border="2px">
<tr class="data-heading">'; //initialize table tag
while ($property = mysqli_fetch_field($result)) {
echo '<td>' . $property->name . '</td>'; //get field name for header
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($all_property as $item) {
echo '<td>' . $row[$item] . '</td>'; //get items using property value
}
echo '</tr>';
}
echo "</table>";
////////////////////////////////////////////////////////////////////////////////////////////////////////////
This is pretty much the idea, you can play from here and adapt it to your solution. Sorry I used my way, I prefer PHP template style when embedding in HTML. ;)
$host = "localhost";
$user = "heijsdb_user";
$pass = "maus";
$db_name = "heijsdb";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
//check if the form was submitted
$table = filter_input(INPUT_POST, 'table', FILTER_SANITIZE_STRING);
?>
<html>
<head>
<title>showing table content on user action</title>
</head>
<body>
<div>
<form id="form-menu" method="post">
<label for="select-menu">Choose a table</label>
<select id="select-menu" name="table">
<option></option>
<?php
$result = mysqli_query($connection,"SELECT table_name FROM information_schema.tables where table_schema='test'"); // <-- the table_schema field here is your database name, change 'test' for yours
while ($row = mysqli_fetch_array($result)) : $selected = $row['table_name'] == $table ? 'selected' : ''; ?>
<option value="<?php echo $row['table_name'] ; ?>" <?php echo $selected; ?>><?php echo $row['table_name'] ; ?></option>
<?php endwhile; ?>
</select>
</form>
<hr>
<div>
<?php if (empty($table)) : ?>
<h3>Please select a table to show its content</h3>
<?php else : ?>
<h3>Content for the table `<?php echo $table; ?>`</h3>
<?php
$result = mysqli_query($connection,"SELECT * FROM `{$table}`");
$all_property = []; //declare an array for saving property
?>
<!-- showing property -->
<table class="data-table w3-table-all" border="2px">
<tr class="data-heading"> <!-- initialize table tag -->
<?php while ($property = mysqli_fetch_field($result)) : ?>
<td><?php echo $property->name; ?></td> <!-- get field name for header -->
<?php $all_property[] = $property->name; //save those to array ?>
<?php endwhile; ?>
</tr> <!-- end tr tag -->
<!-- showing all data -->
<?php while ($row = mysqli_fetch_array($result)) : ?>
<tr>
<?php foreach ($all_property as $item) : ?>
<td><?php echo $row[$item]; ?></td> <!-- get items using property value -->
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
</div>
</div>
<script>
document.getElementById('select-menu').addEventListener('change', function() {
document.getElementById('form-menu').submit();
});
</script>
</body>
</html>
Handy links:
- Get table names using SELECT statement in MySQL
- Examples of how to do query, style, dom, ajax, event etc like jQuery with plain javascript.
Hope this helps :)
Here's a screenshot of the page that I want to put a pagination Below is my code and I want to create a simple pagination. I tried some examples available in this site but unfortunately it doesn't work for me or I might have missed something in the code.
<?php
session_start();
$server = 'localhost';
$user = 'root';
$pass = 'root';
$connect = mysql_connect($server,$user,$pass)
or die(mysql_error());
$selectdb = mysql_select_db('des')
or die(mysql_error());
?>
<form method="post" name="action_form" action="admin2.php">
<div id="gallery1" class="lbGallery">
<table class="table" width="100%" cellpadding="10">
<tbody>
<?php
$allRecords = mysql_query('select * from cms ORDER BY id DESC limit 4');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr><ul>
<td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
<td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
</ul>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</form>
Try this. What is does is:
create a contant $amount en a variable $offset
Create a link (next) with which sends the value of $offset back to the script
Catch the value in $_GET['offset'];
Add the value of $amount to $offset to create a new offset.
Run MySQL statement again with the new values for LIMIT
I didn't actualy test this code for typo's, but you'll get the general idea. Hope this is of any help.
(Best to use the new mysqli statement by te way).
<?php
$amount = 4;
if (!empty($_GET['offset']))
{
$offset = $_GET['offset'] + $amount;
}
else
{
$offset = 1;
}
$allRecords = mysql_query('select * from cms ORDER BY id DESC limit $amount,$offset');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr><ul>
<td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
<td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
</ul>
</tr>
<tr>
<td colspan="2">
Next
</td>
</tr>
<?php
}
}
?>
I am attempting to create a discussion board, and am trying to display the number of topics and number of total posts in each forum. I believe the issue lies somewhere in my WHERE clause, as that was causing me issues before on a previous issue.
I know that rowCount() shouldn't be relied on, and in my case doesn't work anyway, mainly due to the fact that it doesn't work with SELECT statements.
I have looked into both SELECT count(*) statements as well as doing $row = $stmt->fetchAll(); $numrows = count($row) However, I didn't get either of them to work.
<?php
/* CATEGORIES */
/* SELECTS ALL OF THE CATEGORIES OF THE BOARD */
$query = "SELECT * FROM bkg_categories";
try {
$stmt = $db->prepare($query);
$result = $stmt->execute();
} catch(PDOException $e) {
$error[] = "An error has occured. Please try again later.";
}
$categories = $stmt->fetchAll();
/* FORUMS */
/* SELECT ALL OF THE FORUMS IN EACH CATEGORY FOR THE BOARD */
foreach($categories as $category) {
$catid = $category['category_id'];
$query = "SELECT * FROM bkg_forums WHERE category_id = :catid";
$query_params = array(':catid' => $catid);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
} catch(PDOException $e) {
$error[] = "An error has occured. Please try again later.";
}
$forums[$catid] = $stmt->fetchAll();
foreach($forums[$category['category_id']] as $forum) {
$query = "SELECT * FROM bkg_topics where forum_id = :forumid";
$query_params = array(':forumid' => $forum['forum_id']);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
} catch(PDOException $e) {
$error[] = "An error has occured. Please try again later.";
}
$rows3['forumid'] = $stmt->fetchAll();
$rowCount = count($rows3['forumid']);
echo $rowCount;
var_dump($rowCount);
}
}
/* END QUERIES */
?>
These are all of my select queries that I am using. In that last foreach loop in my PHP select queries, you can see what I was playing around with, the var_dump() and the echo works, when it's not inside any foreach loop except for the inital parent one, but once I move it down into my table, it doesn't seem to work.
<?php foreach($categories as $category): ?>
<table class="forumtable">
<tr>
<td colspan="2"><strong><?php echo $category['category_name']; ?></strong></td>
<td><strong>Lastest Post</strong></td>
<td><strong>Topics</strong></td>
<td><strong>Posts</strong></td>
</tr>
<?php foreach($forums[$category['category_id']] as $forum): ?>
<tr>
<td width="5%" class="forum"></td>
<td class="forum no-padding">
<?php echo $forum['forum_name']; ?>
<div class="description"><?php echo $forum['forum_desc']; ?></div>
</td>
<td width="15%" class="forum content">
<?php if($forum['forum_last_post_topic_id'] == 0): ?>
<div>No posts...</div>
<?php else: ?>
<?php if($forum['forum_last_post_id'] == 0): ?>
<?php echo substr($forum['forum_last_post_title'], 0, 10); ?>
<?php else: ?>
<?php echo substr($forum['forum_last_post_title'], 0, 10); ?>
<?php endif; ?>
<?php endif; ?>
</td>
<td width="5%" class="forum content">
<?php
echo $forum['forum_topics']; ?></td>
<td width="5%" class="forum content"><?php
echo $forum['forum_posts']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
I was able to finally get the select(*) to work.
$db->query('SELECT COUNT(*) FROM users')->fetchColumn(); was what I used to get what I needed to work.
I am not looking for alternating rows... Rather, I have two tables A and B and Field1 is present in both. I want to present TableA and everywhere that the field1 matches the field1 in TableB, I would like to highlight the row. How can I do this?
I know how to do this in SQL, but the question is how to present this in the browser. I think I must use javascript. Any advice?
Here is the PHP Script that generates Table A. I would like to change the background of rows in this output where the aif_id is also present in Table B
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif ORDER BY aif_id DESC");
$result->setFetchMode(PDO::FETCH_ASSOC);
echo "<table id=\"all_aifs\">";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";
foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$dbh = NULL;
?>
PHP Script for TableB
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining LIMIT 0,50");
$result->setFetchMode(PDO::FETCH_ASSOC);
echo "<table>";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";
foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$dbh = NULL;
?>
UPDATED CODE
<?php
require_once 'config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining");
$result->setFetchMode(PDO::FETCH_ASSOC);
$match = $dbh->query("SELECT a_aif.aif_id FROM a_aif INNER JOIN a_aif_remaining WHERE a_aif_remaining.aif_id = a_aif.aif_id");?>
<?php if ( !empty($result) ) : ?>
<table id="all_aifs">
<tr>
<th><b>Document ID</b></th>
<th><b>Pubco Name</b></th>
<th><b>Filing Date</b></th>
<th><b>PDF</b></th>
</tr>
<?php foreach($result as $index => $row) : ?>
<tr>
<?= $row['aif_id'] == true ? ' class="match"' : '' ?>
<td><?php echo $row[fee_source_id]; ?></td>
<td><?php echo $row[company_name_per_sedar]; ?></td>
<td><?php echo $row[document_filing_date]; ?></td>
<td>Placeholder</td>
</tr>
<?php endforeach; ?>
</table>
<br>
<?php endif;
$dbh = NULL;
?>
Perform your check in SQL (since you know how to do so).
In PHP use the result of your check to add an (additional) class-attribute to the HTML element that needs the highlight (just like 'alternating rows' do):
<?php
$query = ... // perform your SQL query
// TODO Open HTML table
// TODO Loop though results:
$row = ... // fetch you row here
?>
<tr <?= $row['my_check'] == true ? ' class="match"' : '' ?>>
<td><?= $row['field'] ?></td>
</tr>
<?php
// TODO close HTML table
This (incomplete) example uses the SQL check (named my_check) and adds the match class when required. In CSS you can use this match class to apply highlighting:
.match {
background-color: red;
}
First of all, if you are going to keep your code maintainable and clean as well, then you should avoid printing HTML TAGS.
The way not to go:
echo "<table>";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
The way to go:
<?php
require_once 'config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining LIMIT 0,50");
$result->setFetchMode(PDO::FETCH_ASSOC);
?>
<?php if ( !empty($result) ) : ?>
<table>
<tr>
<th><b>Document ID</b></th>
<th><b>Pubco Name</b></th>
<th><b>Filing Date</b></th>
<th><b>PDF</b></th>
</tr>
<?php foreach($result as $index => $row) : ?>
<tr>
<td><?php echo $row[fee_source_id]; ?></td>
<td><?php echo $row[company_name_per_sedar]; ?></td>
<td><?php echo $row[document_filing_date]; ?></td>
<td>Placeholder</td>
</tr>
<?php endforeach; ?>
</table>
<br />
<?php endif; ?>
Conclusion:
1) The code now looks more readable and clean as well
2) You separated PHP from HTML
3) You can easily determine a problem once it occurs (like, parse error at .., unexcepted ';' ...
Back to your question
So how can you highlight a row you need? Well it's easy, just use if/else in a foreach loop, like
<?php foreach($result as $k => $v): ?>
<?php if ( $k == 'some_value_you_expect_to_be_highlighted' ) : ?>
<tr>... should be highlighted ...</tr>
<?php else : ?>
<tr>.... a regular row</tr>
<?php endif; ?>
<?php endforeach; ?>
You should simply add a class to the rows where the data matches, and set a background color in the associated CSS.
<table> <tr class="matching"> ... </tr>
How to write the appropriate CSS is beyond the scope of this answer. ;-)
Whether that class is added by your web server's PHP code, or by some JavaScript in the client, depends on the design of your Web application.
No, you don't 'have' to use JavaScript. You can use PHP.
When generating your rows, check whether the condition is met and put your 'highlighted' class into the row definition.
Pseudocode:
<?php
if(Condition) echo "<tr class='highlighted'>";
else echo "<tr>";
echo "<td>CellStuff</td>";
echo "</tr>";
?>
I'm not sure if I could understand your problem correct but I dont think you need JS.
You can calculate if a cell/row should be highlighted in PHP and set a css class, also in PHP.
I have built a PDO connection and query, now I need it to be safe from SQL Injection
Here is my code
User Input
<?php $search=$_GET["Search"];?>
SQL that querys DB
<?php
// Issue the query
$Recordset1 = $dbh->query("SELECT * FROM catelogue
WHERE catelogue.TITLE LIKE '$search'");
$Recordset1->setFetchMode(PDO::FETCH_ASSOC);
?>
Fetch The rows
<?php $row_Recordset1 = $Recordset1-> fetch(); ?>
After this there is a table with a do-while loop to display everything that was returned
How do I make a prepared statement for my query?
Thanks
EDIT:
Ok That last bit of code that DavdRew posted helped to get my search working again. Now I have 2 new problems.
Problem 1: After doing a few querys I get this message
mysql_pconnect() [function.mysql-pconnect]: MySQL server has gone away
It still shows the rest of my page with what I searched for. How is this fixed?
Then Problem 2:
With every search the first record returned is empty, a blank record. It never did this before, Why is it doing it now?
Many Thanks DavdRew
EDIT: ADDED MORE CODE
THIS IS THE ENTIRE PDO CODE
<?php
$hostname_EchosPDO = "localhost";
$username_EchosPDO = "echos";
$password_EchosPDO = "echos";
$database_EchosPDO = "full catelogue";
$connStr = 'mysql:host=localhost;dbname=full catelogue';
try {
$dbh = new PDO($connStr, $username_EchosPDO, $password_EchosPDO);
/*** echo a message saying we have connected ***/
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
<?php
$q = $dbh->prepare("SELECT * FROM catelogue WHERE catelogue.TITLE LIKE ?"); // prepare statement
if ($q->execute(array('sharpay%'))) // execute wirh passed params array($search)
{
$row_Recordset1 = $q->fetchAll(PDO::FETCH_ASSOC); // store fetched result into $rows;
}
else
{
$error = $dbh->errorInfo();
throw new Exception("SQLSTATE: {$error[0]}. {$error[1]} {$error[2]}");
}
?>
<?php /* $row_Recordset1 = $Recordset1-> fetch(); */ ?>
<?php do { ?>
<table width="800" border="0">
<tr>
<form action="/Echos Online/Detail.php" method="get"><input value='<?php echo $row_Recordset1['CAT NO.']; ?>' name="detail" type="hidden"/><td width='100' rowspan='4'><input type="image" img src="<?php echo $row_Recordset1['IMAGE PATH']; ?>" width="<?php if ($row_Recordset1['FORMAT']=='DVD') {echo "70";} else if ($row_Recordset1['FORMAT']=='DVD+CD') {echo "70";} else if($row_Recordset1['FORMAT']=='BLURAY+CD') {echo "81";}else if($row_Recordset1['FORMAT']=='BLURAY+DVD') {echo "81";} else if($row_Recordset1['FORMAT']=='BLURAY') {echo "81";} else {echo "100";} ?>" height="100"></td></form>
<td width="100">Artist:</td>
<td><?php echo $row_Recordset1['ARTIST']; ?></td>
</tr>
<tr>
<td width="100">Title</td>
<td><?php echo $row_Recordset1['TITLE']; ?></td>
</tr>
<tr>
<td width="100">Format</td>
<td><?php echo $row_Recordset1['FORMAT']; ?></td>
</tr>
<tr>
<td width="100">Cat. No.</td>
<td><?php echo $row_Recordset1['CAT NO.']; ?></td>
</tr>
<hr background-color="#e4a566" color="#e4a566"; width="100%"/>
<?php } while ($row_Recordset1 = $q-> fetch()); ?>
</table>
Honestly now I'm at the piont of going back to preg_replace and mysql_real_escape_string
Thanks for the help
Like this:
$q = $this->pdo->prepare($query);
$data = array();
if ($q->execute($params)) // params is the array of values for each '?' in your prepared statement.
$data = $q->fetchAll($fetch);
else
{
$error = $this->pdo->errorInfo();
throw new \Exception("SQLSTATE: {$error[0]}. {$error[1]} {$error[2]}");
}
return $data;
Keep in mind that WHERE IN works bad, you need to put as much ? into you imploded by , and wrapped with IN ( from left and ) right.
Example:
$statement = $pdo->prepare('SELECT * FROM my_table AS m WHERE m.id = ?');
if ($statement->execute(array(24))) // here you can pass values too.
$data = $q->fetchAll(\PDO::FETCH_ASSOC);
else
exit('Shit happens');
This search for record with id = 24;
For your code, that would be:
$q = $dbh->prepare("SELECT * FROM catelogue WHERE catelogue.TITLE LIKE ?"); // prepare statement
if ($q->execute(array($search))) // execute wirh passed params array($search)
{
$rows = $q->fetchAll(PDO::FETCH_ASSOC); // store fetched result into $rows;
}
else
{
$error = $dbh->errorInfo();
throw new Exception("SQLSTATE: {$error[0]}. {$error[1]} {$error[2]}");
}
Output:
<table width="800" border="0">
<?php foreach ($rows as $row): ?>
<tr>
<form action="/Echos Online/Detail.php" method="get"><input value='<?php echo $row['CAT NO.']; ?>' name="detail" type="hidden"/><td width='100' rowspan='4'><input type="image" img src="<?php echo $row['IMAGE PATH']; ?>" width="<?php if ($row['FORMAT']=='DVD') {echo "70";} else if ($row['FORMAT']=='DVD+CD') {echo "70";} else if($row['FORMAT']=='BLURAY+CD') {echo "81";}else if($row['FORMAT']=='BLURAY+DVD') {echo "81";} else if($row['FORMAT']=='BLURAY') {echo "81";} else {echo "100";} ?>" height="100"></td></form>
<td width="100">Artist:</td>
<td><?php echo $row['ARTIST']; ?></td>
</tr>
<tr>
<td width="100">Title</td>
<td><?php echo $row['TITLE']; ?></td>
</tr>
<tr>
<td width="100">Format</td>
<td><?php echo $row['FORMAT']; ?></td>
</tr>
<tr>
<td width="100">Cat. No.</td>
<td><?php echo $row['CAT NO.']; ?></td>
</tr>
<hr background-color="#e4a566" color="#e4a566"; width="100%"/>
<?php endforeach; ?>
</table>
$pdo = new PDO('mysql:host=localhost;dbname=mydb', $username, $password);
$stmt = $pdo->prepare('SELECT * FROM catelogue WHERE catelogue.TITLE LIKE :search');
$stmt->bindValue('search', $search);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
OR
$stmt = $pdo->prepare('SELECT * FROM catelogue WHERE catelogue.TITLE LIKE :search');
$stmt->execute(array('search' => $search);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
note that you probably want to have your $search string wrapped with wildcard characters, like
$stmt->bindValue('search', '%'.$search.'%');
also note that searching using like wont use index if you have wildcard on left side of like criteria
$stmt->bindValue('search', '%'.$search.'%'); //will not use index
$stmt->bindValue('search', $search.'%'); //will use index