You are viewing our Forum Archives. To view or take place in current topics click here.
PhP MySQL help please
Posted:

PhP MySQL help pleasePosted:

Endermite
  • New Member
Status: Offline
Joined: Nov 28, 20149Year Member
Posts: 21
Reputation Power: 0
Status: Offline
Joined: Nov 28, 20149Year Member
Posts: 21
Reputation Power: 0
I'm basically trying to get PhP to display all of the contents of the table in by website. Here's the code, I know that the passwords e.c.t are all correct, so it's not that.

<?php
$dbhost = '******';
$dbuser = 'root';
$dbpass = '*******';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM online';

mysql_select_db('****');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "online :{$row['name']} <br>".
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
#2. Posted:
Hacz
  • Christmas!
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
First and foremost, don't use mysql_* functions. Instead, use PDO.

This works and is in PDO:


<?php
$con = mysql_connect('localhost', 'root', '');
@mysql_select_db('test') or die( "Unable to select database");
$db = new PDO('mysql:host=' . 'localhost' . ';dbname=' . 'test', 'root', '');

$sql = $db->query("SELECT * FROM `bullshit`");
while($q = $sql->fetch(PDO::FETCH_ASSOC)) {
   echo $q['ID'].'-'.$q['name'].'-'.$q['desc1'].'-'.$q['desc2'].'<br>';
}
?>


Picture of DB Structure and it working (don't know why you would need this, just thought I'd throw it in there)

[ Register or Signin to view external links. ]
[ Register or Signin to view external links. ]

Enjoy.
#3. Posted:
tc4waza
  • Powerhouse
Status: Offline
Joined: Mar 21, 200915Year Member
Posts: 471
Reputation Power: 20
Status: Offline
Joined: Mar 21, 200915Year Member
Posts: 471
Reputation Power: 20
instead of using a wildcard to select all the columns in a table explicitly call them

like
<code>

<?php
$dbhost = '******';
$dbuser = 'root';
$dbpass = '*******';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT first, last, age, DOB from online";

mysql_select_db('****');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
echo "online :{$row['first']} <br />";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>

</code>
#4. Posted:
Endermite
  • New Member
Status: Offline
Joined: Nov 28, 20149Year Member
Posts: 21
Reputation Power: 0
Status: Offline
Joined: Nov 28, 20149Year Member
Posts: 21
Reputation Power: 0
So, when I've updated it and load it in the browser, here's what I get

"; } echo "Fetched data successfully\n"; mysql_close($conn); ?>
#5. Posted:
Cyimking
  • V5 Launch
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Endermite wrote So, when I've updated it and load it in the browser, here's what I get

"; } echo "Fetched data successfully\n"; mysql_close($conn); ?>


Try this:

 echo "online :".$row['first']."<br />";


See if this fixes it where on the browser it only returns "Fetched data successfully" and not " "; } echo "Fetched data successfully\n"; mysql_close($conn); ?> "
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.