Click to See Complete Forum and Search --> : Need Help with code
DavidAP
05-23-2006, 06:14 PM
I like to play around with code as much as the next guy... well not really, but I have been and with no success. I have to get moving on this project and I can seem to get a script to work.
Here's my script, please show me how to get working:
<?
$username="user";
$password="passwd";
$db="bible";
mysql_connect("localhost",$username,$password);
@mysql_select_db($db) or die( "Unable to select database");
$query="SELECT * FROM genesis WHERE book='Genesis', chapter='1', verse='1'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$book=mysql_result($result,$i,"book");
$chapter=mysql_result($result,$i,"chapter");
$verse=mysql_result($result,$i,"verse");
$text=mysql_result($result,$i,"text");
echo "$book $chapter: $verse $text";
}
?>
I'm getting this error on this script:
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource on line 58.
This is line 58: $num=mysql_numrows($result);
I'm sure there will be more errors behind this one, but I'll have to take them one at a time.
Help would be appreciated - I've been working on this script for two days now - 17 hours each day. Failure is getting old.
Thanks
David
$query="SELECT * FROM genesis WHERE book='Genesis', chapter='1', verse='1'";
$result=mysql_query($query);
$num=mysql_numrows($result);
I'm getting this error on this script:
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource on line 58.
as a fellow hacker I can appreciate you frustration. when I get that error message, it means to me that I got no result from the query. Either the query is wrong or there are no results. If I know that I should get results (or at least pretty sure), it means I have a malformed query.
I would add a print line:
echo "<br>query= $query";
just before $result=mysql_query($query);
then copy and paste that into the SQL monitor in phpmyadmin to see where the error occurs.
your query probably needs to be something like:
$query="SELECT * FROM genesis WHERE book='Genesis' AND chapter='1' AND verse='1'";
and you might want to take a look at the hyperlink my signature line.
.
NogDog
05-23-2006, 07:16 PM
At least while developing your code, instead of just doing...
$result=mysql_query($query);
...use this idiom in case the query doesn't work:
$result=mysql_query($query) or die("Query failed: $query - " . mysql_error());
Then if it fails you'll see the actual query submitted to mysql as well as the error message returned from mysql.
DavidAP
05-23-2006, 08:52 PM
Ok, great! Thanks to both of you. Here's what I ended up with and it works.
This will allow a single verse to be pulled from the database.
<?
$username="user";
$password="passwd";
$db="bible";
mysql_connect("localhost",$username,$password);
@mysql_select_db($db) or die( "Unable to select database");
$query="SELECT * FROM genesis WHERE book='Genesis' AND chapter='1' AND verse='1'";
$result=mysql_query($query) or die("Query failed: $query - " . mysql_error());
$num=mysql_numrows($result);
mysql_close();
?>
<?
$i=0;
while ($i < $num) {
$book=mysql_result($result,$i,"book");
$chapter=mysql_result($result,$i,"chapter");
$verse=mysql_result($result,$i,"verse");
$text=mysql_result($result,$i,"text");
?>
<p><? echo "$book"; ?> <? echo "$chapter"; ?>: <? echo "$verse"; ?><br><br>
<? echo "$text"; ?></p>
<?
++$i;
}
?>
To display the entire chapter I did this:
<?
$username="user";
$password="passwd";
$db="bible";
mysql_connect("localhost",$username,$password);
@mysql_select_db($db) or die( "Unable to select database");
$query="SELECT * FROM 2kings WHERE number='12' AND chapter='23'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<?
$book=mysql_result($result,$i,"book");
$chapter=mysql_result($result,$i,"chapter");
?>
<p><b><? echo "$book"; ?> <? echo "$chapter"; ?></b></p>
<?
$i=0;
while ($i < $num) {
$book=mysql_result($result,$i,"book");
$chapter=mysql_result($result,$i,"chapter");
$verse=mysql_result($result,$i,"verse");
$text=mysql_result($result,$i,"text");
?>
<p><? echo "$verse"; ?> <? echo "$text"; ?></p>
<?
++$i;
}
?>
It works. Now I have to make one just like it for each Book and each Chapter. I've got work ahead of me.
I have all the Books listed on an HTML page with the Chapter numbers listed to the side. You click on a number to read that Chapter. Right now I'm having to put the entire Chapter on an HTML page. Putting it all in a database will make formatting easier. I'll still have the same number of pages (php now rather than html) but a lot less typing.