Jump to content

[SOLVED] explaination needed please! for $SQL


Recommended Posts

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]
?>

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

 

 

 

 

 

 

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.

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!

 

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.