SELECT query returning ARRAY instead of database values

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
billspeg
Forum Newbie
Posts: 7
Joined: Sun Dec 28, 2003 3:47 am

SELECT query returning ARRAY instead of database values

Post by billspeg »

hi this is not coplicated code at all and i don't understand why it is not workin i am just creating a php front end reader of the info in my data base and i get no erroers but i don't get any real data from the database either just the word array
<?
$dbcon = mysql_pconnect("xxxxxxx", "xxxxxx", "xxxxx") or die(mysql_error());
mysql_select_db("suggestions", $dbcon) or die(mysql_error());
$query = "SELECT firstname,suggestion,lastname FROM suggestions "or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
$num_results = mysql_num_rows($result) or die(mysql_error());
for($i = 0; $i < $num_results; $i++){
$row = mysql_fetch_row($result) or die(mysql_error());
echo "Suggestion #". $i;
echo ($row);
}
?>

help please this is my first real complete application in php/mysql and believe me the data exists in a database called suggestions
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The prob is that $row is an array and you can't just echo it, see code with comments below:

Code: Select all

<?php

$dbcon = mysql_pconnect('Billspeg2000', 'root', '382spent') or die(mysql_error());

mysql_select_db('suggestions', $dbcon) or die(mysql_error());

// shouldn't have an or die() statement after this
$query = "SELECT firstname, suggestion, lastname FROM suggestions";

$result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');

// shouldn't have an or die() statement after this either any probs
// will be caught by the die statements after mysql_pconnect(), 
// mysql_select_db() or mysql_query() - they're the only ones that
// need that type of error handling.
$num_results = mysql_num_rows($result);

for ($i = 0; $i < $num_results; $i++) {

	// again no or die() statement needed
	// use mysql_fetch_assoc() instead of mysql_fetch_row() to make
	// it easier to retrieve the data from the resulting array
	$row = mysql_fetch_assoc($result);

	// here we retrieve each element from the array
	// htmlspecialchars() is used to ensure any special characters
	// are encoded
	// nl2br() is used on the suggestion in case it's got linebreaks
	// in it - if it's just one line you can remove this.
	$firstname = htmlspecialchars($row['firstname']);
	$lasname = htmlspecialchars($row['lastname']);
	$suggestion = nl2br(htmlspecialchars($row['suggestion']));


	// i've added some HTML because the results will be easier to 
	// read but it is easy to change
	echo '<h1>Suggestion #'.$i.'</h1>';
	
	// you can't just do 'echo ($row);' because $row is an array and
	// you need to actually say which part you want to display
	echo '<p>Suggestion from: '.$firstname.' '.$lastname.'</p>';
	echo '<p>'.$suggestion.'</p>';
}
?>
Mac
billspeg
Forum Newbie
Posts: 7
Joined: Sun Dec 28, 2003 3:47 am

thanks used most of your suggestions and they all worked

Post by billspeg »

hi used most of your suggestions and it works now i sometimes get this idea that my code doesn't work because i am cursed but i guess it's just inexperience thanks again bill
Post Reply