Table with Connection MySQL DataBase

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PF2G
Forum Newbie
Posts: 13
Joined: Sat Sep 10, 2011 10:51 am

Table with Connection MySQL DataBase

Post by PF2G »

Hello, PHPFreaks

I'm creating a website about a Music School and i want to create a table with the instruments one row with image and a row bellow with their name.

Code: Select all

<table width="200" height="134">
<?PHP

//connect database
$dbhost = "";
$db = "";
$dbuser = "";
$dbpass = "";
$connect = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Falha ao ligar à Base de Bados: " . mysql_error());

//3 columns limit
i == 3;
while {
 i == i + 1;
 if i == 1 {
?>
<tr>
<td>

<?PHP  
}  else  
{
?>
<td>

<?    
}
$sql = "SELECT nome_curso, imagem_curso FROM cursos ORDER BY nome_curso";
}
if i == 3 {
?>
</td>
</tr>

<? 
}else
{
?>
</td>

<?PHP
}?>
</table>
The name and the image are in a MySQL DB.

Please help me.

Thank You
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Table with Connection MySQL DataBase

Post by mikeashfield »

Your code is a mess.

There are lots of issues, the biggest one being that you don't set an active schema ($db) for your queries.
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Table with Connection MySQL DataBase

Post by egg82 »

Code: Select all

<?php
//connect to the database
$dbhost = "";
$db = "";
$dbuser = "";
$dbpass = "";
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$link){
	echo(mysql_error());
	exit();
}
$connect = mysql_select_db($db, $link);
if(!$connect){
	echo(mysql_error());
	exit();
}

//query the database
$result = "SELECT `nome_curso`, `imagem_curso` FROM `cursos` ORDER BY `nome_curso`";
if(!$result){
	echo(mysql_error());
	exit();
}
//output the table
echo('<table cellpadding="2" cellspacing="2">');
//0-2 = 3 rows
for($i=0;$i<2;$i++){
	echo('<tr>');
	echo('<td>'.$row["nome_curso"].'</td>');
	echo('</tr><tr>');
	echo('<td><img src="'.$row["imagem_curso"].'"></td>');
	echo('</tr>');
}
echo('</table>');
?>
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Table with Connection MySQL DataBase

Post by genix2011 »

Hi,

Code: Select all

<?php
//connect to the database
$dbhost = "";
$db = "";
$dbuser = "";
$dbpass = "";
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
$connect = mysql_select_db($db, $link) or die(mysql_error());

//query the database
// If you want to Limit your result, just add LIMIT to your query...
$result = mysql_query("SELECT `nome_curso`, `imagem_curso` FROM `cursos` ORDER BY `nome_curso` LIMIT 0,3") or die(mysql_error());

//output the table
echo '<table cellpadding="2" cellspacing="2">';
while($row = mysql_fetch_array($result)){
    echo '<tr><td>'.$row["nome_curso"].'</td></tr><tr><td><img src="'.$row["imagem_curso"].'"></td></tr>';
}
echo '</table>';
?>
As I already commented, if you want to limit your result, just add LIMIT to your query.

And do not fill your call-stack with unnecessary echo calls.

Greets.
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Table with Connection MySQL DataBase

Post by egg82 »

Code: Select all

$result = mysql_query("SELECT `nome_curso`, `imagem_curso` FROM `cursos` ORDER BY `nome_curso` LIMIT 0,3") or die(mysql_error());
removed your SPAN tags
and I still like exit better :lol:

The reason I echoed more than needed is because it's much easier to read and change. Human readability should come before computer readability
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Table with Connection MySQL DataBase

Post by Benjamin »

egg82 wrote:The reason I echoed more than needed is because it's much easier to read and change. Human readability should come before computer readability
Yes it should.

This is exactly why the code should be split up into MVC.

Therefore this:

Code: Select all

//output the table
echo('<table cellpadding="2" cellspacing="2">');
//0-2 = 3 rows
for($i=0;$i<2;$i++){
	echo('<tr>');
	echo('<td>'.$row["nome_curso"].'</td>');
	echo('</tr><tr>');
	echo('<td><img src="'.$row["imagem_curso"].'"></td>');
	echo('</tr>');
}
echo('</table>');
Would become this:

Code: Select all

<?php if ($records): ?>
<div id="instrumentList">
  <?php foreach ($records as $row):  ?>
    <div>
      <img src="<?php echo $row['imagem_curso']; ?>" alt="<?php echo $row['nome_curso']; ?>" />
      <span><?php echo $row['nome_curso']; ?></span>
    </div>
  <?php endforeach; ?>
</div>
<?php endif; ?>
Further, the view ideally would not use tables for layout, but instead CSS

Code: Select all

