...

View Full Version : mySQL and PHP question



smeshy123
06-12-2005, 05:57 AM
$sql = mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die(mysql_error());


That is a peice of code I have to select something from the PHP database, now I would like to know how to print that I selected onto my page. I tried searching for an answer, and couldn't find one. I'm probably searching the wrong thing. I tried:



echo $sql;


but that just prints:



Resource id #3


as opposed to what the actual question is.

Thanks!

Smesh

gsoft
06-12-2005, 06:07 AM
That echo'ed correctly, what you need to do is this.



$sql = mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die(mysql_error());
if ($sql)
{
$result = mysql_fetch_assoc($sql);
echo $result['question'];
}
else
{
//query failed
}


There are other function instead of mysql_fetch_assoc (http://php.net/mysql_fetch_assoc) just look in the See Also section.

smeshy123
06-12-2005, 06:53 AM
That code doesn't work. It all looks right, but for some reason I just get a blank screen. Any other ideas? Or whats the fucntion to print an error so I know whats wrong?

Thanks!

Smesh

gsoft
06-12-2005, 07:24 AM
More than likely an error in your Query



$sql = mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die(mysql_error());
if (mysql_num_rows($sql) != 0)
{
if ($sql)
{
$result = mysql_fetch_assoc($sql);
var_dump($result);
//if still nothing try mysql_fetch_array($sql, MYSQL_ASSOC);
}
else
{
//query failed
echo mysql_error(); //should have already been processed in the or die though
}
}
else
{
echo "No Rows Found : ". mysql_num_rows($sql);
}

smeshy123
06-12-2005, 08:26 AM
More than likely an error in your Query



$sql = mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die(mysql_error());
if (mysql_num_rows($sql) != 0)
{
if ($sql)
{
$result = mysql_fetch_assoc($sql);
var_dump($result);
//if still nothing try mysql_fetch_array($sql, MYSQL_ASSOC);
}
else
{
//query failed
echo mysql_error(); //should have already been processed in the or die though
}
}
else
{
echo "No Rows Found : ". mysql_num_rows($sql);
}


If there was an error in my querry, wouldn't it die instead of just not doing anything, without any of that if code in my document?

gsoft
06-12-2005, 08:33 AM
Yes my comments state that though



//query failed
echo mysql_error(); //should have already been processed in the or die though


Its why I also made sure that there were rows returned from the query using mysql_num_rows more than likely why your getting a blank screen, that mysql_error() was also put for safety net incase you got a blank screen an error would popup or mysql_error return no error results also giving you an idea on where the code is going and how to debug.

smeshy123
06-12-2005, 08:39 AM
Ok, I get it...only problem is that your code doesn't work either :'(. I know the problem isn't with the Query string because I went into PHP admin copy and pasted it into the SQL Run Query thing and it worked fine. I still also get a white screen. I'll post more code from higher up, maybe I made a mistake somewhere else:



$link = mysql_connect('localhost', 'root');
if (!$link) {
die('Could not connect');
}
mysql_select_db("index");
$randvalue = rand(1,2);
echo $randvalue;


$sql = mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die("doesnt work");
if*(mysql_num_rows($sql)*!=*0)
{
*****if*($sql)
*****{
*********$result*=*mysql_fetch_assoc($sql);
*********var_dump($result);
*********//if still nothing try mysql_fetch_array($sql);
****}
****else
****{
********//query failed
********echo*mysql_error();*//should have already been processed in the or die though
*****}
}*
else
{
****echo*"No Rows Found : ".*mysql_num_rows($sql);*
}*


Thanks for the quick response!

- Smesh

PS: I have NO clue why those *'s are popping up in the forum, and I can't figure out how to get rid of them.

A couple other things to clear up: index = my database, spokenword = a table in my database, question = a column/row (i always confuse them) within my database. The database is higher up in the heiarchy of the server. The php file is Files/Testing/index.php while phpmyadmin is in Files/phpmyadmin

I don't know if any of that makes a difference?

gsoft
06-12-2005, 08:43 AM
View source your file, if blank there too open your php.ini file and change the error reporting to E_ALL restart the server


There should also be a third parameter in the mysql_connect function for password if its blank just use , ""

smeshy123
06-12-2005, 08:46 AM
where is the php.ini file located? I am new to php, and have never needed to use it before. I ran a quick serach on my laptop for it once, and couldn't find it for some reason, when I was playing around with some XML/PHP stuff.

Thanks!

Smesh

gsoft
06-12-2005, 08:57 AM
You using Windows?
If so there are a couple of locations

Using IIS
Should be in the PHP Directory with dlls (e.g. c:\php)
System Folder

Apache
Same directory as the Apache Binary
PHP Binary c:\php
System Folder

Since I dont use Windows with PHP much I usually have the php.ini with the Apache Binary to keep installation clean.

If your using Linux it will depend on your --prefix= settings if set. e.g. --prefix=/usr/local/php would be in the /usr/local/php/lib directory

smeshy123
06-12-2005, 09:00 AM
actually im using Mac..OSX.3 with Apache

gsoft
06-12-2005, 09:02 AM
http://www.php.net/manual/en/install.macosx.php should help you

smeshy123
06-12-2005, 09:23 AM
View source your file, if blank there too open your php.ini file and change the error reporting to E_ALL restart the server


There should also be a third parameter in the mysql_connect function for password if its blank just use , ""


ok, I have the php.ini file open in pico, what is the exact name of the error reporting thing? because I see a lot of different error stuff and all of them just say on or off.

Thanks!

Smesh

gsoft
06-12-2005, 09:50 AM
There should be a bit with



error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT


Something like that, change that to just



error_reporting = E_ALL


Then below that make sure display_errors is On



display_errors = On


Restart the server.

smeshy123
06-16-2005, 04:31 AM
How do you restart an Apache Server on Mac OSX 10.4? Never had to do it before...

Thanks!

Smesh

gsoft
06-16-2005, 04:58 AM
http://phpmac.com/articles.php?view=225 should help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum