Jump to content

Updates


Mencarta
 Share

Recommended Posts

I have an updates script that gets news from the database and displays the most current one. I get an error though, if you can also spot any 'future' errors that will be great.updates.php

<?php	require_once("dbconnect.php");	$updates = array()		$sql = "SELECT * FROM news";	$result = mysql_query($sql);	if (mysql_num_rows($result) == 0) {		echo "No New Updates...";		exit;	}		for ($i = 1;$i < mysql_num_rows($result);$i++) {		$updates[] = $result["id"];	}	arsort(updates);		for ($i = 0;$i < 3;$i++) {		$sql2 = "SELECT * FROM news WHERE id='$updates[$i]'";		$result2 = mysql_query($sql2);		echo "$result2['title']";		echo "$result2['date']";		echo "$result2['message']";	}?>

I get this error: Parse error: syntax error, unexpected T_VARIABLE in /www/zxq.net/p/r/i/princefinance/htdocs/updates.php on line 6

Link to comment
Share on other sites

Now I have this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /www/zxq.net/p/r/i/princefinance/htdocs/updates.php on line 21updates.php

<?php	require_once("dbconnect.php");	$updates = array();		$sql = "SELECT * FROM news";	$result = mysql_query($sql);	if (mysql_num_rows($result) == 0) {		echo "No New Updates...";		exit;	}		for ($i = 1;$i < mysql_num_rows($result);$i++) {		$updates[] = $result["id"];	}	arsort($updates);		for ($i = 0;$i < 3;$i++) {		$sql2 = "SELECT * FROM news WHERE id='$updates[$i]'";		$result2 = mysql_query($sql2);		echo "$result2['title']";		echo "$result2['date']";		echo "$result2['message']";	}?>

Link to comment
Share on other sites

You really should learn how to debug these things for yourself :) look at line 21. Think about the syntax for variables. Why do you have quotation marks around it?

Link to comment
Share on other sites

Do you know what doing that does? Why did you choose to put quotes around it vs. not, or did you just see someone else do that and assume that's what you do? Putting quotes around a variable has a specific effect, so unless you're trying to achieve that specific effect there's no reason to just quote a variable when you print it.Also, if it's OK to do that, then why is PHP complaining? What about the special syntax for using an array value inside a quoted string? $result2 in this case is not an array, it's a result, so trying to use array syntax on it wouldn't even work.

Link to comment
Share on other sites

Yes, but if you do, you need to delimit syntactically ambiguous variable references, e.g.

echo "{$result2['title']}";

Link to comment
Share on other sites

Do you know what doing that does? Why did you choose to put quotes around it vs. not, or did you just see someone else do that and assume that's what you do? Putting quotes around a variable has a specific effect, so unless you're trying to achieve that specific effect there's no reason to just quote a variable when you print it.Also, if it's OK to do that, then why is PHP complaining? What about the special syntax for using an array value inside a quoted string? $result2 in this case is not an array, it's a result, so trying to use array syntax on it wouldn't even work.
I don't know what you just said, all I know and care about is that it is working and why its working and not my previous version.
Link to comment
Share on other sites

Right, you need to understand why the things that work do, and the effects of doing things. There's a difference between this:$var = "{$result2['title']}";and this:$var = $result2['title'];One of them casts the value as a string, and one of them doesn't. When you echo or print something you print the string value of it, so there's no reason to cast the value to a string before printing it, it will be cast automatically when it prints.Even so, that wouldn't have changed anything for your situation except the error message. So if you're interested in learning, here's the problem:$result2 = mysql_query($sql2);echo $result2['title'];Look at the type that mysql_query returns:http://www.php.net/manual/en/function.mysql-query.php

resource mysql_query ( string $query [, resource $link_identifier ] )
It returns a resource type. This syntax:$result2['title']Is to refer to an item in an array, but mysql_query doesn't return an array, it returns a resource, specifically a MySQL Result Resource. There are several other MySQL functions which take result resources, including this one:http://www.php.net/manual/en/function.mysql-fetch-assoc.php
array mysql_fetch_assoc ( resource $result )
That function takes a resource as input and returns an array:
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
And it explains what the result parameter is:
resultThe result resource that is being evaluated. This result comes from a call to mysql_query().
There are several examples on that page of using mysql_fetch_assoc with mysql_query.
$result = mysql_query($sql);if (!$result) {	echo "Could not successfully run query ($sql) from DB: " . mysql_error();	exit;}if (mysql_num_rows($result) == 0) {	echo "No rows found, nothing to print so am exiting";	exit;}// While a row of data exists, put that row in $row as an associative array// Note: If you're expecting just one row, no need to use a loop// Note: If you put extract($row); inside the following loop, you'll//	   then create $userid, $fullname, and $userstatuswhile ($row = mysql_fetch_assoc($result)) {	echo $row["userid"];	echo $row["fullname"];	echo $row["userstatus"];}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...