#instrumentList { }
#instrumentList img { float: left; width: 100px; margin: 0px 10px 10px 0px; }
#instrumentList span { float: left; }
PF2G
Forum Newbie
Posts: 13
Joined: Sat Sep 10, 2011 10:51 am

Re: Table with Connection MySQL DataBase

Post by PF2G »

Sorry, guys

But can you tell me the whole code. I'm confuse with so much codes you gave me xP

Thank You.
PF2G
Forum Newbie
Posts: 13
Joined: Sat Sep 10, 2011 10:51 am

Re: Table with Connection MySQL DataBase

Post by PF2G »

The code is now correct:

Code: Select all

<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "cursos";    
$connect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

mysql_select_db($database,$connect);
$sql_imagem = "SELECT imagem, nome FROM cursos ORDER BY nome ASC";

echo "<table widht = 100% height = 25% border = 1>";

echo "<tr>  <td>";

$executa=mysql_query($sql_imagem,$connect);
while($dados=mysql_fetch_array($executa))
  {
   extract($dados);
   echo "<img src = $imagem>";
  }
 
 echo "  </td>  </tr>"; 
?>
Now i want to limit the number of columns. And other thing, the name has to appear bellow the image. I was thinking about a <br> or the image in one row and the name in other.

PLEASE THIS IS URGENT

Thank You Very Much
Last edited by PF2G on Thu Oct 27, 2011 5:17 pm, edited 1 time in total.
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Table with Connection MySQL DataBase

Post by mikeashfield »

[quote="PF2G"]The code is now correct:

Code: Select all

<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "cursos";       
$connect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

mysql_select_db($database,$connect);
$sql_imagem = "SELECT imagem FROM cursos WHERE cod_curso = 2";
?>

<table widht = 100% height = 25% border = 1>
<tr>
<td>

<?PHP
$executa=mysql_query($sql_imagem,$connect);
while($dados=mysql_fetch_array($executa))

{
extract($dados);
echo "$imagem <br />";
}
?>

</td>
</tr>
This code should be:

Code: Select all

<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "cursos";       
$connect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

mysql_select_db($database,$connect);
$sql_imagem = "SELECT imagem FROM cursos WHERE cod_curso = 2";
?>

<table widht = 100% height = 25% border = 1>
<tr>
<td>

<?PHP
$executa=mysql_query($sql_imagem,$connect);
while($dados=mysql_fetch_array($executa))

{
extract($dados);
echo"<img src=" . $sql_imagem . "></img>" . "<br/>";
}
?>

</td>
</tr>
Last edited by mikeashfield on Thu Oct 27, 2011 4:56 pm, edited 1 time in total.
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Table with Connection MySQL DataBase

Post by egg82 »

one last thing: be careful where you get your pictures. Some people like to copyright their work, and you could potentially be in the middle of a legal battle.
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Table with Connection MySQL DataBase

Post by mikeashfield »

Another thing too, your URL for the Bass wont work on any other machine than the server the code is being hosted on. You'll need to give it a net-wide reference not a local one.
PF2G
Forum Newbie
Posts: 13
Joined: Sat Sep 10, 2011 10:51 am

Re: Table with Connection MySQL DataBase

Post by PF2G »

mikeashfield wrote:Another thing too, your URL for the Bass wont work on any other machine than the server the code is being hosted on. You'll need to give it a net-wide reference not a local one.
Yes, i saw that mistake, when i was testing the code. Stupid error xP

But now i edited the post. If you could help me...
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Table with Connection MySQL DataBase

Post by mikeashfield »

PF2G wrote:
mikeashfield wrote:Another thing too, your URL for the Bass wont work on any other machine than the server the code is being hosted on. You'll need to give it a net-wide reference not a local one.
Yes, i saw that mistake, when i was testing the code. Stupid error xP

But now i edited the post. If you could help me...
So the code that I posted above didn't work? I don't see anything wrong with it?
PF2G
Forum Newbie
Posts: 13
Joined: Sat Sep 10, 2011 10:51 am

Re: Table with Connection MySQL DataBase

Post by PF2G »

mikeashfield wrote:
PF2G wrote:
mikeashfield wrote:Another thing too, your URL for the Bass wont work on any other machine than the server the code is being hosted on. You'll need to give it a net-wide reference not a local one.
Yes, i saw that mistake, when i was testing the code. Stupid error xP

But now i edited the post. If you could help me...
So the code that I posted above didn't work? I don't see anything wrong with it?

The code you posted, the images didn't appear...

No offense, but i would like to move on to the next step. I need this ASAP.

Thnk You
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Table with Connection MySQL DataBase

Post by mikeashfield »

So the code that I posted above didn't work? I don't see anything wrong with it?[/quote]


The code you posted, the images didn't appear...

No offense, but i would like to move on to the next step. I need this ASAP.

Thnk You[/quote]

No offence to you either, but it's clearly evident from this thread alone that you don't follow the replies with code.
Post Reply