Page 1 of 1

Table with Connection MySQL DataBase

Posted: Tue Oct 25, 2011 1:45 pm
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

Re: Table with Connection MySQL DataBase

Posted: Tue Oct 25, 2011 1:56 pm
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.

Re: Table with Connection MySQL DataBase

Posted: Tue Oct 25, 2011 2:27 pm
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>');
?>

Re: Table with Connection MySQL DataBase

Posted: Tue Oct 25, 2011 5:15 pm
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.

Re: Table with Connection MySQL DataBase

Posted: Tue Oct 25, 2011 5:18 pm
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

Re: Table with Connection MySQL DataBase

Posted: Wed Oct 26, 2011 8:45 am
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; }

Re: Table with Connection MySQL DataBase

Posted: Wed Oct 26, 2011 12:15 pm
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.

Re: Table with Connection MySQL DataBase

Posted: Thu Oct 27, 2011 4:31 pm
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

Re: Table with Connection MySQL DataBase

Posted: Thu Oct 27, 2011 4:43 pm
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>

Re: Table with Connection MySQL DataBase

Posted: Thu Oct 27, 2011 4:51 pm
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.

Re: Table with Connection MySQL DataBase

Posted: Thu Oct 27, 2011 4:53 pm
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.

Re: Table with Connection MySQL DataBase

Posted: Thu Oct 27, 2011 5:19 pm
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...

Re: Table with Connection MySQL DataBase

Posted: Thu Oct 27, 2011 5:21 pm
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?

Re: Table with Connection MySQL DataBase

Posted: Thu Oct 27, 2011 5:31 pm
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

Re: Table with Connection MySQL DataBase

Posted: Thu Oct 27, 2011 5:37 pm
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.