PHP and SQLite 3, multiple returned row - php

I have a problem reading out multiple rows that get returned from an SQLite 3 database.
I tested my querys before and i know what the result should be. But i cant seem to figure out how to read these different lines/rows that get returned from this query on a PHP based webpage.
I searched the web, including stackoverflew but i can't seem to find an anwser to this question :/
Any help would be great!
Regards
P.S. this is how i fetch my the multiple rows on my php page:
$ID[$i] = $dbase->querySingle("SELECT rowid FROM rules WHERE parameter".$i." = '$Model'");

I have this in PHP:
$query = $db->query("SELECT * FROM files WHERE OBJECT = '".$id."'") ;
// begin table
echo '<table bgcolor="#FFFFFF">'."\n";
// give titles
echo '<tr>';
echo '<td bgcolor="#669999"><b><u>File</u></b></td>';
echo '<td bgcolor="#669999"><b><u>Night</u></b></td>';
echo '<td bgcolor="#669999"><b><u>Name</u></b></td>';
echo '<td bgcolor="#669999"><b><u>comment</u></b></td>';
echo '</tr>'."\n";
//
while($row = $query->fetchArray()) {
echo '<tr>';
echo '<td bgcolor="#CCCCCC">'.$row["uniquenumber"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["NIGHT"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["NAME"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["COMMENTS"].'</td>';
echo '</tr>'."\n";
}
It works here.
Hope it can help you !

Related

MySQL select where column LIKE and is not empty

I have the following query and I'm trying to select only the rows where I have picture.
In table kat_gl_picture I have 3 categories, but I don't have picture in all 3 categories yet!
All work just fine, but I have printed name of third category, where I don't have picture.
I tried WHERE link LIKE '%$first_var%'AND NOT (link <=> NULL)
....IS NOT NULL - but nothing yet worked.
Tabele1 and 2 and web problem solved
<?
include("connection.php");
$kategorije = mysql_query("SELECT * FROM kat_gl_picture ORDER BY rbr");
while ($red=mysql_fetch_array($kategorije))
{
$first_var = $red['kat'];
$result = mysql_query("SELECT id, naziv, ime, tekst, username, link, file_name, datum FROM Tab_Pic_Pic
WHERE link LIKE '%$first_var%'
ORDER BY id");
echo '<table>';
echo '<tbody>';
echo $first_var;
echo '<tr>';
echo '<TD valign="top">';
while ($row=mysql_fetch_array($result))
{
list($x, $y) = getimagesize("admin /upload/".$row['file_name']);
if ($x>$y) {
$y=($y/$x)*150;
$x=150;
}
else
{
$x=($x/$y)*115;
$y=115;
}
$ID_broj = $row["id"];
$tekst_broj = $row["tekst"];
?>
<? echo '<img src="admin /upload/'.$row['file_name'].'" height="'.$y.'" width="'.$x.'"/>';?>
<?
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
The problem is that I cannot determine if there is a foreign key in your Tab_Pic_Pic table. You can however join on a LIKE.
Try this:
SELECT * FROM Tab_Pic_Pic JOIN kat_gl_picture ON Tab_Pic_Pic.link LIKE CONCAT('%', kat_gl_picture.kat, '%') ORDER BY kat_gl_picture.kat, Tab_Pic_Pic, rbr
It should give you a list of all pictures including the kat_gl_picture.kat field which you can just use a local variable to detect change.
Would be easier to provide a more accurate example if I had the full table info. Standard method is to have a foreign key in Tab_Pic_Pic that references the corresponding primary key from the kat_gl_picture table.
Alternatively, if you simply want to omit the blank category it can be done in your PHP code like this:
if(mysqli_num_rows($result)>0){
echo '<table>';
echo '<tbody>';
echo $first_var;
echo '<tr>';
echo '<TD valign="top">';
while ($row=mysql_fetch_array($result))
{
list($x, $y) = getimagesize("admin /upload/".$row['file_name']);
if ($x>$y) {
$y=($y/$x)*150;
$x=150;
}
else
{
$x=($x/$y)*115;
$y=115;
}
$ID_broj = $row["id"];
$tekst_broj = $row["tekst"];
?>
<? echo '<img src="admin /upload/'.$row['file_name'].'" height="'.$y.'" width="'.$x.'"/>';?>
<?
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
Wrapping the output of your HTML in a simple condition like this should illustrate that the script itself is inefficient. Trust me when I say it can all be accomplished with one query. Do not get frustrated. You'll figure it out. Programming is hard.

How to display emaiIds with hyperlink when using php-mysql

I am display the contents of mysql table using php.
Based on the user inputs, I will do some querying and the results results will be stored in an array and displayed.
Now I have emilIds in the output, when I display the table in the webpage, I want to display with hyperlinks.
How can I achieve this. Thanks in advance.
if($result2->num_rows > 0){
echo '<table width="100%" bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" border="1" class="db-table" boarder-collapse ="collapse">';
echo '<tr><th>STUDENT_NAME</th><th>CLASS</th><th>ID_NUMBER</th><th>ADDRESS</th><th>CITY</th><th>STATE</th><th>ZIP</th><th>COUNTRY</th><th>PHONE</th><th>INSURANCE</th>
<th>EMAIL</th><th>MAILING_ADDRESS</th><th>JOINED_ON</th></tr>';
while($rowz2 = mysqli_fetch_assoc($result2)){
echo "<tr>";
foreach($rowz2 as $key=>$value){
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo "</table><br/>";
}
else{
echo "<h2>No data based on the entered values</h2>";
}
Use an a tag with mail:
echo '<td>' . $value .'</td>';
Assuming that $value content is an email address
Changes based the code you posted in your comments:
Apparently was a couple of nesting problems but I think what you wanted to say was...
echo "<table>";
while($rowz2 = mysqli_fetch_assoc($result2)){
echo "<tr>";
foreach($rowz2 as $key=>$value){
if($key=='EMAIL') {
echo '<td>' . $value . '</td>';
} else {
echo '<td>' . $value . '</td>';
}
}
echo '</tr>';
}
echo "</table><br/>";
I can't say for sure if that would work as you expect but if a value is retrieved from DB with a field name "EMAIL" then it will be echoed inside a mailto: link.
So whether the field name is EMAIL or you used SELECT this_field, that_field, the_email_field as EMAIL FROM your_table it would bring a working mailto: link in your table.
I cannot comment yet so i will post it here. Check if your database column is 'email' or 'EMAIL' because
if ($key == 'EMAIL')
is case sensitive and as such it will never pass the condition if column is 'email'. If for some reason you need a case insensitive string comparison use:
if (strcasecmp($key, 'EMAIL') === 0)

Can I display query results without naming columns?

Is there a php or html code to dynamically generate a table.
query :
"SELECT * FROM table1;
Can I display this grid of info without any specifics.
IF there are 4 rows and 4 columns I want the table to be that size; if 5x5 than that.
It seems like this should be possible, but all code I can find wants me to specify names or columns.
Yes. There are a couple different ways to do this, but for illustration I will assume that the result of your query is stored in a variable called $results, which is simply a multidimensional array that you can loop through using a double foreach to dynamically produce your table.
echo '<table>';
foreach ($results as $row) {
echo '<tr>';
foreach ($row as $col) {
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';
OR if you don't have a $results array and are instead getting query results and building the table at the same time, something like this might be more appropriate for your needs:
echo '<table>';
while ($row = mysqli_fetch_array($query)) {
echo '<tr>';
foreach ($row as $col) {
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';
echo'<table>';
while($row = mysql_fetch_array($result))
{
echo'<tr>';
echo '<td>'.$row[colname].'</td>';
echo '</tr>';
}
echo'</table>';

Creating a HTML table with MYSQL fetching data doesn't work

I'm attempting to create a dynamic table with MYSQL's function: while(mysql_fetch_assoc).. However, when it fetches more than one result, it doesn't create the table anymore (or fill in the tags. Excuse me for explaining this incorrectly)
This is my code. Ignore the Dutch words :)
$sql2 = mysql_query("SELECT * FROM kostendb WHERE ProjectID = '$_GET[id]'") or die (mysql_error());
echo '
<table border="1" style="width:60%">
<tr>
<th>Kostencode</th>
<th>Datum</th>
<th>Bedrag</th>
</tr>';
while($res = mysql_fetch_assoc($sql2))
{
echo '<tr>';
echo '<td>' .$res['KostenID']. '</td>';
echo '<td>' .$res['Datum']. '</td>';
echo '<td>' .$res['Bedrag']. '</td>';
echo '</tr>';
}
echo '</table>';
When it finds more than one result, the while-loop doesn't do anything. When it finds just one result, it works fine.
What is causing this, and how can I fix this?
I've checked out an example script, but it's exactly using my method.
Thanks
You might have mixed mysql with mysqli.
Choose one, don't mix and this might fix your problem.
make a variable like $project = $_GET['ID'];
then put in sql statement as ....WHERE Project_ID = $project");
Try this

PHP Echo Table formatting. 3rd column to appear under row 1. Is it possible with Echo?

I am using PHP to pull back data from within MySQL. My basic web page and query is working ok for the most part. I am using echo to dump the results out to a table on screen. The code is as follows:
foreach ($dbh->query($sql) as $row){
echo "<tr>";
echo "<td'>".$row['DATAA']."</td>";
echo "<td'>".$row['DATAB']."</td>";
echo "<td'>".$row['DATAC']."</td>";
echo "</tr>";
}
$sql->closeCursor();
I would like to do some slightly advanced formatting of the tables as follows:
Does anyone know if this is possible with the PHP Echo command?
I am having problems finding suggestions around the internet, due to PHP and HTML tables vs MySQL tables and rows and columns all getting mixed up on search results.
So any help or code samples would be appreciated.
Nothing to do with echo. you need to format your html (Use colspan) as below
foreach ($dbh->query($sql) as $row){
echo '<tr>';
echo '<td>'.$row['DATAA'].'</td>';
echo '<td>'.$row['DATAB'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2">'.$row['DATAC'].'</td>';
echo '</tr>';
}
You must use the colspan property of a table:
foreach ($dbh->query($sql) as $row){
echo "<tr>";
echo "<td'>".$row['DATAA']."</td>";
echo "<td'>".$row['DATAB']."</td>";
echo "</tr>";
echo "<tr colspan=\"2\">";
echo "<td'>".$row['DATAC']."</td>";
echo "</tr>";
}
$sql->closeCursor();

Categories