PDA

View Full Version : php+mysql problems



zomby_pengy
April 23rd, 2008, 11:34 AM
<?php
$dbhost = 'mysql';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db("cms", $conn);
$query = "SELECT * FROM news";
$result = mysql_query($query, $conn);
$result_ar = mysql_fetch_assoc($result);
print_r($result_ar);
?>

Im having some problems with this script... instead of getting me all the information from the "news" table like I am trying to tell it to do, I am only getting back one of the rows from the table... I have three rows on the table right now, and when I do the print_r(); on the array, i only get this:

Array
(
[id] => 1
[title] => Testing
[author] => Rob Carrillo
[date] => 4/23/08
[content] => this is just a test!!!!!
)

simplistik
April 23rd, 2008, 12:44 PM
<?php
$dbhost = 'mysql';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db("cms", $conn);
$query = "SELECT * FROM news";
$result = mysql_query($query, $conn);
$result_ar = mysql_fetch_assoc($result);
print_r($result_ar);
?>

try ummm... replacing:


$result_ar = mysql_fetch_assoc($result);
print_r($result_ar);

with


$myarray = array();
while ( $result_ar = mysql_fetch_assoc($result) )
{
$myarray[] = $result_ar;
}
print_r($myarray);


didn't test it but it should work

nobody
April 23rd, 2008, 02:27 PM
You shouldn't have to do that, though. The data should all be dumped into the array regardless of looping through it it.

Sounds to me like a database problem.

simplistik
April 23rd, 2008, 03:05 PM
Pretty sure you have to loop through the array to get it to return more than one result. It's very similar in how if you did


while ( $row = mysql_fetch_assoc($result) )
{
echo "<p>".$row['name']."<br/>".$row['description']."</p>";
}

which will return a list of your rows based on the query;

where as


$row = mysql_fetch_assoc($result);
echo "<p>".$row['name']."<br/>".$row['description']."</p>";

will always return the first entry and stop

Yeldarb
April 23rd, 2008, 03:31 PM
Yep, Simp is right, to get a 2d array like that you have to construct it yourself by iterating through the result.

nobody
April 23rd, 2008, 04:01 PM
That's weird, I really feel like I've had a different experience, but for the past six months I've been using mysql_fetch_object like it's my job so what do I know.

Ignore me.

zomby_pengy
April 23rd, 2008, 04:26 PM
awesome, thanks so much!

simplistik
April 23rd, 2008, 04:41 PM
That's weird, I really feel like I've had a different experience, but for the past six months I've been using mysql_fetch_object like it's my job so what do I know.

Ignore me.
Can you post an example of how you use it to get more than one result? I ist interested in seeing it, I'm always interested in learning different methods.

joran420
April 23rd, 2008, 05:36 PM
yeah ive always had to iterate through with the while statement...so if you can get an array with a single statement im interested in hearing how..