samoi Posted September 25, 2009 Share Posted September 25, 2009 hi! <? $query="select timediff(dueTime, now()) from store where userid=".$id; $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result); if ($row[0][0]=="-"){ // do this } else{ // do that } ?> the question is : what does this mean? <? $row[0][0] ?> Quote Link to comment https://forums.phpfreaks.com/topic/175457-solved-explaination-needed-please-for-sql/ Share on other sites More sharing options...
hamza Posted September 25, 2009 Share Posted September 25, 2009 hi! <? $query="select timediff(dueTime, now()) from store where userid=".$id; $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result); if ($row[0][0]=="-"){ // do this } else{ // do that } ?> the question is : what does this mean? <? $row[0][0] ?> $row=mysql_fetch_row($result); IS FETCHING ONLY ONE ROW FROM TABLE as an array see syntax array mysql_fetch_row ( resource $result ) it return array $row ----- ROW IS IN THIS array VARIABLE $row[0][0] AND THESE ARE array INDEX LIKE INDEX ARE COLUMN. ON 0 array INDEX NEED ZERO array INDEX VALUE. TRY TO RUN CODE AND CHECK OUT WHAT EXACTLY WHAT HAPPENING IN IT. or see http://www.php.net/mysql_fetch_row hope it helps you alote Quote Link to comment https://forums.phpfreaks.com/topic/175457-solved-explaination-needed-please-for-sql/#findComment-924591 Share on other sites More sharing options...
mikesta707 Posted September 25, 2009 Share Posted September 25, 2009 well, that is wrong first off. but ignoring that it basically means that $row is a multidimensional array, or an array of arrays. say for instance we had an array definition that looked like $arr = array(0=>array("array", "of", "different", "strings"), 1=> array("More", "strings")/*etc.. */); the first index, accessed by $row[0]; which is an array with 4 different entries. You can also access the first index of the array at index 0 by doing $row[0][0] if I were to echo that it would say "Array" if I were to output stuff like so echo "I am an " . $row[0][0] . " " . $row[0][1] . " " . $row[0][2] . " " . $row[0][3]; the output would be I am an array of different strings. Just think of it as a table. the first set of brackets is the row, and the second is the column Now for why thats wrong. The function mysql_fetch_array() only returns a single dimension array with the elements that your SQL query returned from one row That means that doing that function will only return the first row of your sql return set. if you want everything, you have to iterate through the array, like so $query="select timediff(dueTime, now()) from store where userid=".$id; $result=mysql_query($query, $db_id); $allTheRows = array(); while($row = mysql_fetch_array($result)){//this is the key part $allTheRows[] = $row//this pushes the content of $row into the array allTheRows } now you could do echo $allTheRows[0][0]; and that would echo the first column of the first row returned. However in your code, the array $row is 1 dimensional, and it is being accessed if it were two dimensional. which will probably throw error. Quote Link to comment https://forums.phpfreaks.com/topic/175457-solved-explaination-needed-please-for-sql/#findComment-924592 Share on other sites More sharing options...
samoi Posted September 25, 2009 Author Share Posted September 25, 2009 hi! <? $query="select timediff(dueTime, now()) from store where userid=".$id; $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result); if ($row[0][0]=="-"){ // do this } else{ // do that } ?> the question is : what does this mean? <? $row[0][0] ?> $row=mysql_fetch_row($result); IS FETCHING ONLY ONE ROW FROM TABLE as an array see syntax array mysql_fetch_row ( resource $result ) it return array $row ----- ROW IS IN THIS array VARIABLE $row[0][0] AND THESE ARE array INDEX LIKE INDEX ARE COLUMN. ON 0 array INDEX NEED ZERO array INDEX VALUE. TRY TO RUN CODE AND CHECK OUT WHAT EXACTLY WHAT HAPPENING IN IT. or see http://www.php.net/mysql_fetch_row hope it helps you alote Thank you for helping I love this forums ! I appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/175457-solved-explaination-needed-please-for-sql/#findComment-924595 Share on other sites More sharing options...
samoi Posted September 25, 2009 Author Share Posted September 25, 2009 well, that is wrong first off. but ignoring that it basically means that $row is a multidimensional array, or an array of arrays. say for instance we had an array definition that looked like $arr = array(0=>array("array", "of", "different", "strings"), 1=> array("More", "strings")/*etc.. */); the first index, accessed by $row[0]; which is an array with 4 different entries. You can also access the first index of the array at index 0 by doing $row[0][0] if I were to echo that it would say "Array" if I were to output stuff like so echo "I am an " . $row[0][0] . " " . $row[0][1] . " " . $row[0][2] . " " . $row[0][3]; the output would be I am an array of different strings. Just think of it as a table. the first set of brackets is the row, and the second is the column Now for why thats wrong. The function mysql_fetch_array() only returns a single dimension array with the elements that your SQL query returned from one row That means that doing that function will only return the first row of your sql return set. if you want everything, you have to iterate through the array, like so $query="select timediff(dueTime, now()) from store where userid=".$id; $result=mysql_query($query, $db_id); $allTheRows = array(); while($row = mysql_fetch_array($result)){//this is the key part $allTheRows[] = $row//this pushes the content of $row into the array allTheRows } now you could do echo $allTheRows[0][0]; and that would echo the first column of the first row returned. However in your code, the array $row is 1 dimensional, and it is being accessed if it were two dimensional. which will probably throw error. You are the man! Amazing quick tutorial ! I love it! so many thanks for you my friend ! and thank you too for being patient and offering a FULL tutorial ! I appreciate that! Quote Link to comment https://forums.phpfreaks.com/topic/175457-solved-explaination-needed-please-for-sql/#findComment-924597 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.