Page 1 of 1

retrieving values from mysql

Posted: Wed Apr 20, 2011 8:28 pm
by danwguy
Ok, so I have a table called test, in the table there is rows for everyday of the week, titled monday, teusday... etc for monday there are time, 6am, 10am, 11am...etc I am trying to get those times to be echo-ed onto the screen but for some reason I can't figure it out. I have always run sql commands with

Code: Select all

 while($row = mysql_fetch_assoc($sql)) {
echo $row['monday'];
and so on, but there is no while loop for this one, I am using

Code: Select all

sql = mysql_query("SELECT monday FROM test WHERE first_name='Mary'");
nowhow do I get that array of 6am, 7am, 10a, 11am, and so on, out to the screen? Thank you for the advance.

Re: retrieving values from mysql

Posted: Wed Apr 20, 2011 8:43 pm
by fugix
do you have the times for monday stored in an array?

Re: retrieving values from mysql

Posted: Wed Apr 20, 2011 8:55 pm
by danwguy
this is what I have...

Code: Select all

<?php
session_start();
require("connect.php");
$day = "monday";
$sql = 'SELECT monday FROM test WHERE first_name=\'Mary\'';
	if(!$sql) {
		echo "Nope - " .mysql_error();
		exit();
		}
echo $row['monday'];


?>
picture of the table... http://www.theials.com/table.jpg
table called test, with rows first_name, last_name, monday, tuesday, wednesday, thrsday, friday, saturday, sunday, ID
in monday the values stored are 6am, 10am, 11am, 12am, 12pm, 1pm, 5pm, 6pm, 7pm, 8pm, 9pm, 10pm, 11pm.
So when I use

Code: Select all

$sql = mysql_query("SELECT monday FROM test WHERE first_name='Mary'");
how would I then get all those 6am, 10am... and so on out to the screen, since I can't say while($row= mysql_fetch_assoc($sql)) { echo $row['monday']; }
because I'm selecting monday, so there is no fetch_array or whatever.

Re: retrieving values from mysql

Posted: Wed Apr 20, 2011 9:03 pm
by fugix
you can use the while loop that you stated above..why do you think you wouldnt be able to?

Re: retrieving values from mysql

Posted: Wed Apr 20, 2011 9:07 pm
by danwguy
when I put the while loop in

Code: Select all

<?php
session_start();
require("connect.php");
$day = "monday";
$sql = 'SELECT monday FROM test WHERE first_name=\'Mary\'';
	if(!$sql) {
		echo "Nope - " .mysql_error();
		exit();
		}
while($row = mysql_fetch_assoc($sql)) {
echo $row['monday'];
}


?>
i get this error...
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/test.php on line 10

Re: retrieving values from mysql

Posted: Wed Apr 20, 2011 9:58 pm
by fugix

Code: Select all

$sql = 'SELECT monday FROM test WHERE first_name=\'Mary\'';
needs to be...

Code: Select all

$sql =  mysql_query("SELECT monday FROM test WHERE first_name='Mary''');
then try it again

Re: retrieving values from mysql

Posted: Wed Apr 20, 2011 10:49 pm
by danwguy
same result

Re: retrieving values from mysql

Posted: Wed Apr 20, 2011 10:50 pm
by fugix
dunno if you copy pasted my code...but the last quotation should be a double quotation not a single quotation like i have